File size: 1,600 Bytes
1a25ebd |
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 |
document.addEventListener("DOMContentLoaded", function() {
const chatHistory = document.getElementById("chat-history");
const userInput = document.getElementById("user-input");
const sendButton = document.getElementById("send-button");
sendButton.addEventListener("click", function() {
const userMessage = userInput.value.trim();
if (userMessage !== "") {
appendMessage(userMessage, true);
userInput.value = "";
sendMessageToPythonAnywhere(userMessage);
}
});
function appendMessage(message, isUser) {
const messageElement = document.createElement("div");
messageElement.classList.add("message", isUser ? "user-message" : "bot-message");
messageElement.textContent = message;
chatHistory.appendChild(messageElement);
chatHistory.scrollTop = chatHistory.scrollHeight;
}
async function sendMessageToPythonAnywhere(message) {
try {
const response = await fetch('YOUR_PYTHONANYWHERE_API_ENDPOINT', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: message })
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
const botResponse = data.message;
appendMessage(botResponse, false);
} catch (error) {
console.error('Error:', error);
}
}
});
|