File size: 2,885 Bytes
45aeeb7
 
 
 
 
43d4650
 
45aeeb7
 
 
43d4650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45aeeb7
 
 
43d4650
 
 
 
45aeeb7
43d4650
 
 
 
45aeeb7
43d4650
 
 
45aeeb7
43d4650
 
 
 
 
45aeeb7
43d4650
 
 
 
 
 
 
45aeeb7
43d4650
 
 
 
 
 
 
45aeeb7
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vivi Chatbot</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">
            <h1>Vivi - Trợ lý ảo</h1>
        </div>
        <div class="chat-box" id="chat-box">
            {% for entry in history %}
                <div class="user-message"><span>{{ entry.timestamp }}</span> {{ entry.user }}</div>
                <div class="bot-message"><span>{{ entry.timestamp }}</span> {{ entry.bot }}</div>
            {% endfor %}
            <div id="typing-indicator" class="bot-message" style="display: none;">Đang gõ...</div>
        </div>
        <form id="chat-form">
            <input type="text" id="user-input" placeholder="Nhập tin nhắn..." autocomplete="off" required>
            <button type="submit">Gửi</button>
            <button type="button" id="clear-chat">🗑️</button>
        </form>
    </div>

    <script>
        document.getElementById("chat-form").addEventListener("submit", async function(event) {
            event.preventDefault();
            const userInput = document.getElementById("user-input").value;
            const chatBox = document.getElementById("chat-box");

            // Hiển thị tin nhắn người dùng
            const timestamp = new Date().toLocaleTimeString();
            chatBox.innerHTML += `<div class="user-message"><span>${timestamp}</span> ${userInput}</div>`;
            document.getElementById("user-input").value = "";

            // Hiển thị trạng thái "đang gõ..."
            const typingIndicator = document.getElementById("typing-indicator");
            typingIndicator.style.display = "block";

            // Gửi tin nhắn đến server
            const response = await fetch("/chat", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ message: userInput })
            });
            const data = await response.json();

            // Ẩn trạng thái "đang gõ..." và hiển thị phản hồi bot
            typingIndicator.style.display = "none";
            chatBox.innerHTML += `<div class="bot-message"><span>${data.timestamp}</span> ${data.response}</div>`;
            chatBox.scrollTop = chatBox.scrollHeight;
        });

        // Xóa lịch sử chat
        document.getElementById("clear-chat").addEventListener("click", async function() {
            const response = await fetch("/clear", { method: "POST" });
            if (response.ok) {
                document.getElementById("chat-box").innerHTML = ""; // Xóa nội dung chat-box
            }
        });
    </script>
</body>
</html>