-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (63 loc) · 2.61 KB
/
script.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
var sendBtn = document.getElementById('sendBtn');
var textbox = document.getElementById('textbox');
var chatContainer = document.getElementById('chatContainer');
var arrayofPossibleMessage = [
{ message: "hi", response: "Hello dear!" },
{ message: "how are you", response: "I am great!" },
{ message: "what is your name", response: "I am a Chatbot." },
{ message: "who made you", response: "The name Vivi made me." },
{
message: "How do I make a chocolate cake?",
response: "To make a chocolate cake, you'll need flour, sugar, cocoa powder, baking powder, eggs, milk, and butter. Mix the ingredients, bake at 350°F (175°C) for 30-35 minutes."
},
{
message: "What is the tallest mountain in the world?",
response: "The tallest mountain in the world is Mount Everest, which is about 29,029 feet (8,848 meters) tall."
},
];
function sendMessage(userMessage) {
var messageElement = document.createElement('div');
messageElement.style.textAlign = "right";
messageElement.style.marginRight = "2px";
messageElement.innerHTML = "<span>You:</span> <span>" + userMessage + "</span>";
chatContainer.appendChild(messageElement);
chatContainer.scrollTop = chatContainer.scrollHeight; // Auto-scroll to the bottom
}
function chatbotResponse(userMessage) {
var chatbotMessage = "I didn't understand that.";
var result = arrayofPossibleMessage.find(val => val.message.toLowerCase() === userMessage.toLowerCase());
if (result) {
chatbotMessage = result.response;
}
var messageElement = document.createElement('div');
messageElement.style.textAlign = "left";
messageElement.style.marginLeft = "2px";
messageElement.innerHTML = "<span>Chatbot:</span> <span>" + chatbotMessage + "</span>";
setTimeout(() => {
messageElement.animate([{ opacity: 0.4 }, { opacity: 1 }], { duration: 1000 });
chatContainer.appendChild(messageElement);
chatContainer.scrollTop = chatContainer.scrollHeight; // Auto-scroll to the bottom
// Send data to the backend
fetch('http://localhost:8080/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ userMessage, chatbotResponse: chatbotMessage })
});
}, 1000);
}
sendBtn.addEventListener('click', function (e) {
var userMessage = textbox.value.trim();
if (userMessage === "") {
alert('Type a message!');
} else {
textbox.value = "";
sendMessage(userMessage);
chatbotResponse(userMessage);
}
});
// script.js
function toggleDarkMode() {
document.body.classList.toggle('dark-mode');
}