-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainpage.js
32 lines (27 loc) · 1.19 KB
/
mainpage.js
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
// Function to show the chatbox when the button is clicked
function startChat() {
document.getElementById('chat-box').style.display = 'block';
document.querySelector('.talk-button').style.display = 'none';
}
// Function to send the message to the Python chatbot
async function sendMessage() {
const userInput = document.getElementById('user-input').value;
if (userInput.trim() !== "") {
// Show user's message in the chat log
document.getElementById('chat-log').innerHTML += `<p><strong>You:</strong> ${userInput}</p>`;
// Clear input field
document.getElementById('user-input').value = '';
// Send the message to the server (Python backend)
const response = await fetch('/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: userInput })
});
const data = await response.json();
const botMessage = data.reply;
// Show chatbot's response in the chat log
document.getElementById('chat-log').innerHTML += `<p><strong>Bot:</strong> ${botMessage}</p>`;
}
}