-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_script.js
59 lines (48 loc) · 2.11 KB
/
bot_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
// Function to handle user input and send messages
async function sendMessage() {
const userInput = document.getElementById('user-input').value.trim();
if (userInput === '') return;
appendMessage('user', userInput);
// Example: Check if user input includes 'joke' and fetch Chuck Norris joke
if (userInput.toLowerCase().includes('joke')) {
const joke = await fetchChuckNorrisJoke();
appendMessage('bot', joke);
} else {
appendMessage('bot', "I'm sorry, I don't understand.");
}
document.getElementById('user-input').value = '';
}
// Function to fetch Chuck Norris joke from API
async function fetchChuckNorrisJoke() {
try {
const response = await fetch('https://api.chucknorris.io/jokes/random');
if (!response.ok) {
throw new Error('Failed to fetch Chuck Norris joke');
}
const data = await response.json();
return data.value; // Assuming 'value' field contains the joke text
} catch (error) {
console.error('Error fetching Chuck Norris joke:', error.message);
return 'Oops! Failed to fetch a joke. Please try again later.';
}
}
// Function to append messages to the message container
function appendMessage(sender, message) {
const messageContainer = document.getElementById('message-container');
const messageElement = document.createElement('div');
messageElement.classList.add(`${sender}-message`);
messageElement.textContent = message;
messageContainer.appendChild(messageElement);
// Scroll to bottom of message container
messageContainer.scrollTop = messageContainer.scrollHeight;
}
// Event listener for pressing Enter key in input field
document.getElementById('user-input').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendMessage();
}
});
// Initial message from the bot
appendMessage('bot', 'Welcome! Type your concern/any doubt.');
// Example: Handle additional commands or APIs based on user input
// Feel free to expand this functionality as needed