Spaces:
Sleeping
Sleeping
Arrcttacsrks
commited on
Commit
•
43d4650
1
Parent(s):
688c860
Update index.html
Browse files- index.html +47 -59
index.html
CHANGED
@@ -3,76 +3,64 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>Chatbot</title>
|
7 |
-
<link rel="stylesheet" href="style.css">
|
8 |
</head>
|
9 |
<body>
|
10 |
<div class="chat-container">
|
11 |
-
<
|
12 |
-
|
13 |
-
|
14 |
-
<
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
</div>
|
18 |
|
19 |
<script>
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
const clearButton = document.getElementById('clear-button');
|
25 |
-
|
26 |
-
// Lưu trữ lịch sử tin nhắn
|
27 |
-
const chatHistory = JSON.parse(localStorage.getItem('chatHistory')) || [];
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
chatBox.innerHTML += `<div class="
|
32 |
-
|
33 |
-
|
34 |
-
function addMessage(role, content) {
|
35 |
-
chatHistory.push({ role, content });
|
36 |
-
localStorage.setItem('chatHistory', JSON.stringify(chatHistory));
|
37 |
-
chatBox.innerHTML += `<div class="${role}-message">${content}</div>`;
|
38 |
-
chatBox.scrollTop = chatBox.scrollHeight; // Cuộn xuống dưới
|
39 |
-
}
|
40 |
-
|
41 |
-
sendButton.addEventListener('click', sendMessage);
|
42 |
-
userInput.addEventListener('keypress', function (e) {
|
43 |
-
if (e.key === 'Enter') {
|
44 |
-
sendMessage();
|
45 |
-
}
|
46 |
-
});
|
47 |
-
|
48 |
-
clearButton.addEventListener('click', clearChat);
|
49 |
|
50 |
-
|
51 |
-
const
|
52 |
-
|
53 |
-
if (!message) return;
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
headers: {
|
61 |
-
'Content-Type': 'application/json'
|
62 |
-
},
|
63 |
-
body: JSON.stringify({ message })
|
64 |
-
})
|
65 |
-
.then(response => response.json())
|
66 |
-
.then(data => {
|
67 |
-
addMessage('bot', `<strong>Bot:</strong> ${data.response}`);
|
68 |
});
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
</script>
|
77 |
</body>
|
78 |
</html>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Vivi Chatbot</title>
|
7 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
8 |
</head>
|
9 |
<body>
|
10 |
<div class="chat-container">
|
11 |
+
<div class="chat-header">
|
12 |
+
<h1>Vivi - Trợ lý ảo</h1>
|
13 |
+
</div>
|
14 |
+
<div class="chat-box" id="chat-box">
|
15 |
+
{% for entry in history %}
|
16 |
+
<div class="user-message"><span>{{ entry.timestamp }}</span> {{ entry.user }}</div>
|
17 |
+
<div class="bot-message"><span>{{ entry.timestamp }}</span> {{ entry.bot }}</div>
|
18 |
+
{% endfor %}
|
19 |
+
<div id="typing-indicator" class="bot-message" style="display: none;">Đang gõ...</div>
|
20 |
+
</div>
|
21 |
+
<form id="chat-form">
|
22 |
+
<input type="text" id="user-input" placeholder="Nhập tin nhắn..." autocomplete="off" required>
|
23 |
+
<button type="submit">Gửi</button>
|
24 |
+
<button type="button" id="clear-chat">🗑️</button>
|
25 |
+
</form>
|
26 |
</div>
|
27 |
|
28 |
<script>
|
29 |
+
document.getElementById("chat-form").addEventListener("submit", async function(event) {
|
30 |
+
event.preventDefault();
|
31 |
+
const userInput = document.getElementById("user-input").value;
|
32 |
+
const chatBox = document.getElementById("chat-box");
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
// Hiển thị tin nhắn người dùng
|
35 |
+
const timestamp = new Date().toLocaleTimeString();
|
36 |
+
chatBox.innerHTML += `<div class="user-message"><span>${timestamp}</span> ${userInput}</div>`;
|
37 |
+
document.getElementById("user-input").value = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
// Hiển thị trạng thái "đang gõ..."
|
40 |
+
const typingIndicator = document.getElementById("typing-indicator");
|
41 |
+
typingIndicator.style.display = "block";
|
|
|
42 |
|
43 |
+
// Gửi tin nhắn đến server
|
44 |
+
const response = await fetch("/chat", {
|
45 |
+
method: "POST",
|
46 |
+
headers: { "Content-Type": "application/json" },
|
47 |
+
body: JSON.stringify({ message: userInput })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
});
|
49 |
+
const data = await response.json();
|
50 |
+
|
51 |
+
// Ẩn trạng thái "đang gõ..." và hiển thị phản hồi bot
|
52 |
+
typingIndicator.style.display = "none";
|
53 |
+
chatBox.innerHTML += `<div class="bot-message"><span>${data.timestamp}</span> ${data.response}</div>`;
|
54 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
55 |
+
});
|
56 |
|
57 |
+
// Xóa lịch sử chat
|
58 |
+
document.getElementById("clear-chat").addEventListener("click", async function() {
|
59 |
+
const response = await fetch("/clear", { method: "POST" });
|
60 |
+
if (response.ok) {
|
61 |
+
document.getElementById("chat-box").innerHTML = ""; // Xóa nội dung chat-box
|
62 |
+
}
|
63 |
+
});
|
64 |
</script>
|
65 |
</body>
|
66 |
</html>
|