-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.js
55 lines (52 loc) · 1.72 KB
/
chatbot.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const chatContainer = document.getElementById("chat-container");
const userInput = document.getElementById("chatbot-user-input");
const sendButton = document.getElementById("chatbot-send-button");
const chatBotUrl = document.getElementById("chatbot-url");
function addMessage(message, sender) {
const messageElement = document.createElement("div");
messageElement.classList.add("message", sender);
messageElement.textContent = `${
sender === "user" ? "You" : "ChatBot"
}: ${message}`;
chatContainer.appendChild(messageElement);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
async function sendMessage() {
const message = userInput.value.trim();
if (message) {
addMessage(message, "user");
userInput.value = "";
let chatbot_url = chatBotUrl.value;
try {
const response = await fetch(chatbot_url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message }),
});
if (response.ok) {
const data = await response.json();
if (data.error) {
addMessage(`Error: ${data.error}`, "bot");
} else if (data.response) {
addMessage(data.response, "bot");
} else {
addMessage("Error: Unexpected response format", "bot");
}
} else {
addMessage(`Error: ${response.status} ${response.statusText}`, "bot");
}
} catch (error) {
console.error("Error:", error);
addMessage("Error: Unable to connect to the server", "bot");
}
}
}
sendButton.addEventListener("click", sendMessage);
userInput.addEventListener("keypress", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});