-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
567 lines (474 loc) · 17.7 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
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
const http = require('http');
// const https = require('https');
const Bot = require('messenger-bot');
const request = require('request');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const limdu = require('limdu');
const assert = require('assert');
const express = require('express');
const fs = require('fs');
const _ = require("underscore");
const AlchemyLanguageV1 = require('watson-developer-cloud/alchemy-language/v1');
var alchemy_language = new AlchemyLanguageV1({
api_key: '1889a7223446a6db5776d1e94a3887409ddbc319'
});
/*
* Machine learning stuffs here!
*/
// First, define our base classifier type (a multi-label classifier based on winnow):
var TextClassifier = limdu.classifiers.multilabel.BinaryRelevance.bind(0, {
binaryClassifierType: limdu.classifiers.Winnow.bind(0, {retrain_count: 10})
});
// Now define our feature extractor - a function that takes a sample and adds features to a given features set:
var WordExtractor = function(input, features) {
input.split(" ").forEach(function(word) {
features[word]=1;
});
};
// Initialize a classifier with the base classifier type and the feature extractor:
var intentClassifier = new limdu.classifiers.EnhancedClassifier({
classifierType: TextClassifier,
normalizer: limdu.features.LowerCaseNormalizer,
featureExtractor: WordExtractor
});
// Train and test:
intentClassifier.trainBatch([
{input: "Help i'm lost", output: "need_directions"},
{input: "I'm lost", output: "need_directions"},
{input: "Help me, i'm lost", output: "need_directions"},
{input: "I don't know where to go", output: "need_directions"},
{input: "Hey i'm lost", output: "need_directions"},
{input: "Can i have directions", output: "need_directions"},
{input: "I'm not sure where to go", output: "need_directions"},
{input: "I'm unsure where to go", output: "need_directions"},
{input: "I'm checking my flight", output: "check_flight"},
{input: "I need to check about my flight", output: "check_flight"},
{input: "Do you think you can help me check my flight?", output: "check_flight"},
{input: "What's my flight", output: "check_flight"},
{input: "Can you help me check flight details?", output: "check_flight"},
{input: "Can i check my flight details?", output: "check_flight"},
{input: "Can i check my flight details", output: "check_flight"},
{input: "Can i check my flight timing?", output: "check_flight"},
{input: "Can i check my flight timing", output: "check_flight"},
{input: "Can i check my boarding gate?", output: "check_flight"},
{input: "Can i check my boarding gate", output: "check_flight"},
{input: "Can i check my boarding time?", output: "check_flight"},
{input: "Can i check my boarding time", output: "check_flight"},
{input: "Do you think you can remind me of my flight?", output: "remind_me"},
{input: "Could you remind me of my flight?", output: "remind_me"},
{input: "Could you remind me later?", output: "remind_me"},
{input: "Can you remind me later?", output: "remind_me"},
{input: "Could you notify me later?", output: "remind_me"},
{input: "Could you notify me of my flight later?", output: "remind_me"},
{input: "Can you notify me of my flight later?", output: "remind_me"},
{input: "Please remind me", output: "remind_me"},
{input: "Do remind me later!", output: "remind_me"},
{input: "Do notify me later!", output: "remind_me"},
{input: "Can i check the weather for my flight?", output: "check_weather"},
{input: "What's the weather like?", output: "check_weather"},
{input: "Do you think i could check the weather for my flight?", output: "check_weather"},
{input: "Can i check the weather?", output: "check_weather"},
{input: "Hey what's the weather like?", output: "check_weather"},
{input: "What's the weather at", output: "check_weather"},
{input: "What's the weather like later?", output: "check_weather"},
{input: "No", output: "negative_response"},
{input: "Dude", output: "negative_response"},
{input: "Go to hell", output: "negative_response"},
{input: "Screw off", output: "negative_response"},
{input: "How about no", output: "negative_response"},
{input: "Thanks so much!", output: "positive_response"},
{input: "Thanks for the help!", output: "positive_response"},
{input: "Appreciate it!", output: "positive_response"},
{input: "Thank you", output: "positive_response"},
{input: "Hey there", output: "greetings"},
{input: "Hi", output: "greetings"},
{input: "Nice to meet you", output: "greetings"},
{input: "I need help", output: "greetings"},
{input: "Hello", output: "greetings"},
]);
// Here's how to use our test data
// intentClassifier.classify("I want an apple and a banana") // ['apl','bnn']
/*
* END machine learning stuffs here!
*/
var url = 'mongodb://localhost:27017/sia';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server.");
db.close();
});
var bot = new Bot({
token: 'EAAaKuvOeZCMsBAB3tVM1nmOyuzOvHKsM88c7mlob0K8WZBuAyVi7Hz5LRHZCZBH9r7rZBNZC8oVPxF5bMz0bhjZAwp7hkZBBAINaEg8esLqOhv28f3Lwh53KNwgKd9EY88IoEN7LUr80kwx6BsWlqw9wrjAgJkReJWqQVQ5FUtaZAZAQZDZD',
verify: 'sia-app-challenge-bot'
});
bot.on('error', (err) => {
console.log(err.message)
});
bot.on('message', (payload, reply) => {
var fbid = payload.sender.id;
var userQuery = payload.message.text;
var queryType = intentClassifier.classify(userQuery); // E.g. ['need_directions','check_flight']
bot.getProfile(fbid, (err, profile) => {
if (err) throw err;
// Connecting to DB
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
// Checking to see if user exists
queryUserDocument(fbid, db, function(err, user){
if (err == null) {
if (user.length > 0){
// This means a user is found
// This will assume user is typing in a flight ID, because he typed something alphanumeric
if (/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+[0-9a-z]+$/i.test(userQuery)){
var params = {
"userID": fbid,
"directions": 0,
"checkFlight": 0,
"flightID": userQuery
};
updateUserDocument(fbid, params, db, function(err, result){
if (!err){
insertConversation(fbid, userQuery, db, function(err, result){});
reply({text: "Ok we've noted your flight number! How can i help you?"}, function(err, info){});
insertConversation(fbid, "Ok we've noted your flight number! How can i help you?", db, function(err, result){});
}
db.close();
});
} else {
// We will parse the user's message to see what type of query he has made
insertConversation(fbid, userQuery, db, function(err, result){});
sendReply(user, fbid, queryType, reply);
db.close();
}
} else {
insertConversation(fbid, userQuery, db, function(err, result){});
insertUserDocument(fbid, db, function(err, result){
reply({text: "It seems like you're a first time user, we've registered an account for you!"}, function(err, info){
askForFlightNumber(reply);
insertConversation(fbid, "It seems like you're a first time user, we've registered an account for you!", db, function(err, result){});
});
});
}
} else {
// TODO must check to see if user has sent a registration number instead
defaultReply(reply);
db.close();
}
});
});
});
});
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(process.cwd() + '/public'));
var server = http.createServer(app).listen(8080);
console.log('Echo bot server running at port 8080.');
app.get('/', (req, res) => {
return bot._verify(req, res)
});
app.get('/retrieve_conversations', (req, res) => {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
retrieveConversations(db, function (err, result) {
res.send(result);
db.close();
});
});
});
app.post('/', (req, res) => {
bot._handleMessage(req.body)
res.end(JSON.stringify({status: 'ok'}))
});
app.get('/test', (req, res)=> {
// the same kind of magic happens here!
var userQuery = "Hi I'm lost";
if (req.query.message != undefined){
userQuery = req.query.message;
}
var queryType = intentClassifier.classify(userQuery); // E.g. ['need_directions','check_flight']
for (var i = 0; i < queryType.length; i++){
if (queryType[i] == "need_directions"){
console.log("holy shit, need_directions work");
res.send("holy shit, need_directions work");
} else if (queryType[i] == "check_flight"){
console.log("holy shit, check_flight work");
res.send("holy shit, check_flight work");
}
}
});
var fakeJson = {"code":200,"customers":[{"flightInfo":{"qrCodeBinary":"longassshithere","departureGate":"","departureAirport":"Singapore Changi","cabin":"Economy","eTicketNumber":"618241177233101","seatNumber":"047D","bookingReference":"248LJV","aircraftType":"Boeing 777-300ER","departureDate":"13 Jan","scheduledArrivalTime":"07:20AM","airlineUse":"001","departureTerminal":"T3","flightDeck":"","offPoint":"London","fullName":"TEST MR","boardPoint":"Singapore","isA380":false,"boardingDateTime":"12:15AM 13 Jan","membershipNo":"","boardPointCode":"SIN","flightNumber":"SQ306","boardingZone":
"5","scheduledDepartureTime":"01:15AM","offPointCode":"LHR","arrivalAirport":"London Heathrow","did":"2301D78000006503","loungeText":"","operatingAirline":"Singapore Airlines"},"uci":"2301D78000006CA2"}],"message":""}
var insertUserDocument = function(userID, db, callback) {
db.collection('users').insertOne({
"userID": userID, // This could be the FBID etc
"directions": 1,
"checkFlight": 0,
"flightID": ""
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the users collection.");
callback(err, result);
});
};
var queryUserDocument = function(userID, db, callback){
db.collection('users').find({
"userID" : userID
}).toArray(function(err, results){
assert.equal(err, null);
callback(err, results);
});
};
var updateUserDocument = function(userID, data, db, callback){
var toUpdate = {};
if (data.directions != undefined)
toUpdate.directions = data.directions;
if (data.checkFlight != undefined)
toUpdate.checkFlight = data.checkFlight;
if (data.flightID != undefined)
toUpdate.flightID = data.flightID;
db.collection('users').update({
"userID": userID, // This could be the FBID etc
}, {$set: toUpdate}, function(err, result){
callback(err, result);
});
};
var insertConversation = function(userID, msg, db, callback){
db.collection('conversations').insertOne({
"userID": userID, // This could be the FBID etc, and it will say "BOT" if it is the bot
"message": msg,
"timestamp": new Date()
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the conversations collection.");
callback(err, result);
});
};
var retrieveConversations = function(db, callback){
db.collection('conversations').find({}).toArray(function(err, results){
assert.equal(err, null);
callback(err, results);
});
};
var insertKeyword = function(sentence, keyword, count, sentiment, db, callback) {
db.collection('keywords').insertOne({
"sentence": sentence,
"keyword": keyword,
"count": count,
"sentiment": sentiment
}, function(err, result) {
assert.equal(err, null);
callback(err, result);
});
};
var pullKeywords = function(db, callback){
var array = db.collection('keywords').find().toArray(function(err, results){
assert.equal(err, null);
callback(err, results);
});
var arrayOfSentiments = [];
var arrayOfKeywords = [];
for (var x in array) {
arrayOfSentiments.push([x.sentence, x.sentiment]); //[sentance, sentiment score]
arrayOfKeywords.push([x.keyword, x.count]); //[keyword, count of that keyword]
}
arrayOfKeywords.sort(function(a, b) { //Sort the array of keywords by count
return a[1] > b[1] ? 1 : -1;
});
console.log(arrayOfSentiments);
console.log(arrayOfKeywords);
};
function sendReply(user, fbid, queryType, reply){
if (queryType.length > 0){
if (user[0].flightID == ""){
askForFlightNumber(reply);
return;
}
for (var i = 0; i < queryType.length; i++){
if (queryType[i] == "need_directions"){
sendDirections(reply);
} else if (queryType[i] == "check_flight"){
checkFlight(true, user[0].flightID, reply);
} else if (queryType[i] == "remind_me"){
remindMe(user[0].flightID, reply);
} else if (queryType[i] == "check_weather"){
checkWeather(user[0].flightID, reply);
} else if (queryType[i] == "negative_response"){
negativeResponse(reply);
} else if (queryType[i] == "positive_response"){
positiveResponse(reply);
} else if (queryType[i] == "greetings"){
greetingsResponse(reply);
} else {
defaultReply(reply);
}
}
} else {
// This means there wasn't any sensible analysis on the user's input
defaultReply(reply);
}
}
function sendDirections(reply){
//In order for us to give you directions, we would need your flight ID
var lat = 1.3644202;
var long = 103.99153079999996;
var mapJSON = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": {
"element": {
"title": "Follow the map to get to Belt 16!", //Change to belt number??
"image_url": "https:\/\/maps.googleapis.com\/maps\/api\/staticmap?size=764x400¢er="+lat+","+long+"&zoom=15&markers="+lat+","+long,
"item_url": "http:\/\/maps.apple.com\/maps?q="+lat+","+long+"&z=15"
}
}
}
}
};
reply(mapJSON, function(err, info) {
if (err) {
console.log(err);
}
})
}
function remindMe(flightID, reply){
//TODO add make a request to find out about flight
// var flightNum = userQuery.replace(/^\D+/g, '');
reply({text:"Okay sure! Based on your flight ID of: " + flightID + ", We will remind you 7 days, 3 days, 1 day, " +
"and also 5 hours before your flight! (But for proof of concept, we shall remind you in 7 seconds! :)"}, function(err, info) {
setTimeout(function(){
checkFlight(false, flightID, reply);
}, 7000);
if (err) {
console.log(err);
}
});
}
function checkWeather(flightID, reply){
var flightAlpha = flightID.replace(/[0-9]/g, '');
var flightNum = flightID.replace(/([a-zA-Z ])/g,'');
// Make an immediate reply first so that user doesn't think its lagging!
reply({text: "Searching your flight details for you! This may take some time, please be patient!"}, function(err, info){});
// Make a request to find flight details, where we subsequently parse it for more details
request({
url: "https://flifo-qa.api.aero/flifo/v3/flight/sin/" + flightAlpha + "/" + flightNum + "/d",
headers: {
'X-apiKey': '2cfd0827f82ceaccae7882938b4b1627',
'Accept': 'application/json'
}
}, function(requestErr, requestRes, requestBody){
requestBody = JSON.parse(requestBody);
if (requestBody.success) {
var responseText = "Based on your flight ID of: " + flightID + ", the weather at " + requestBody.flightRecord[0].city + " will be " + "rainy" + " at the time you touch down!";
reply({text: responseText}, function(err, info) {
if (err) {
console.log(err);
}
});
} else {
reply({
text: "It seems like you gave wrong flight details, because we can't find any flight data. :( Do let us know if you have a different Flight Number!"
}, function (err, info) {
if (err) {
console.log(err);
}
});
}
});
}
function negativeResponse(reply){
reply({text: "That doesn't help me do my job :("}, function(err, info) {
if (err) {
console.log(err);
}
});
}
function positiveResponse(reply){
reply({
"text":"Great! It has been nice chatting! :) Care to feedback?",
"quick_replies":[
{
"content_type":"text",
"title": "Great",
"payload":"Great job!"
},
{
"content_type":"text",
"title": "Meh",
"payload":"Feels meh"
},
{
"content_type":"text",
"title":"Lousy",
"payload":"That was bad"
}
]
}, function(err, info) {
if (err) {
console.log(err);
}
});
}
function greetingsResponse(reply){
reply({text: "Hey there! How can i help? :)"}, function(err, info) {
if (err) {
console.log(err);
}
});
}
function checkFlight(shouldGreet, flightID, reply){
var flightAlpha = flightID.replace(/[0-9]/g, '');
var flightNum = flightID.replace(/([a-zA-Z ])/g,'');
if (shouldGreet) {
// Make an immediate reply first so that user doesn't think its lagging!
reply({text: "Searching your flight details for you! This may take some time, please be patient!"}, function (err, info) {
});
}
// Make a request to find flight details, where we subsequently parse it for more details
request({
url: "https://flifo-qa.api.aero/flifo/v3/flight/sin/" + flightAlpha + "/" + flightNum + "/d",
headers: {
'X-apiKey': '2cfd0827f82ceaccae7882938b4b1627',
'Accept': 'application/json'
}
}, function(requestErr, requestRes, requestBody){
requestBody = JSON.parse(requestBody);
if (requestBody.success) {
var responseText = "Here are your flight details for " + flightID + ":\nFlight Date - " + requestBody.flightDate + "\nTerminal Number - " + requestBody.flightRecord[0].terminal + "\nFlight Duration - " + (requestBody.flightRecord[0].duration/60).toFixed(1) + "hrs \nHeaded For - " + requestBody.flightRecord[0].city;
reply({
text: responseText
}, function (err, info) {
if (err) {
console.log(err);
}
});
} else {
reply({
text: "It seems like you gave wrong flight details, because we can't find any flight data. :( Do let us know if you have a different Flight Number!"
}, function (err, info) {
if (err) {
console.log(err);
}
});
}
});
}
function defaultReply(reply){
reply({text:"Sorry i don't understand your query! :("}, function(err, info) {
if (err) {
console.log(err);
}
})
}
function askForFlightNumber(reply){
reply({text:"Welcome! Do you think i could have your Flight Number, so i can help you better?"}, function(err, info) {
if (err) {
console.log(err);
}
});
}