-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhome.js
More file actions
38 lines (32 loc) · 1.36 KB
/
home.js
File metadata and controls
38 lines (32 loc) · 1.36 KB
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
document.getElementById("logoutButton").addEventListener("click", function() {
alert("Logging out...");
window.location.href = "index.html"; // Redirect to login page
});
// Chatbot Functionality
function sendMessage() {
let userInput = document.getElementById("user-input").value;
let chatBox = document.getElementById("chat-box");
if (userInput.trim() === "") return;
// Display user message
chatBox.innerHTML += `<p><strong>You:</strong> ${userInput}</p>`;
// Send request to backend chatbot
fetch("http://127.0.0.1:5000/chat", { // Ensure correct API URL
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: userInput })
})
.then(response => response.json())
.then(data => {
if (data.reply) {
chatBox.innerHTML += `<p><strong>Bot:</strong> ${data.reply}</p>`;
} else {
chatBox.innerHTML += `<p><strong>Bot:</strong> Sorry, I didn't understand that.</p>`;
}
chatBox.scrollTop = chatBox.scrollHeight; // Auto-scroll
})
.catch(error => {
console.error("Error:", error);
chatBox.innerHTML += `<p><strong>AI:</strong> Error connecting to the chatbot.</p>`;
});
document.getElementById("user-input").value = ""; // Clear input field
}