-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
309 lines (252 loc) · 9.12 KB
/
bot.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
// // // variables
// // var input_mode = 'question';
// // var positive_sentiment = 'Thank you for the feedback! We work hard to get the best experience and try to hit the mark for you. Hoping to see you in the upcoming events by MSPC!';
// // var negative_sentiment = 'Kindly receive our apologies for the inconvenience caused. We are constantly making progress to enhance your experience. We appreciate your valuable feedback.';
var feedback_mode = false;
// // // -------
function botCommand(cmd) {
console.log('CMD: ' + cmd.toString());
switch (cmd.toString()) {
case 'help':
botMessage(
'You can ask questions related to the club. We will try our best to answer them :)<br>Following Commands are also available for convenience:<br>@feedback - give feedback<br>@clear - clear current chat<br>@quit - close chatbot'
);
break;
case 'feedback':
botMessage('Your next message will be recorded as feedback.');
feedback_mode = true;
break;
case 'clear':
closeChat();
popupChat();
break;
case 'quit':
closeChat();
break;
default:
botMessage('Invalid Command Attempt. Use @help to see available commands.');
}
}
chat_log = [];
function popupChat() {
// var newz = document.getElementsByClassName('events_btn_cer')[0];
// newz.style.zIndex="1";
var bot_interface = document.getElementsByClassName('bot-interface')[0];
bot_interface.style.display = 'block';
var chatBox = document.getElementById('chat-box');
var chatHolder = document.createElement('div');
chatHolder.id = 'chat-holder';
chatBox.insertBefore(chatHolder, document.getElementsByClassName('user-input-dialogue')[0]);
document.getElementsByClassName('user-input')[0].focus();
botMessage('How may we help you?');
// presentButtonOptions();
}
function closeChat() {
// document.getElementsByClassName('events_btn_cer').style.zIndex = '6';
var bot_interface = document.getElementsByClassName('bot-interface')[0];
var chat_box = document.getElementById('chat-box');
document.getElementById('chat-holder').remove();
bot_interface.style.display = 'none';
}
function storeChat(chat_box) {
var diaList = chat_log;
console.log(diaList);
chat_log = [];
var chat = JSON.stringify({
timestamp: new Date().getTime(),
dialogue: diaList
});
console.log(chat);
var myHeaders = new Headers();
myHeaders.append('Content-Type', 'text/plain');
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: JSON.stringify(chat)
};
// fetch("http://localhost:5000/botadmin/logs/chat", requestOptions)
fetch('https://mlsabot.azurewebsites.net/botadmin/logs/chat', requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log('error', error));
}
function botMessage(msg) {
var chat_box = document.getElementById('chat-holder');
var dialogueContainer = document.createElement('div');
dialogueContainer.id = 'dialogue-container';
var dialogue = document.createElement('p');
dialogue.className = 'dialogue';
var spanT = document.createElement('span');
spanT.id = 'bot-prompt';
var botPrompt = document.createTextNode('/atlas> ');
spanT.appendChild(botPrompt);
dialogue.appendChild(spanT);
var htmlString = '<p>' + msg + '</p>';
var doc = new DOMParser().parseFromString(htmlString, 'text/html');
console.log(doc);
paraSets = doc.getElementsByTagName('p');
console.log(paraSets);
for (i = 0; i < paraSets.length; ++i) {
if (paraSets[i].innerText != '') {
var mainPara = paraSets[i];
}
}
dialogue.appendChild(mainPara);
dialogueContainer.appendChild(dialogue);
chat_box.append(dialogueContainer);
dialogueContainer.scrollIntoView();
document.getElementsByClassName('bounds')[1].scrollIntoView();
chat_log.push({
bot: mainPara.innerText.toString()
});
var links = document.getElementsByTagName('a');
if (links.length) {
for (var i = 0; i < links.length; i++) {
links[i].setAttribute('target', '_blank');
}
}
}
function userMessage(msg) {
var chat_box = document.getElementById('chat-holder');
var dialogueContainer = document.createElement('div');
dialogueContainer.id = 'dialogue-container';
var dialogue = document.createElement('p');
dialogue.className = 'dialogue';
var spanT = document.createElement('span');
spanT.id = 'user-prompt';
var botPrompt = document.createTextNode('/user> ');
spanT.appendChild(botPrompt);
dialogue.appendChild(spanT);
var htmlString = '<p>' + msg + '</p>';
var doc = new DOMParser().parseFromString(htmlString, 'text/html');
console.log(doc);
paraSets = doc.getElementsByTagName('p');
console.log(paraSets);
for (i = 0; i < paraSets.length; ++i) {
if (paraSets[i].innerText != '') {
var mainPara = paraSets[i];
}
}
dialogue.appendChild(mainPara);
dialogueContainer.appendChild(dialogue);
chat_box.appendChild(dialogueContainer);
dialogueContainer.scrollIntoView();
chat_log.push({
user: msg.toString()
});
}
// // function presentButtonOptions () {
// // var inputToBeDisabled = document.getElementsByClassName('msg')[0];
// // inputToBeDisabled.disabled = true;
// // inputToBeDisabled.placeholder = '';
// // var chat_box = document.getElementById('chat-box');
// // var newRow = document.createElement('div');
// // newRow.className = "row justify-content-md";
// // newRow.id = "button-dialogue"
// // var thisButtonOption = document.createElement('div');
// // thisButtonOption.className = 'col-sm-auto p-1 m-1';
// // thisButtonOption.id = 'button-option';
// // var thisButton = document.createElement('button');
// // thisButton.className = 'option-button';
// // var thisOption = document.createTextNode('Question');
// // thisButton.appendChild(thisOption);
// // thisButtonOption.appendChild(thisButton);
// // newRow.appendChild(thisButtonOption);
// // thisButton.onclick = () => {
// // questionMode();
// // }
// // var thisButtonOption = document.createElement('div');
// // thisButtonOption.className = 'col-sm-auto p-1 m-1';
// // thisButtonOption.id = 'button-option';
// // var thisButton = document.createElement('button');
// // thisButton.className = 'option-button';
// // var thisOption = document.createTextNode('Feedback');
// // thisButton.appendChild(thisOption);
// // thisButtonOption.appendChild(thisButton);
// // newRow.appendChild(thisButtonOption);
// // thisButton.onclick = () => {
// // feedbackMode();
// // }
// // chat_box.appendChild(newRow);
// // }
// // function questionMode() {
// // var inputToBeEnabled = document.getElementsByClassName('msg')[0];
// // inputToBeEnabled.disabled = false;
// // inputToBeEnabled.placeholder = 'Type here...';
// // input_mode = 'question';
// // var button_row = document.getElementById('button-dialogue');
// // button_row.remove();
// // userMessage('Question');
// // botMessage("We're glad to help! Ask us anything..");
// // }
// // function feedbackMode() {
// // var inputToBeEnabled = document.getElementsByClassName('msg')[0];
// // inputToBeEnabled.disabled = false;
// // inputToBeEnabled.placeholder = 'Type here...';
// // input_mode = 'feedback';
// // var button_row = document.getElementById('button-dialogue');
// // button_row.remove();
// // userMessage('Feedback');
// // botMessage('We would love to have your views!');
// // }
function askQuestion() {
var inputField = document.getElementsByClassName('user-input')[0];
var prompt = document.getElementsByTagName('form')[0];
prompt.style.display = 'none';
msg = inputField.value;
console.log(msg[0]);
inputField.value = '';
if (msg[0] != '@') {
if (msg.toString() != '') {
if (!feedback_mode) {
payload = {
data: {
question: '"' + msg.toString() + '"',
host: 'mspcbot-faq.azurewebsites.net',
POSTkey: '2be5dde2-29b4-4b60-820a-04a6acf34292',
key: 'EndpointKey d984024e-ca78-4394-8b1c-d96907585545'
}
};
var myHeaders = new Headers();
myHeaders.append('Content-Type', 'text/plain');
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: JSON.stringify(payload)
};
userMessage(msg);
// fetch("http://localhost:5000/botservice/question", requestOptions)
fetch('https://mlsabot.azurewebsites.net/botservice/question', requestOptions)
.then((response) => response.json())
.then((result) => {
var md = new Remarkable();
text = md.render(result['reply']);
botMessage(text);
// inputField.style.display = "inline";
prompt.style.display = 'inline';
inputField.focus();
})
.catch((error) => {
console.log('error', error);
botMessage("I couldn't quite understand that.");
prompt.style.display = 'inline';
inputField.focus();
});
}
if (feedback_mode) {
userMessage(msg);
storeChat();
input_mode = 'question';
botMessage('Thank you for your feedback! Need help with something else?');
prompt.style.display = 'inline';
inputField.focus();
feedback_mode = false;
}
}
} else if (msg[0] == '@') {
userMessage(msg);
botCommand(msg.slice(1, msg.length));
prompt.style.display = 'inline';
inputField.focus();
}
}