-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.js
56 lines (48 loc) · 2.7 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
// Get chatbot elements
const chatbot = document.getElementById('chatbot');
const conversation = document.getElementById('conversation');
const inputForm = document.getElementById('input-form');
const inputField = document.getElementById('input-field');
// Add event listener to input form
inputForm.addEventListener('submit', function(event) {
// Prevent form submission
event.preventDefault();
// Get user input
const input = inputField.value;
// Clear input field
inputField.value = '';
const currentTime = new Date().toLocaleTimeString([], { hour: '2-digit', minute: "2-digit" });
// Add user input to conversation
let message = document.createElement('div');
message.classList.add('chatbot-message', 'user-message');
message.innerHTML = `<p class="chatbot-text" sentTime="${currentTime}">${input}</p>`;
conversation.appendChild(message);
// Generate chatbot response
const response = generateResponse(input);
// Add chatbot response to conversation
message = document.createElement('div');
message.classList.add('chatbot-message','chatbot');
message.innerHTML = `<p class="chatbot-text" sentTime="${currentTime}">${response}</p>`;
conversation.appendChild(message);
message.scrollIntoView({behavior: "smooth"});
});
// Generate chatbot response function
function generateResponse(input) {
// Add chatbot logic here
const responses = [
"Hello, how can I help you today? 😊",
"I'm sorry, I didn't understand your question. Could you please rephrase it? 😕",
"I'm here to assist you with any questions or concerns you may have. 📩",
"I'm sorry, I'm not able to browse the internet or access external information. Is there anything else I can help with? 💻",
"What would you like to know? 🤔",
"I'm sorry, I'm not programmed to handle offensive or inappropriate language. Please refrain from using such language in our conversation. 🚫",
"I'm here to assist you with any questions or problems you may have. How can I help you today? 🚀",
"Is there anything specific you'd like to talk about? 💬",
"I'm happy to help with any questions or concerns you may have. Just let me know how I can assist you. 😊",
"I'm here to assist you with any questions or problems you may have. What can I help you with today? 🤗",
"Is there anything specific you'd like to ask or talk about? I'm here to help with any questions or concerns you may have. 💬",
"I'm here to assist you with any questions or problems you may have. How can I help you today? 💡",
];
// Return a random response
return responses[Math.floor(Math.random() * responses.length)];
}