-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchatbot-ui.js
321 lines (253 loc) · 10.5 KB
/
chatbot-ui.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Makes backend API call to rasa chatbot and display output to chatbot frontend
*/
function init() {
//---------------------------- Including Jquery ------------------------------
var script = document.createElement('script');
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
//--------------------------- Important Variables----------------------------
botLogoPath = "./imgs/bot-logo.png"
//--------------------------- Chatbot Frontend -------------------------------
const chatContainer = document.getElementById("chat-container");
template = ` <button class='chat-btn'><img src = "./icons/comment.png" class = "icon" ></button>
<div class='chat-popup'>
<div class='chat-header'>
<div class='chatbot-img'>
<img src='${botLogoPath}' alt='Chat Bot image' class='bot-img'>
</div>
<h3 class='bot-title'>Covid Bot</h3>
<button class = "expand-chat-window" ><img src="./icons/open_fullscreen.png" class="icon" ></button>
</div>
<div class='chat-area'>
<div class='bot-msg'>
<img class='bot-img' src ='${botLogoPath}' />
<span class='msg'>Hi, How can i help you?</span>
</div>
<!-- <div class='bot-msg'>
<img class='bot-img' src ='${botLogoPath}' />
<div class='response-btns'>
<button class='btn-primary' onclick= 'userResponseBtn(this)' value='/sign_in'>sample btn</button>
</div>
</div> -->
<!-- <div class='bot-msg'>
<img class='msg-image' src = "https://i.imgur.com/nGF1K8f.jpg" />
</div> -->
<!-- <div class='user-msg'>
<span class='msg'>Hi, How can i help you?</span>
</div> -->
</div>
<div class='chat-input-area'>
<input type='text' autofocus class='chat-input' onkeypress='return givenUserInput(event)' placeholder='Type a message ...' autocomplete='off'>
<button class='chat-submit'><i class='material-icons'>send</i></button>
</div>
</div>`
chatContainer.innerHTML = template;
//--------------------------- Important Variables----------------------------
var inactiveMessage = "Server is down, Please contact the developer to activate it"
chatPopup = document.querySelector(".chat-popup")
chatBtn = document.querySelector(".chat-btn")
chatSubmit = document.querySelector(".chat-submit")
chatHeader = document.querySelector(".chat-header")
chatArea = document.querySelector(".chat-area")
chatInput = document.querySelector(".chat-input")
expandWindow = document.querySelector(".expand-chat-window")
root = document.documentElement;
chatPopup.style.display = "none"
var host = ""
//------------------------ ChatBot Toggler -------------------------
chatBtn.addEventListener("click", () => {
mobileDevice = !detectMob()
if (chatPopup.style.display == "none" && mobileDevice) {
chatPopup.style.display = "flex"
chatInput.focus();
chatBtn.innerHTML = `<img src = "./icons/close.png" class = "icon" >`
} else if (mobileDevice) {
chatPopup.style.display = "none"
chatBtn.innerHTML = `<img src = "./icons/comment.png" class = "icon" >`
} else {
mobileView()
}
})
chatSubmit.addEventListener("click", () => {
let userResponse = chatInput.value.trim();
if (userResponse !== "") {
setUserResponse();
send(userResponse)
}
})
expandWindow.addEventListener("click", (e) => {
// console.log(expandWindow.innerHTML)
if (expandWindow.innerHTML == '<img src="./icons/open_fullscreen.png" class="icon">') {
expandWindow.innerHTML = `<img src = "./icons/close_fullscreen.png" class = 'icon'>`
root.style.setProperty('--chat-window-height', 80 + "%");
root.style.setProperty('--chat-window-total-width', 85 + "%");
} else if (expandWindow.innerHTML == '<img src="./icons/close.png" class="icon">') {
chatPopup.style.display = "none"
chatBtn.style.display = "block"
} else {
expandWindow.innerHTML = `<img src = "./icons/open_fullscreen.png" class = "icon" >`
root.style.setProperty('--chat-window-height', 500 + "px");
root.style.setProperty('--chat-window-total-width', 380 + "px");
}
})
}
// end of init function
var passwordInput = false;
function userResponseBtn(e) {
send(e.value);
}
// to submit user input when he presses enter
function givenUserInput(e) {
if (e.keyCode == 13) {
let userResponse = chatInput.value.trim();
if (userResponse !== "") {
setUserResponse()
send(userResponse)
}
}
}
// to display user message on UI
function setUserResponse() {
let userInput = chatInput.value;
if (passwordInput) {
userInput = "******"
}
if (userInput) {
let temp = `<div class="user-msg"><span class = "msg">${userInput}</span></div>`
chatArea.innerHTML += temp;
chatInput.value = ""
} else {
chatInput.disabled = false;
}
scrollToBottomOfResults();
}
function scrollToBottomOfResults() {
chatArea.scrollTop = chatArea.scrollHeight;
}
/***************************************************************
Frontend Part Completed
****************************************************************/
// host = 'http://localhost:5005/webhooks/rest/webhook'
function send(message) {
chatInput.type = "text"
passwordInput = false;
chatInput.focus();
console.log("User Message:", message)
$.ajax({
url: host,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
"message": message,
"sender": "User"
}),
success: function (data, textStatus) {
if (data != null) {
setBotResponse(data);
}
console.log("Rasa Response: ", data, "\n Status:", textStatus)
},
error: function (errorMessage) {
setBotResponse("");
console.log('Error' + errorMessage);
}
});
chatInput.focus();
}
//------------------------------------ Set bot response -------------------------------------
function setBotResponse(val) {
setTimeout(function () {
if (val.length < 1) {
//if there is no response from Rasa
// msg = 'I couldn\'t get that. Let\' try something else!';
msg = inactiveMessage;
var BotResponse = `<div class='bot-msg'><img class='bot-img' src ='${botLogoPath}' /><span class='msg'> ${msg} </span></div>`;
$(BotResponse).appendTo('.chat-area').hide().fadeIn(1000);
scrollToBottomOfResults();
chatInput.focus();
} else {
//if we get response from Rasa
for (i = 0; i < val.length; i++) {
//check if there is text message
if (val[i].hasOwnProperty("text")) {
const botMsg = val[i].text;
if (botMsg.includes("password")) {
chatInput.type = "password";
passwordInput = true;
}
var BotResponse = `<div class='bot-msg'><img class='bot-img' src ='${botLogoPath}' /><span class='msg'>${val[i].text}</span></div>`;
$(BotResponse).appendTo('.chat-area').hide().fadeIn(1000);
}
//check if there is image
if (val[i].hasOwnProperty("image")) {
var BotResponse = "<div class='bot-msg'>" + "<img class='bot-img' src ='${botLogoPath}' />"
'<img class="msg-image" src="' + val[i].image + '">' +
'</div>'
$(BotResponse).appendTo('.chat-area').hide().fadeIn(1000);
}
//check if there are buttons
if (val[i].hasOwnProperty("buttons")) {
var BotResponse = `<div class='bot-msg'><img class='bot-img' src ='${botLogoPath}' /><div class='response-btns'>`
buttonsArray = val[i].buttons;
buttonsArray.forEach(btn => {
BotResponse += `<button class='btn-primary' onclick= 'userResponseBtn(this)' value='${btn.payload}'>${btn.title}</button>`
})
BotResponse += "</div></div>"
$(BotResponse).appendTo('.chat-area').hide().fadeIn(1000);
chatInput.disabled = true;
}
}
scrollToBottomOfResults();
chatInput.disabled = false;
chatInput.focus();
}
}, 500);
}
function mobileView() {
$('.chat-popup').width($(window).width());
if (chatPopup.style.display == "none") {
chatPopup.style.display = "flex"
// chatInput.focus();
chatBtn.style.display = "none"
chatPopup.style.bottom = "0"
chatPopup.style.right = "0"
// chatPopup.style.transition = "none"
expandWindow.innerHTML = `<img src = "./icons/close.png" class = "icon" >`
}
}
function detectMob() {
return ((window.innerHeight <= 800) && (window.innerWidth <= 600));
}
function chatbotTheme(theme) {
const gradientHeader = document.querySelector(".chat-header");
const orange = {
color: "#FBAB7E",
background: "linear-gradient(19deg, #FBAB7E 0%, #F7CE68 100%)"
}
const purple = {
color: "#B721FF",
background: "linear-gradient(19deg, #21D4FD 0%, #B721FF 100%)"
}
if (theme === "orange") {
root.style.setProperty('--chat-window-color-theme', orange.color);
gradientHeader.style.backgroundImage = orange.background;
chatSubmit.style.backgroundColor = orange.color;
} else if (theme === "purple") {
root.style.setProperty('--chat-window-color-theme', purple.color);
gradientHeader.style.backgroundImage = purple.background;
chatSubmit.style.backgroundColor = purple.color;
}
}
function createChatBot(hostURL, botLogo, title, welcomeMessage, inactiveMsg, theme = "blue") {
host = hostURL;
botLogoPath = botLogo;
inactiveMessage = inactiveMsg;
init()
const msg = document.querySelector(".msg");
msg.innerText = welcomeMessage;
const botTitle = document.querySelector(".bot-title");
botTitle.innerText = title;
chatbotTheme(theme)
}