-
Notifications
You must be signed in to change notification settings - Fork 11
/
webchat_io.js
323 lines (249 loc) · 11.5 KB
/
webchat_io.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
/* WEBCHAT HEARS */
var botVars = require("../bot.js");
var spellCheckerMiddleware = botVars.spellCheckerMiddleware;
var botFunctions = require("../doremus/bot_functions.js");
var log = botVars.log;
// WORKS-BY INTENT
module.exports.worksBy = botVars.webchatController.hears(['works-by'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
if (message.text !== message.old) {
var understoodText = (spellCheckerMiddleware.currentLang() === "en") ? "I understood: " : "J'ai compris: ";
bot.reply(message, {
"text": understoodText + message["text"],
"color": "warning"
});
}
// GET PARAMETERS
var parameters = {
artist: message.entities["doremus-artist"],
prevArtist: message.entities["doremus-artist-prev"],
number: message.entities["number"],
instruments: message.entities["doremus-instrument"],
strictly: message.entities["doremus-strictly"],
year: message.entities["date-period"],
genre: message.entities["doremus-genre"]
}
// COUNT OF THE FILTER SET BY THE USER
var filterCounter = 0;
for (var key in parameters) {
if (typeof parameters[key] === "string" && parameters[key] !== "") {
filterCounter++;
} else if (typeof parameters[key] !== "string" && parameters[key].length != 0) {
filterCounter++;
}
}
// CHECK IF THE MAX AMOUNT OF FILTERS IS APPLIED
if (filterCounter > 2) {
// YEAR CHECK AND PARSING
var startyear = null;
var endyear = null;
if (parameters.year !== "") {
startyear = parseInt(parameters.year.split("/")[0]);
endyear = parseInt(parameters.year.split("/")[1]);
// SWAP IF PROVIDED IN THE INVERSE ORDER
if (startyear > endyear) {
var tmp = startyear;
startyear = endyear;
endyear = tmp;
}
}
// ARTIST PARSING
if (parameters.artist == "" && parameters.prevArtist !== "") {
parameters.artist = parameters.prevArtist;
}
// DO THE QUERY (WITH ALL THE INFOS)
botFunctions.doQuery(parameters.artist, parameters.number, parameters.instruments,
parameters.strictly, startyear, endyear, parameters.genre, "webchat", bot, message);
log.write(bot.type, message.user, message.channel, "works-by",
"<result_card>", '"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
} else {
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "works-by",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"', '"' + message.old + '"',
message.lang, message.confidence);
}
});
// WORKS-BY - YES INTENT
module.exports.worksByYes = botVars.webchatController.hears(['works-by - yes'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "works-by - yes",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"', '"' + message.old + '"',
message.lang, message.confidence);
});
// WORKS-BY - NO
module.exports.worksByNo = botVars.webchatController.hears(['works-by - no'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
var context = message["nlpResponse"]["result"]["contexts"][0];
// GET PARAMETERS
var artist = context["parameters"]["doremus-artist"];
var prevArtist = context["parameters"]["doremus-artist-prev"];
var number = context["parameters"]["number"];
var instruments = context["parameters"]["doremus-instrument"];
var strictly = context["parameters"]["doremus-strictly"];
var year = context["parameters"]["date-period"];
var genre = context["parameters"]["doremus-genre"];
// YEAR CHECK AND PARSING
var startyear = null;
var endyear = null;
if (year !== "") {
startyear = parseInt(year.split("/")[0]);
endyear = parseInt(year.split("/")[1]);
// SWAP IF PROVIDED IN THE INVERSE ORDER
if (startyear > endyear) {
var tmp = startyear;
startyear = endyear;
endyear = tmp;
}
}
// ARTIST PARSING
if (artist === "" && prevArtist !== "") {
artist = prevArtist;
}
// DO THE QUERY (WITH ALL THE INFOS)
botFunctions.doQuery(artist, number, instruments, strictly, startyear, endyear, genre, "webchat", bot, message);
log.write(bot.type, message.user, message.channel, "works-by - no",
"<result_card>", '"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
});
// WORKS-BY-SOMETHING INTENT
module.exports.worksBySomething = botVars.webchatController.hears(['works-by-artist', 'works-by-instrument', 'works-by-genre', 'works-by-years'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "works-by-something",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"', '"' + message.old + '"',
message.lang, message.confidence);
});
// DISCOVER ARTIST
module.exports.findArtist = botVars.webchatController.hears(['find-artist'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
if (message.text !== message.old) {
var understoodText = (spellCheckerMiddleware.currentLang() === "en") ? "I understood: " : "J'ai compris: ";
bot.reply(message, {
"text": understoodText + message["text"],
"color": "warning"
});
}
// GET ENTITIES
var date = message.entities["date-period"];
var number = message.entities["number"];
var place = message.entities["geo-city"];
var instrument = message.entities["doremus-instrument"];
var genre = message.entities["doremus-genre"];
// PARSE ENTITIES
var startdate = "";
var enddate = "";
if (date !== "") {
startdate = date.split("/")[0];
enddate = date.split("/")[1];
}
var num = 1;
if (number !== "") {
num = parseInt(number);
}
var city = "";
if (place !== "") {
city = place.toLowerCase();
}
// SEND THE BIO TO THE USER
botFunctions.doQueryFindArtist(num, startdate, enddate, city, instrument, genre, "webchat", bot, message);
log.write(bot.type, message.user, message.channel, "find-artist",
"<result_card>", '"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
});
// DISCOVER ARTIST
module.exports.discoverArtist = botVars.webchatController.hears(['discover-artist'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
if (message.text !== message.old) {
var understoodText = (spellCheckerMiddleware.currentLang() === "en") ? "I understood: " : "J'ai compris: ";
bot.reply(message, {
"text": understoodText + message["text"],
"color": "warning"
});
}
// ACTION COMPLETE (we have all the required infos)
if (message['nlpResponse']['result']['actionIncomplete'] == false) {
// SEND THE BIO TO THE USER
botFunctions.answerBio(message.entities["doremus-artist"], "webchat", bot, message);
log.write(bot.type, message.user, message.channel, "discover-artist",
"<result_card>", '"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
}
// ACTION INCOMPLETE (the artist names hasn't been provided or it was misspelled)
else {
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "discover-artist",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"', '"' + message.old + '"',
message.lang, message.confidence);
}
});
// FIND-PERFORMANCE
module.exports.findPerformance = botVars.webchatController.hears(['find-performance'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
if (message.text !== message.old) {
var understoodText = (spellCheckerMiddleware.currentLang() === "en") ? "I understood: " : "J'ai compris: ";
bot.reply(message, {
"text": understoodText + message["text"],
"color": "warning"
});
}
// ACTION COMPLETE (the date has been provided)
if (message['nlpResponse']['result']['actionIncomplete'] == false) {
var date = message.entities["date-period"];
var place = message.entities["geo-city"];
var number = message.entities["number"];
var city = "";
if (place !== "") {
city = place.toLowerCase();
}
var num = 1;
if (number !== "") {
num = parseInt(number);
}
var startdate = date.split("/")[0];
var enddate = date.split("/")[1];
// DO THE QUERY (WITH ALL THE INFOS)
botFunctions.doQueryPerformance(num, city, startdate, enddate, "webchat", bot, message);
log.write(bot.type, message.user, message.channel, "find-performance",
"<result_card>", '"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
}
// ACTION INCOMPLETE (missing date)
else {
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "find-performance",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"', '"' + message.old + '"',
message.lang, message.confidence);
}
});
// HELLO INTENT
module.exports.hello = botVars.webchatController.hears(['hello'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
// Clear the context
botFunctions.sendClearContext(message.nlpResponse.sessionId);
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "hello",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
});
// HELP INTENT
module.exports.help = botVars.webchatController.hears(['help'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "help",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
});
// RESET INTENT
module.exports.reset = botVars.webchatController.hears(['reset'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
// Clear the context
botFunctions.sendClearContext(message.nlpResponse.sessionId);
bot.reply(message, message['fulfillment']['speech']);
log.write(bot.type, message.user, message.channel, "reset",
'"' + message['fulfillment']['speech'] + '"',
'"' + message.text + '"',
'"' + message.old + '"', message.lang, message.confidence);
});
// DEFAULT INTENT
module.exports.default = botVars.webchatController.hears(['Default Fallback Intent'], 'message_received', botVars.dialogflowMiddleware.hears, function(bot, message) {
bot.reply(message, message['fulfillment']['speech']);
});