File size: 1,410 Bytes
aeea182
 
 
 
 
 
 
 
 
 
b45a0d5
aeea182
b45a0d5
aeea182
 
 
7fce0c8
 
 
535fb1f
7fce0c8
 
 
535fb1f
 
 
7fce0c8
 
 
27b6a0d
7fce0c8
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
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 }),
    });

    // Check if the response is okay
    try {
        const jsonResponse = await response.json();
        // Display the bot's response in the chatbox
        document.getElementById(
            "chatbox"
        ).value += `Bot: ${jsonResponse.response}\n`;

        // Also display the message using displayMessage function
        displayMessage("Bot: " + jsonResponse.response, "bot-message");
    } catch (error) {
        console.error("Failed to parse JSON:", error);
        document.getElementById(
            "chat-log"
        ).value += `Bot: Something went wrong. Please try again.\n`;
    }
}

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;
}