-
Notifications
You must be signed in to change notification settings - Fork 0
/
lionbotchatbot.js
736 lines (706 loc) · 18.4 KB
/
lionbotchatbot.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
var html_to_append = `
<div style="all:unset;">
<style scoped>
@import "https://fonts.googleapis.com/css?family=Noto+Sans";
* {
box-sizing: border-box;
}
.bubble {
height: 60px;
position: fixed;
bottom: 150px;
left: 20px;
width: 200px;
color: rgb(13, 29, 98);
margin: 10px;
line-height: 60px;
font-size: 15px;
cursor: pointer;
font-family: "Open Sans";
text-align: center;
background: rgb(234, 236, 237);
border-radius: 15px 15px 15px 0px;
}
.floating-chat {
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: white;
position: fixed;
bottom: 50px;
left: 20px;
width: 80px;
height: 80px;
transition: all 250ms ease-out;
border-radius: 50%;
opacity: 0;
background: #5a5eb9;
padding: 28px;
cursor: pointer;
box-shadow: 0px 3px 16px 0px rgba(0, 0, 0, 0.6),
0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
}
.floating-chat.enter {
transform: translateY(0);
opacity: 1;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.12), 0px 1px 2px rgba(0, 0, 0, 0.14);
}
.floating-chat.expand {
width: 365px;
max-height: 70%;
height: 500px;
border-radius: 5px;
cursor: auto;
opacity: 1;
background: #efefef;
box-shadow: 0px 5px 35px 9px #ccc;
}
@media only screen and (max-width: 768px) {
.floating-chat.expand {
width: 80%;
max-height: 60%;
height: 60%;
border-radius: 5px;
cursor: auto;
opacity: 1;
background: #efefef;
box-shadow: 0px 5px 35px 9px #ccc;
}
}
.floating-chat :focus {
outline: 0;
box-shadow: #5a5eb9;
}
.floating-chat button {
background: transparent;
border: 0;
color: white;
text-transform: uppercase;
border-radius: 3px;
cursor: pointer;
padding:0;
margin:0;
}
#stylebg {
opacity: 1;
}
.floating-chat .chat {
display: flex;
flex-direction: column;
position: absolute;
opacity: 0;
width: 1px;
height: 1px;
border-radius: 50%;
transition: all 250ms ease-out;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.floating-chat .chat.enter {
opacity: 1;
border-radius: 0;
width: auto;
height: auto;
}
.floating-chat .chat .header {
flex-shrink: 0;
padding-bottom: 10px;
display: flex;
background: transparent;
}
.floating-chat .chat .header .title {
flex-grow: 1;
flex-shrink: 1;
padding: 0 5px;
}
.floating-chat .chat .header button {
flex-shrink: 0;
}
.floating-chat .chat .messages {
padding: 10px;
margin: 0;
list-style: none;
overflow-y: scroll;
overflow-x: hidden;
flex-grow: 1;
border-radius: 4px;
background: transparent;
}
.floating-chat .chat .messages::-webkit-scrollbar {
width: 5px;
}
.floating-chat .chat .messages::-webkit-scrollbar-track {
border-radius: 5px;
background-color: #f5f5f5;
}
.floating-chat .chat .messages::-webkit-scrollbar-thumb {
border-radius: 5px;
background-color: #f5f5f5;
}
.floating-chat .chat .messages li {
position: relative;
clear: both;
display: inline-block;
padding: 14px;
margin: 0 0 20px 0;
font: 12px/16px "Noto Sans", sans-serif;
border-radius: 10px;
word-wrap: break-word;
max-width: 81%;
}
.floating-chat .chat .messages li:before {
position: absolute;
top: 0;
width: 35px;
height: 35px;
border-radius: 35px;
content: "";
background-size: cover;
}
.floating-chat .chat .messages li:after {
position: absolute;
top: 10px;
content: "";
width: 0;
height: 0;
border-top: 10px solid rgba(25, 147, 147, 0.2);
}
.floating-chat .chat .messages li.other {
animation: show-chat-odd 0.15s 1 ease-in;
-moz-animation: show-chat-odd 0.15s 1 ease-in;
-webkit-animation: show-chat-odd 0.15s 1 ease-in;
float: right;
margin-right: 45px;
background-color: white;
color: black;
}
.floating-chat .chat .messages li.other:before {
right: -45px;
background-image: url(https://www.seekpng.com/png/detail/110-1100707_person-avatar-placeholder.png);
}
.floating-chat .chat .messages li.other:after {
right: -10px;
}
.floating-chat .chat .messages li.self {
animation: show-chat-even 0.15s 1 ease-in;
-moz-animation: show-chat-even 0.15s 1 ease-in;
-webkit-animation: show-chat-even 0.15s 1 ease-in;
float: left;
margin-left: 45px;
background-color: #5a5eb9;
color: white;
}
.floating-chat .chat .messages li.self:before {
left: -45px;
background-image: url(https://ayush4921.github.io/ai_chat_bot_js/lb_logo.png);
}
.floating-chat .chat .messages li.self:after {
left: -10px;
}
.floating-chat .chat .footer {
flex-shrink: 0;
display: flex;
padding-top: 10px;
max-height: 90px;
background: transparent;
}
z @keyframes show-chat-even {
0% {
margin-left: -480px;
}
100% {
margin-left: 0;
}
}
@-moz-keyframes show-chat-even {
0% {
margin-left: -480px;
}
100% {
margin-left: 0;
}
}
@-webkit-keyframes show-chat-even {
0% {
margin-left: -480px;
}
100% {
margin-left: 0;
}
}
@keyframes show-chat-odd {
0% {
margin-right: -480px;
}
100% {
margin-right: 0;
}
}
@-moz-keyframes show-chat-odd {
0% {
margin-right: -480px;
}
100% {
margin-right: 0;
}
}
@-webkit-keyframes show-chat-odd {
0% {
margin-right: -480px;
}
100% {
margin-right: 0;
}
}
.chat-box-header {
background: #5a5eb9;
height: 70px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
color: white;
text-align: center;
font-size: 20px;
padding-top: 17px;
}
.material-icons{
vertical-align: middle;
line-height: 35px;
}
#chat-input {
background: #f4f7f9;
width: 100%;
position: relative;
height: 47px;
padding-top: 10px;
padding-right: 50px;
padding-bottom: 10px;
padding-left: 15px;
border: none;
resize: none;
outline: none;
border: 1px solid #ccc;
color: #888;
border-top: none;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
overflow: hidden;
}
.chat-input > div {
margin-bottom: 0;
}
#chat-input::-webkit-input-placeholder {
/* Chrome/Opera/Safari */
color: #ccc;
}
#chat-input::-moz-placeholder {
/* Firefox 19+ */
color: #ccc;
}
#chat-input:-ms-input-placeholder {
/* IE 10+ */
color: #ccc;
}
#chat-input:-moz-placeholder {
/* Firefox 18- */
color: #ccc;
}
.chat-submit {
position: absolute;
bottom: 3px;
right: 10px;
background: transparent;
box-shadow: none;
border: none;
border-radius: 50%;
color: #5a5eb9;
height: 35px;
line-height: 35px;
text-align: center;
}
.chat-box-toggle {
float: right;
margin-right: 15px;
cursor: pointer;
}
</style>
<div class="bubble">
<p>Hello! How can I help you?</p>
</div>
<div class="floating-chat">
<img class="avatar" src="https://og-blog-css.outgrow.co/blog/wp-content/uploads/2019/01/robo_small.gif?x65579" style="max-width:120px;max-height:120px;">
<div class="chat">
<div class="chat-box-header">
Ask LB Virtual Assistant
<span class="chat-box-toggle"><button>
<i class="fa fa-times" aria-hidden="true"></i>
</button> </span>
</div>
<ul class="messages">
<li class="self">Hi! How can I help you?</li>
</ul>
<div class="chat-input">
<div placeholder="Send a message..." class="text-box" contenteditable="true" disabled="true" id="chat-input" placeholder="Send a message..."></div>
<button id="sendMessage" type="submit" class="chat-submit"><i class="material-icons" style="color: #5a5eb9;">send</i></button>
</div>
</div>
</div>
</div>
`;
var styles_to_append = `
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-material-design/4.0.2/bootstrap-material-design.css'>
<link rel='stylesheet' href='https://fonts.googleapis.com/icon?family=Material+Icons'>
`;
style_tab = document.getElementsByTagName("head")[0];
style_tab.insertAdjacentHTML("beforeend", styles_to_append);
body_tag = document.getElementsByTagName("body")[0];
body_tag.insertAdjacentHTML("beforeend", html_to_append);
var element = $(".floating-chat");
var bubble = $(".bubble");
bubble.hide();
var myStorage = localStorage;
if (!myStorage.getItem("chatID")) {
myStorage.setItem("chatID", createUUID());
}
setTimeout(function () {
element.addClass("enter");
}, 1000);
var url =
window.location != window.parent.location
? document.referrer
: document.location.href;
element.click(openElement);
function openElement() {
var messages = element.find(".messages");
var textInput = element.find(".text-box");
element.find(".avatar").hide();
bubble.hide();
element.addClass("expand");
element.find(".chat").addClass("enter");
var strLength = textInput.val().length * 2;
textInput.keydown(onMetaAndEnter).prop("disabled", false).focus();
element.off("click", openElement);
element.find(".chat-box-header button").click(closeElement);
element.find("#sendMessage").click(sendNewMessage);
messages.scrollTop(messages.prop("scrollHeight"));
}
function closeElement() {
element.find(".chat").removeClass("enter").hide();
element.find(".avatar").show();
element.removeClass("expand");
element.find(".header button").off("click", closeElement);
element.find("#sendMessage").off("click", sendNewMessage);
element
.find(".text-box")
.off("keydown", onMetaAndEnter)
.prop("disabled", true)
.blur();
setTimeout(function () {
element.find(".chat").removeClass("enter").show();
element.click(openElement);
}, 500);
}
if (
url == "https://www.lionsbefrienders.org.sg/" &&
!/Android|webOS|iPhone|iPad|Mac|Macintosh|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
)
) {
bubble.show();
}
function createUUID() {
// http://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";
var uuid = s.join("");
return uuid;
}
function sendNewMessage() {
var userInput = $(".text-box");
var newMessage = userInput
.html()
.replace(/\<div\>|\<br.*?\>/gi, "\n")
.replace(/\<\/div\>/g, "")
.trim()
.replace(/\n/g, "<br>");
var messagesContainer = $(".messages");
messagesContainer.append(
['<li class="other">', newMessage, "</li>"].join("")
);
if (!newMessage) return;
if (newMessage == "q1" || newMessage == "Q1" || newMessage == "1") {
send_message_for_question(messagesContainer, userInput, 0);
return;
}
if (newMessage == "q2" || newMessage == "Q2" || newMessage == "2") {
send_message_for_question(messagesContainer, userInput, 1);
return;
}
window.final_response = getResponse(newMessage);
if (
window.final_response[0]["question"] != "hi there" &&
window.final_response.length > 1
) {
messagesContainer.append(
[
'<li class="self"> Q1) ',
final_response[0]["question"],
"<br>",
"<br>",
"Q2) ",
final_response[1]["question"],
"<br>",
"</li>",
].join("")
);
clear_input(userInput, messagesContainer);
messagesContainer.append(
[
'<li class="self" id="questionare"> <span id="answer1" onclick="return_answer(0)">Q1</span> <span id="answer2" onclick="return_answer(1)">Q2</span> </li>',
].join("")
);
} else {
messagesContainer.append(
['<li class="self">', final_response[0]["response"], "</li>"].join("")
);
}
clear_input(userInput, messagesContainer);
}
function send_message_for_question(messagesContainer, userInput, question) {
messagesContainer.append(
[
'<li class="self">',
window.final_response[question]["response"],
"</li>",
].join("")
);
clear_input(userInput, messagesContainer);
window.final_response = "";
}
function return_answer(question) {
var userInput = $(".text-box");
var messagesContainer = $(".messages");
if (window.final_response != "") {
messagesContainer.append(
['<li class="other">Q', question + 1, "</li>"].join("")
);
clear_input(userInput, messagesContainer);
send_message_for_question(messagesContainer, userInput, question);
document.getElementById("questionare").style.display = "none";
}
}
function clear_input(userInput, messagesContainer) {
userInput.html("");
// focus on input
userInput.focus();
messagesContainer.finish().animate(
{
scrollTop: messagesContainer.prop("scrollHeight"),
},
250
);
}
function onMetaAndEnter(event) {
if ((event.metaKey || event.ctrlKey) && event.keyCode == 13) {
sendNewMessage();
}
}
function getResponse(word) {
list_of_responses = [
{
tag: "greetings",
patterns: [
"hi there",
"hello",
"haroo",
"yaw",
"wassup",
"hi",
"hey",
"holla",
"hello",
],
responses: [
"Hello thanks for checking in",
"hi there, how can i help you",
],
context: [""],
},
{
tag: "volunteer",
patterns: [
"How do I become a volunteer?",
"how to volunteer",
"volunteer",
"how I volunteer",
"become a volunteer",
"volunteering how to",
],
responses: [
'Visit <a target="_parent" href="https://www.lionsbefrienders.org.sg/volunteer-programmes/" style="color:white;text-decoration: underline;text-decoration-color: white;">Link</a> to find out ',
],
context: [""],
},
{
tag: "refer",
patterns: [
"How do I refer a senior?",
"senior refer",
"refer",
"referral",
"refer a senior",
"how to refer",
],
responses: [
"Since 3 May 2021, MOH launched a new Elder Care model, where all befriending referrals have been centralized and now need to be referred via the IRMS system. Hence, LB no longer take in direct referrals and only receive referrals via the system. If you don’t have an IRMS account, you may sign up at https://app.aic.sg/eReg/.",
],
context: [""],
},
{
tag: "Senior Activity Centre",
patterns: [
"How can I join your Senior Activity Centre?",
"senior Activity center",
"join senior activity center",
"senior activity",
"senior activity center joining",
],
responses: [
'Visit <a target="_parent" href="https://www.lionsbefrienders.org.sg/contact-us/" style="color:white;text-decoration: underline;text-decoration-color: white;">Link</a> to find the nearest centre to your place and dial in to the specific centre to enquire.',
],
context: [""],
},
{
tag: "Miscellineous",
patterns: ["How can I meet the team", "meet the team", "How I meet team"],
responses: ["Meeting the team is currently not supported"],
context: [""],
},
{
tag: "ad-hoc volunteering",
patterns: [
"how to do ad-hoc volunteering",
"how I give daily necessities to seniors.",
"volunteering give out to seniors",
"adhoc",
"ad-hoc volunteering",
"volunteer ad-hoc",
],
responses: [
"Please send your message enquiry to [email protected].",
],
context: [""],
},
{
tag: "mass event",
patterns: [
"how do I collaborate with LB on a mass event",
"My company wish to collaborate with LB on a mass event",
"want to collaborate on mass event",
"collaborate",
"mass event organisation",
"mass event",
"even organize with lion group",
],
responses: ["Please send your message enquiry to [email protected]."],
context: [""],
},
{
tag: "donate",
patterns: [
"how do I donate to LB",
"donate to lion befrienders",
"How can I donate?",
"donate",
"how to donate",
"donate to lionsbefrienders",
"donate now",
],
responses: [
'Visit <a target="_parent" href="https://www.lionsbefrienders.org.sg/donate/" style="color:white;text-decoration: underline;text-decoration-color: white;">Link</a> to support us.',
],
context: [""],
},
{
tag: "partner",
patterns: [
"I wish to partner LB on a CSR project. How do I go about that?",
"partner on CSR project",
"partner with LB on CSR project",
"partner",
"CSR",
"CSR project partner",
"I want to do CSR",
],
responses: [
'Visit <a target="_parent" href="https://www.lionsbefrienders.org.sg/partnerships/" style="color:white;text-decoration: underline;text-decoration-color: white;">Link</a> for more info.',
],
context: [""],
},
{
tag: "help",
patterns: ["I want to help", "how do I help?", "help how to", "help"],
responses: ["Is there something that I can help you with?"],
context: [""],
},
{
tag: "work",
patterns: [
"I want to work at lion befrienders",
"how do I work here?",
"career at lion befrienders",
"I want to work for you",
"work at lion befrienders",
"work now at LB",
"work at now at lion befrienders",
"career at LB",
"careers",
"job posting",
"where can I find job posting",
"get a job posting at lb",
],
responses: [
'Visit <a target="_parent" href="https://www.lionsbefrienders.org.sg/careers/" style="color:white;text-decoration: underline;text-decoration-color: white;">Link</a> for the available job opportunities.',
],
context: [""],
},
];
const options = {
includeScore: true,
keys: ["patterns", "tag"],
};
const fuse = new Fuse(list_of_responses, options);
const result = fuse.search(word);
let dict_to_return = [];
if (result.length == 1) {
dict_to_return = [
{
question: result[0]["item"]["patterns"][0],
response: result[0]["item"]["responses"][0],
},
];
} else {
dict_to_return = [
{
question: result[0]["item"]["patterns"][0],
response: result[0]["item"]["responses"][0],
},
{
question: result[1]["item"]["patterns"][0],
response: result[1]["item"]["responses"][0],
},
];
}
return dict_to_return;
}