-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
131 lines (112 loc) · 4.9 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Define pre-defined responses and timers
const responses = {
"hi": ["Hello!", "Hi there!", "Hey!"],
"how are you?": ["I'm doing well, thank you!", "I'm great, thanks for asking!", "All good, how about you?"],
"hru": ["I'm doing well, thank you!", "I'm great, thanks for asking!", "All good, how about you?"],
"how are you": ["I'm doing well, thank you!", "I'm great, thanks for asking!", "All good, how about you?"],
"what's your name?": ["I'm just a simple AI chatbot.", "You can call me ChatBot.", "I'm an AI assistant."],
"bye": ["Goodbye! Have a nice day.", "See you later!", "Take care!"],
"how to make pizza?": ["To make a pizza, you will need dough, tomato sauce, cheese, and your choice of toppings. Here's a simple recipe:\n1. Preheat your oven to 475°F (245°C).\n2. Roll out the dough on a floured surface to your desired thickness.\n3. Spread tomato sauce evenly over the dough, leaving a small border around the edges.\n4. Sprinkle shredded cheese over the sauce.\n5. Add your favorite toppings, such as pepperoni, mushrooms, onions, or bell peppers.\n6. Bake in the preheated oven for 12-15 minutes, or until the crust is golden brown and the cheese is bubbly and melted.\n7. Remove from the oven, let it cool for a few minutes, then slice and enjoy!"],
};
const timers = {};
// Function to get response
async function getResponse(message) {
const lowercaseMessage = message.toLowerCase();
// Check if the message has a pre-defined response
if (responses.hasOwnProperty(lowercaseMessage)) {
const possibleResponses = responses[lowercaseMessage];
if (Array.isArray(possibleResponses)) {
return possibleResponses[Math.floor(Math.random() * possibleResponses.length)];
} else {
return possibleResponses;
}
}
// Check if the message is a mathematical expression
const mathExpressionMatch = message.match(/^[+\-*/^0-9().\s]+$/);
if (mathExpressionMatch) {
try {
const result = eval(mathExpressionMatch[0]);
return `The result of ${mathExpressionMatch[0]} is ${result}.`;
} catch (error) {
return "Sorry, I couldn't evaluate that expression.";
}
}
// Check if the message is about setting a timer
const timerMatch = message.match(/(?:make|set) (\d+) (seconds?|minutes?|hours?) timer/i);
if (timerMatch) {
const time = parseInt(timerMatch[1]);
const unit = timerMatch[2].toLowerCase();
const timerDuration = calculateTimerDuration(time, unit);
if (timerDuration > 0) {
setTimer(timerDuration);
return `Timer set for ${time} ${unit}.`;
}
}
// If no pre-defined response, timer command, or mathematical expression, return a generic response
return "I'm sorry, I don't understand that.";
}
// Function to fetch information about World War I or World War II from Wikipedia
async function fetchWorldWarInfo(worldWar) {
try {
const response = await fetch(`https://en.wikipedia.org/api/rest_v1/page/summary/${worldWar}`);
const data = await response.json();
if (data.extract) {
return data.extract;
} else {
return "No information found for World War I or II.";
}
} catch (error) {
console.error(`Error fetching information for ${worldWar}:`, error);
return `Error fetching information for ${worldWar}.`;
}
}
// Function to calculate timer duration in milliseconds
function calculateTimerDuration(time, unit) {
let duration = 0;
switch (unit) {
case "second":
case "seconds":
duration = time * 1000;
break;
case "minute":
case "minutes":
duration = time * 60 * 1000;
break;
case "hour":
case "hours":
duration = time * 60 * 60 * 1000;
break;
}
return duration;
}
// Function to set timer
function setTimer(duration) {
const timerId = setTimeout(() => {
displayMessage("bot", "Timer done!");
delete timers[timerId];
}, duration);
timers[timerId] = true;
}
// Function to send message
async function sendMessage() {
const userInput = document.getElementById("user-input");
const message = userInput.value.trim();
if (message !== "") {
displayMessage("user", message);
const response = await getResponse(message);
setTimeout(() => {
displayMessage("bot", response);
}, 500);
userInput.value = "";
}
}
// Function to display message in the chat box
function displayMessage(sender, message) {
const chatBox = document.getElementById("chat-box");
const messageElement = document.createElement("div");
messageElement.classList.add("message", sender);
messageElement.innerText = message;
chatBox.appendChild(messageElement);
// Scroll to bottom
chatBox.scrollTop = chatBox.scrollHeight;
}