File size: 915 Bytes
aeea182
 
 
 
 
 
 
 
 
 
 
b45a0d5
aeea182
b45a0d5
aeea182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// script.js
async function sendMessage() {
    const userInput = document.getElementById("user-input");
    const message = userInput.value;

    if (message.trim() === "") return;

    displayMessage("You: " + message, "user-message");
    userInput.value = "";

    // Send message to the server
    const response = await fetch("/get_response", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ message }),
    });

    const data = await response.json();
    displayMessage("Bot: " + data.response, "bot-message");
}

function displayMessage(text, className) {
    const chatLog = document.getElementById("chat-log");
    const messageElement = document.createElement("div");
    messageElement.className = className;
    messageElement.textContent = text;
    chatLog.appendChild(messageElement);
    chatLog.scrollTop = chatLog.scrollHeight;
}