-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.js
77 lines (67 loc) · 2.6 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
document.addEventListener("DOMContentLoaded", function() {
const bot = new RiveScript();
bot.loadFile("brain.rive").then(botReady).catch(botError);
const chatOutput = document.getElementById("chat-output");
const userInput = document.getElementById("user-input");
const sendButton = document.getElementById("send-btn");
const clearButton = document.getElementById("clear-btn");
// Load chat history from local storage on page load
loadChatHistory();
function botReady() {
bot.sortReplies();
}
function botError(error) {
console.error("Error loading RiveScript:", error);
chatOutput.innerHTML += "<p><strong>Error:</strong> Couldn't load the bot brain.</p>";
}
// Send user input to the bot on button click
sendButton.addEventListener("click", function() {
let input = userInput.value.trim();
if (input) {
addChat(input, "user");
bot.reply("local-user", input).then(function(reply) {
addChat(reply, "bot");
});
userInput.value = "";
}
});
// Clear chat history on button click
clearButton.addEventListener("click", function() {
localStorage.removeItem("chatHistory");
chatOutput.innerHTML = "";
});
// Function to add chat messages to the output and save to local storage
function addChat(text, sender) {
const message = document.createElement("p");
message.className = sender;
message.innerHTML = `<strong>${sender === 'user' ? 'You' : 'Bot'}:</strong> ${text}`;
chatOutput.appendChild(message);
chatOutput.scrollTop = chatOutput.scrollHeight;
// Save to local storage
saveChatHistory();
}
// Save chat history to local storage
function saveChatHistory() {
const chatMessages = [];
chatOutput.querySelectorAll("p").forEach(p => {
chatMessages.push({
sender: p.className,
text: p.innerHTML
});
});
localStorage.setItem("chatHistory", JSON.stringify(chatMessages));
}
// Load chat history from local storage
function loadChatHistory() {
const chatHistory = JSON.parse(localStorage.getItem("chatHistory"));
if (chatHistory) {
chatHistory.forEach(msg => {
const message = document.createElement("p");
message.className = msg.sender;
message.innerHTML = msg.text;
chatOutput.appendChild(message);
});
chatOutput.scrollTop = chatOutput.scrollHeight;
}
}
});