-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
234 lines (200 loc) · 6.08 KB
/
index.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
// CHANGE IP AND PORT HERE:
const IP = "localhost"
const PORT = "5005"
//------------------------
var socket = io(`http://${IP}:${PORT}`, { path: "/furhat" });
var form = document.getElementById('form');
var input = document.getElementById('input');
var images = document.getElementById('images');
var chatwindow = document.getElementsByClassName("chatwindow"[0]);
images.style = "list-style-type: none";
var chatlist = document.getElementById('messages');
form.addEventListener('submit', function (e) {
e.preventDefault();
if (input.value) {
var messageContent = input.value;
socket.emit('user_uttered', { "message": messageContent });
input.value = '';
}
});
socket.on('reset_frontend', function (args) {
location.reload()
})
socket.on('user_uttered', function (args) {
var messageContent = args.text
if (messageContent) {
addMessageToChatwindow(messageContent, "usermessage")
}
})
socket.on('bot_uttered', function (args) {
console.log(args)
if (isText(args)) {
try {
handleText(args); //Will not fail because it is checked already.
} catch (err) {
console.log("An error has occured: " + err);
}
}
if (isImage(args)) {
try {
clearAllImages();
handleImage(args.attachment.payload.src, images);
} catch (err) {
console.log("An error has occured: " + err);
}
}
if (isIFrame(args)) {
try {
clearAllImages();
handleIFrame(args.attachment.payload.src, images);
} catch (err) {
console.log("An error has occured: " + err);
}
}
if (isCarousel(args)) {
try {
clearAllImages();
handleCarousels(args);
} catch (err) {
console.log("An error has occured: " + err);
}
}
});
//Autoscroll runs every half-second
autoScrollLoop();
function addMessageToChatwindow(messageContent, messageID) {
clearOldMessages();
addMessageToMessages(messageContent, messageID);
autoScroll();
}
function addMessageToMessages(messageContent, messageID) {
var messageContainer = document.createElement("div");
var messageBubble = document.createElement("div");
var messageText = document.createElement("li");
messageContainer.id = "messageContainer";
messageBubble.id = messageID
messageText.id = "messageText";
messageText.textContent = messageContent;
messageBubble.appendChild(messageText);
messageContainer.appendChild(messageBubble);
chatlist.appendChild(messageContainer);
}
function clearOldMessages() {
const maxMessages = 12;
var chatMessages = chatlist.childNodes;
if (chatMessages.length > maxMessages) {
chatlist.removeChild(chatMessages[0]);
}
}
function clearAllImages() {
while (images.firstChild) {
images.removeChild(images.firstChild)
}
}
function autoScroll() {
chatlist.scrollTop = chatlist.scrollHeight;
images.scrollTop = images.scrollHeight;
}
function autoScrollLoop() {
autoScroll();
setTimeout(autoScrollLoop, 500);
}
function handleImage(args, docLocation, imgid) {
var img = document.createElement('img');
img.src = args;
img.id = imgid;
img.style = "width: 100%";
if (docLocation.id == "card") {
docLocation.appendChild(img);
images.appendChild(docLocation);
}
var item = document.createElement('li');
item.appendChild(img);
images.appendChild(item);
}
function handleIFrame(args, docLocation, imgid) {
var iframe = document.createElement('iframe');
iframe.src = args;
iframe.id = imgid;
iframe.height = 600;
iframe.width = 400;
if (docLocation.id == "card") {
docLocation.appendChild(iframe);
images.appendChild(docLocation);
}
var item = document.createElement('li');
item.appendChild(iframe);
images.appendChild(item);
}
function handleText(args) {
//console.log("this is args handletext",args)
var messageContent = args.text;
addMessageToChatwindow(messageContent, 'botmessage')
}
function handleCarousels(args) {
var elements = args.attachment.payload.elements;
console.log(elements);
for (let i = 0; i < elements.length; i++) {
var card_info = elements[i];
var card = document.createElement("div");
card.id = "card";
var title = document.createElement("div");
title.textContent = card_info['title'];
title.id = "title";
card.appendChild(title);
if (args.attachment.payload.elements[i].subtitle) {
var subtitle = document.createElement("div");
subtitle.textContent = card_info['subtitle'];
subtitle.id = "subtitle";
card.appendChild(subtitle);
}
if (card_info['image_url']) {
try {
handleImage(card_info['image_url'], card, "image_url");
} catch (err) {
console.log("An error has occured: " + err);
}
}
if (card_info['iframe_url']) {
try {
handleIFrame(card_info['iframe_url'], card, "iframe_url");
} catch (err) {
console.log("An error has occured: " + err);
}
}
}
}
// check if is text message
function isText(message) {
return Object.keys(message).includes('text');
}
//check if is image
function isImage(message) {
return Object.keys(message).includes('attachment')
&& Object.keys(message.attachment).includes('type')
&& message.attachment.type === 'image';
}
//check if is iFrame
function isIFrame(message) {
return Object.keys(message).includes('attachment')
&& Object.keys(message.attachment).includes('type')
&& message.attachment.type === 'iframe';
}
//check if is button
function isButton(message) {
return Object.keys(message).length === 2
&& Object.keys(message).includes('text')
&& (Object.keys(message).includes('quick_replies') ||
Object.keys(message).includes('buttons'));
}
//check if is carousel
function isCarousel(message) {
return Object.keys(message).includes('attachment')
&& Object.keys(message.attachment).includes('type')
&& message.attachment.type === 'template'
&& Object.keys(message.attachment).includes('payload')
&& Object.keys(message.attachment.payload).indexOf('template_type') >= 0
&& message.attachment.payload.template_type === 'generic'
&& Object.keys(message.attachment.payload).indexOf('elements') >= 0
&& message.attachment.payload.elements.length > 0;
}