tudeplom commited on
Commit
494d6fc
·
verified ·
1 Parent(s): a6e93d8

Update static/js/main.js

Browse files
Files changed (1) hide show
  1. static/js/main.js +67 -58
static/js/main.js CHANGED
@@ -1,58 +1,67 @@
1
- // Variables
2
- var messages = document.querySelector('.message-list')
3
- var btn = document.querySelector('.btn')
4
- var input = document.querySelector('input')
5
-
6
- // Button/Enter Key
7
- btn.addEventListener('click', sendMessage)
8
- input.addEventListener('keyup', function(e){ if(e.keyCode == 13) sendMessage() })
9
-
10
- function loadHistory(){
11
- fetch('/history')
12
- .then(response => response.json())
13
- .then(data => {
14
- for(let i = 0; i < data.length; i++){
15
-
16
- if(data[i].FROM == 'User') {
17
- writeLine('<span>User</span><br><br> ${data[i].content}','primary')
18
- }
19
- else {
20
- writeLine('<span>oshinoko</span><br><br> ${data[i].content}','secondary')
21
- }
22
- }
23
-
24
- })
25
- .catch(error => console.error('Error:', error));
26
- }
27
-
28
-
29
-
30
- loadHistory()
31
- // Messenger Functions
32
- function sendMessage(){
33
- var msg = input.value;
34
- writeLine(`<span>User</span><br><br> ${msg}`,'primary')
35
-
36
- input.value = ''
37
- fetch('/chat', {
38
- method: 'POST',
39
- headers: {
40
- 'Content-Type': 'application/json'
41
- },
42
- body: JSON.stringify({'message': msg})
43
- })
44
- .then(response => response.json())
45
- .then(data => console.log(data, 'secondary'))
46
- .catch(error => console.error('Error:', error));
47
-
48
- }
49
- function addMessage(msg,typeMessage ='primary'){
50
- writeLine(`<span>${msg.FROM}</span><br><br> ${msg.MESSAGE}`,typeMessage)
51
- }
52
- function writeLine(text,typeMessage){
53
- var message = document.createElement('li')
54
- message.classList.add('message-item', 'item-'+typeMessage)
55
- message.innerHTML = 'Miguel says: ' + text
56
- messages.appendChild(message)
57
- messages.scrollTop = messages.scrollHeight;
58
- }
 
 
 
 
 
 
 
 
 
 
1
+ // Lấy các phần tử DOM
2
+ var messages = document.querySelector('.message-list');
3
+ var btn = document.querySelector('.btn');
4
+ var input = document.querySelector('input');
5
+
6
+ // Thêm sự kiện cho nút Send và phím Enter
7
+ btn.addEventListener('click', sendMessage);
8
+ input.addEventListener('keyup', function(e) {
9
+ if (e.key === 'Enter') sendMessage();
10
+ });
11
+
12
+ // Tải lịch sử chat khi trang load
13
+ function loadHistory() {
14
+ fetch('/history')
15
+ .then(response => response.json())
16
+ .then(data => {
17
+ for (let i = 0; i < data.length; i++) {
18
+ if (data[i].role === 'User') {
19
+ addMessage({ FROM: 'User', MESSAGE: data[i].content }, 'primary');
20
+ } else if (data[i].role === 'assistant') {
21
+ addMessage({ FROM: 'Oshi no Ko', MESSAGE: data[i].content }, 'secondary');
22
+ }
23
+ }
24
+ })
25
+ .catch(error => console.error('Error:', error));
26
+ }
27
+
28
+ loadHistory();
29
+
30
+ // Hàm gửi tin nhắn
31
+ function sendMessage() {
32
+ var msg = input.value.trim();
33
+ if (!msg) return; // Không gửi tin nhắn rỗng
34
+
35
+ // Hiển thị tin nhắn người dùng
36
+ addMessage({ FROM: 'User', MESSAGE: msg }, 'primary');
37
+
38
+ // Xóa input sau khi gửi
39
+ input.value = '';
40
+
41
+ // Gửi request đến server Flask
42
+ fetch('/chat', {
43
+ method: 'POST',
44
+ headers: { 'Content-Type': 'application/json' },
45
+ body: JSON.stringify({ message: msg })
46
+ })
47
+ .then(response => response.json())
48
+ .then(data => {
49
+ // Hiển thị phản hồi từ chatbot
50
+ addMessage({ FROM: 'Oshi no Ko', MESSAGE: data.MESSAGE }, 'secondary');
51
+ })
52
+ .catch(error => console.error('Error:', error));
53
+ }
54
+
55
+ // Hàm thêm tin nhắn vào giao diện
56
+ function addMessage(msg, typeMessage = 'primary') {
57
+ writeLine(`<span>${msg.FROM}</span><br><br> ${msg.MESSAGE}`, typeMessage);
58
+ }
59
+
60
+ // Hàm tạo dòng tin nhắn
61
+ function writeLine(text, typeMessage) {
62
+ var message = document.createElement('li');
63
+ message.classList.add('message-item', 'item-' + typeMessage);
64
+ message.innerHTML = text;
65
+ messages.appendChild(message);
66
+ messages.scrollTop = messages.scrollHeight; // Cuộn xuống cuối cùng
67
+ }