-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatBotJs.js
38 lines (30 loc) · 1.3 KB
/
chatBotJs.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
let chatbotMsgList = ["Hi", "Hey", "Good Morning", "Good Evening", "How can I help you?", "Thank You"];
let chatContainerEl = document.getElementById("chatContainer");
let userInputEl = document.getElementById("userInput");
let sendMsgBtnEl = document.getElementById("sendMsgBtn");
function getReplyFromChatbot() {
let noChat = chatbotMsgList.length;
let chatbotMsg = chatbotMsgList[Math.ceil(Math.random() * noChat) - 1];
let msgContainerEl = document.createElement("div");
msgContainerEl.classList.add("msg-from-chatbot-container");
chatContainerEl.appendChild(msgContainerEl);
let chatbotMsgEl = document.createElement("span");
chatbotMsgEl.textContent = chatbotMsg;
chatbotMsgEl.classList.add("msg-to-chatbot");
msgContainerEl.appendChild(chatbotMsgEl);
}
function sendMsgToChatbot() {
let userMsg = userInputEl.value;
let msgContainerEl = document.createElement("div");
msgContainerEl.classList.add("msg-to-chatbot-container");
chatContainerEl.appendChild(msgContainerEl);
let userMsgEl = document.createElement("span");
userMsgEl.textContent = userMsg;
userMsgEl.classList.add("msg-to-chatbot");
msgContainerEl.appendChild(userMsgEl);
userInputEl.value = "";
getReplyFromChatbot();
}
sendMsgBtnEl.onclick = function() {
sendMsgToChatbot();
}