-
Notifications
You must be signed in to change notification settings - Fork 216
/
chatbot.js
87 lines (76 loc) · 2.73 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Send user input to the API and get a response
async function chatbot(input) {
try {
const response = await fetch("https://ticket-chatbot-final.onrender.com/chatbot", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ question: input }),
});
if (!response.ok) {
throw new Error(`Server responded with status: ${response.status}`);
}
const data = await response.json(); // Parse the JSON response
console.log("API Response:", data); // Log the entire response to inspect its structure
return data.response; // Return the 'response' property from the API
} catch (error) {
console.error("Error:", error);
return "Sorry, I am having trouble connecting to the server.";
}
}
// Display the user message on the chat
function displayUserMessage(message) {
let chat = document.getElementById("chatbot-chat");
let userMessage = document.createElement("div");
userMessage.classList.add("chatbot-message", "chatbot-user");
let userAvatar = document.createElement("div");
userAvatar.classList.add("chatbot-avatar");
let userText = document.createElement("div");
userText.classList.add("chatbot-text");
userText.innerHTML = message;
userMessage.appendChild(userAvatar);
userMessage.appendChild(userText);
chat.appendChild(userMessage);
chat.scrollTop = chat.scrollHeight;
}
// Display the bot message on the chat
function displayBotMessage(message) {
let chat = document.getElementById("chatbot-chat");
let botMessage = document.createElement("div");
botMessage.classList.add("chatbot-message", "chatbot-bot");
let botAvatar = document.createElement("div");
botAvatar.classList.add("chatbot-avatar");
let botText = document.createElement("div");
botText.classList.add("chatbot-text");
botText.innerHTML = message;
botMessage.appendChild(botAvatar);
botMessage.appendChild(botText);
chat.appendChild(botMessage);
chat.scrollTop = chat.scrollHeight;
}
// Send the user message and get the bot response
async function sendMessage() {
let input = document.getElementById("chatbot-input").value;
if (input) {
displayUserMessage(input);
// Wait for the chatbot response
let output = await chatbot(input); // Ensure this line is awaited
setTimeout(function () {
displayBotMessage(output);
}, 1000);
document.getElementById("chatbot-input").value = "";
}
}
// Add a click event listener to the button
document
.getElementById("chatbot-button")
.addEventListener("click", sendMessage);
// Add a keypress event listener to the input
document
.getElementById("chatbot-input")
.addEventListener("keypress", function (event) {
if (event.keyCode == 13) {
sendMessage();
}
});