-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCerulean.js
62 lines (54 loc) · 1.91 KB
/
Cerulean.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
class Chatbot {
constructor() {
this.responses = {
"hello": ["Hello there!", "Hi there! How can I help you today?"],
"how are you": ["I'm doing well, thanks for asking!", "Pretty good, thanks! And you?"],
"bye": ["Goodbye! Talk to you later!", "Bye! Have a great day!"]
};
this.loadResponses();
}
loadResponses() {
const savedResponses = localStorage.getItem('chatbotResponses');
if (savedResponses) {
this.responses = JSON.parse(savedResponses);
}
}
saveResponses() {
localStorage.setItem('chatbotResponses', JSON.stringify(this.responses));
}
updateChatlog(message) {
const chatlog = document.getElementById('chatlog');
chatlog.value += message + "\n";
chatlog.scrollTop = chatlog.scrollHeight;
}
sendChat(userInput) {
this.updateChatlog("You: " + userInput);
const botResponse = this.generateResponse(userInput);
this.updateChatlog("Bot: " + botResponse);
document.getElementById('userinput').value = '';
}
generateResponse(input) {
input = input.toLowerCase();
for (const key in this.responses) {
if (input.includes(key)) {
const responses = this.responses[key];
return responses[Math.floor(Math.random() * responses.length)];
}
}
return "Sorry, I didn't understand that.";
}
addResponse(keyword, newResponse) {
if (!this.responses[keyword]) {
this.responses[keyword] = [];
}
this.responses[keyword].push(newResponse);
this.saveResponses();
}
}
const chatbot = new Chatbot();
document.getElementById('sendButton').addEventListener('click', () => {
const userText = document.getElementById('userinput').value.trim();
if (userText !== "") {
chatbot.sendChat(userText);
}
});