forked from microsoft/AcademicContent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
480 lines (414 loc) · 17.6 KB
/
app.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
var restify = require('restify');
var builder = require('botbuilder');
var tfl = require('tfl.api')(process.env.TFL_APP_ID, process.env.TFL_APP_KEY);
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
//var connector = new builder.ConsoleConnector().listen();
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
var model = process.env.LUIS_MODEL;
var recognizer = new builder.LuisRecognizer(model);
var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.endConversationAction('goodbye', 'Goodbye :)', { matches: /^goodbye/i });
bot.beginDialogAction('help', '/help', { matches: /^help/i });
bot.dialog('/', intents);
intents.matches('None', '/help')
.matches('findbylocation', '/findByClosestBusStop')
.matches('findbybusnum', '/findByBusNum')
.matches('detailedquery', '/findByQuery')
.matches('greeting', '/greeting')
.matches('setlocation', '/location')
.onDefault(builder.DialogAction.send("I'm sorry. I didn't understand."))
//=========================================================
// Bot Dialogs
//=========================================================
bot.dialog('/findByClosestBusStop', [
function (session){
builder.Prompts.text(session, "Send me your current location, if you can't type: pass");
},
function (session, results, next) {
if(results.response == "pass" || results.response == "Pass"){
session.beginDialog('/noLocation');
}else{
session.userData.lat = session.message.entities[0].geo.latitude;
session.userData.lon = session.message.entities[0].geo.longitude;
// session.userData.lat = 51.4673838;
// session.userData.lon = -0.190017;
tfl.stoppoint({ lat: session.userData.lat, lon: session.userData.lon, stopTypes: 'NaptanBusWayPoint,NaptanBusCoachStation,NaptanPublicBusCoachTram', radius: 500})
.then(result => {
var searchResult = JSON.parse(result.text);
var naptanId = searchResult.stopPoints[0].id;
return tfl.stoppoint.byIdArrivals(naptanId);
})
.then(result => {
var searchResult = JSON.parse(result.text);
if(searchResult.length != 0){
var i = searchResult.length-1;
for(i; i>=0; i--){
var lineName = searchResult[i].lineName;
var destinationName = searchResult[i].destinationName;
var arrivalTime = searchResult[i].expectedArrival;
var time = new Date(arrivalTime);
var hour = time.getHours() + 1;
session.send(lineName + " to " + destinationName + " " + hour + ":" + time.getMinutes());
console.log(time.getHours() + ":" + time.getMinutes() + " " + lineName + " to " + destinationName);
}
}else{
session.send("There are no near by bus stops");
}
});
}
}
]);
bot.dialog('/findByBusNum', [
function (session, args) {
if(args.entities != null){
var busnum = builder.EntityRecognizer.findEntity(args.entities, 'busnum');
session.userData.busnum = busnum.entity;
}else{
session.beginDialog('/getBusNum');
}
builder.Prompts.text(session, "Send me your current location, if you can't type: pass");
},
function (session, results, next) {
if(results.response == "pass" || results.response == "Pass"){
session.beginDialog('/noLocation');
}else{
session.userData.lat = session.message.entities[0].geo.latitude;
session.userData.lon = session.message.entities[0].geo.longitude;
// session.userData.lat = 51.4673838;
// session.userData.lon = -0.190017;
tfl.stoppoint({ lat: session.userData.lat, lon: session.userData.lon, stopTypes: 'NaptanBusWayPoint,NaptanBusCoachStation,NaptanPublicBusCoachTram'})
.then(result => {
var searchResult = JSON.parse(result.text);
var stopPointsNum = searchResult.stopPoints.length;
var direction = new Array();
var i = 0;
var j = 0;
var counter = 0;
for(i; i<stopPointsNum; i++){
for(j; j<searchResult.stopPoints[i].lines.length; j++){
if(searchResult.stopPoints[i].lines[j].name == session.userData.busnum){
direction[counter] = searchResult.stopPoints[i].additionalProperties[1].value;
counter++;
}
}
j=0;
}
session.userData.directionArray = direction;
session.beginDialog('/selectDirection');
});
next;
}
},
function (session, next){
tfl.stoppoint({ lat: session.userData.lat, lon: session.userData.lon, stopTypes: 'NaptanBusWayPoint,NaptanBusCoachStation,NaptanPublicBusCoachTram'})
.then(result => {
var naptanId;
var busDestination;
var searchResult = JSON.parse(result.text);
var userDirection = session.userData.direction;
var stopPointsNum = searchResult.stopPoints.length;
for(var i=0; i<stopPointsNum; i++){
if(searchResult.stopPoints[i].lines.length != 0){
busDestination = searchResult.stopPoints[i].additionalProperties[1].value;
if(busDestination.includes(userDirection)){
naptanId = searchResult.stopPoints[i].id;
break;
}
}
}
return tfl.stoppoint.byIdArrivals(naptanId);
}).then(result => {
var busnum = session.userData.busnum;
var direction = session.userData.direction;
var searchResult = JSON.parse(result.text);
var i = searchResult.length-1;
session.send(busnum + " towards " + direction);
for(i; i>=0; i--){
if(searchResult[i].lineName == busnum){
var lineName = searchResult[i].lineName;
var destinationName = searchResult[i].destinationName;
var arrivalTime = searchResult[i].expectedArrival;
var time = new Date(arrivalTime);
session.send(time.getHours()+1 + ":" + time.getMinutes());
console.log(time.getHours()+1 + ":" + time.getMinutes() + " " + lineName + " to " + destinationName);
}
}
});
session.endDialog();
}
]);
bot.dialog('/findByQuery', [
function (session, args) {
if(args != null){
var busnum = builder.EntityRecognizer.findEntity(args.entities, 'busnum');
var busstop = builder.EntityRecognizer.findEntity(args.entities, 'busstop');
var towards = builder.EntityRecognizer.findEntity(args.entities, 'towards');
if (!busnum) {
session.beginDialog('/getBusNum');
} else {
session.userData.busnum = busnum.entity;
}
if (!busstop) {
session.beginDialog('/getBusStop');
} else {
session.userData.busstop = busstop.entity;
}
if (!towards) {
session.beginDialog('/getTowards');
} else {
session.userData.towards = towards.entity;
}
session.beginDialog('/proxy');
}else{
session.beginDialog('/noLocation');
}
},
function(session){
session.beginDialog('/checkArrivals');
}
]);
bot.dialog('/proxy', [
function(session){
session.endDialog();
}
])
bot.dialog('/noLocation', [
function (session) {
session.beginDialog('/getBusNum');
},
function (session) {
session.beginDialog('/getBusStop');
},
function (session) {
session.beginDialog('/getTowards');
},
function (session) {
session.beginDialog('/checkArrivals');
session.endDialog();
}
]);
bot.dialog('/checkArrivals', [
function (session) {
var busnum = session.userData.busnum;
var busstop = session.userData.busstop;
var towards = session.userData.towards;
tfl.stoppoint.search(busstop)
.then(result => {
var searchResult = JSON.parse(result.text);
var stopId = searchResult.matches[0].id;
return tfl.stoppoint.byId(stopId);
})
.then(result => {
var naptanId;
var searchResult = JSON.parse(result.text);
for(var i=0; i<searchResult.children.length; i++){
var busdestination = searchResult.children[i].additionalProperties[1].value.toLowerCase();
if(busdestination.includes(towards.toLowerCase())){
naptanId = searchResult.children[i].id;
}
}
return tfl.stoppoint.byIdArrivals(naptanId);
})
.then(result => {
var searchResult = JSON.parse(result.text);
var i = searchResult.length-1;
session.send(busnum + " expected arrival times:");
for(i; i>=0; i--){
if(searchResult[i].lineName == busnum){
var lineName = searchResult[i].lineName;
var destinationName = searchResult[i].destinationName;
var arrivalTime = searchResult[i].expectedArrival;
var time = new Date(arrivalTime);
session.send(time.getHours()+1 + ":" + time.getMinutes());
console.log(time.getHours()+1 + ":" + time.getMinutes() + " " + lineName + " to " + destinationName);
}
}
});
session.endDialog();
}
]);
////////////////////////////////////
bot.dialog('/help', [
function (session) {
session.endDialog("Example commands that you can try: \n\n* Next bus \n* When is the 28 coming \n* When is the next 28 from townmead road to wandsworth \n* help - Displays these commands.");
}
]);
bot.dialog('/greeting', [
function (session) {
//Should check if user is using Telegram before trying to send a Telegram sticker
var data = { method: "sendSticker", parameters: { sticker: { url: "https://telegram.org/file/811140007/2/uHbXgsdVXQY/a75eee858dd829fb89", mediaType: "image/webp"} } };
const message = new builder.Message(session);
message.setChannelData(data);
session.send(message);
session.endDialog("Hey!");
}
]);
bot.dialog('/getBusNum', [
function (session) {
builder.Prompts.text(session, "Which bus are you looking for?");
},
function (session, results) {
if (results.response) {
session.userData.busnum = results.response;
session.endDialog();
} else {
session.send("Error");
session.endDialog();
}
}
]);
bot.dialog('/getBusStop', [
function (session) {
builder.Prompts.text(session, "Which bus stop are you at?");
},
function (session, results) {
if (results.response) {
session.userData.busstop = results.response;
session.endDialog();
} else {
session.send("Error");
session.endDialog();
}
}
]);
bot.dialog('/getTowards', [
function (session) {
builder.Prompts.text(session, "Where is the bus heading towards?");
},
function (session, results) {
if (results.response) {
session.userData.towards = results.response;
session.endDialog();
} else {
session.send("Error");
session.endDialog();
}
}
]);
bot.dialog('/selectDirection', [
function (session){
builder.Prompts.choice(session, "Which direction are you heading?", session.userData.directionArray);
},
function (session, results) {
session.userData.direction = results.response.entity;
session.endDialog();
}
])
bot.dialog('/selectBus', [
function (session){
builder.Prompts.choice(session, "Which bus?", session.userData.busArray);
},
function (session, results) {
session.userData.busnum = results.response.entity;
session.endDialog();
}
])
////////////////////////////
bot.dialog('/location', [
function (session, next) {
builder.Prompts.text(session, "Send me your current location");
},
function (session, results, next) {
session.userData.lat = session.message.entities[0].geo.latitude;
session.userData.lon = session.message.entities[0].geo.longitude;
// session.userData.lat = 51.4673838;
// session.userData.lon = -0.190017;
tfl.stoppoint({ lat: session.userData.lat, lon: session.userData.lon, stopTypes: 'NaptanBusWayPoint,NaptanBusCoachStation,NaptanPublicBusCoachTram'})
.then(result => {
var searchResult = JSON.parse(result.text);
var stopPointsNum = searchResult.stopPoints.length;
var direction = new Array();
var counter=0;
for(var i=0; i<stopPointsNum; i++){
if(searchResult.stopPoints[i].lines.length != 0){
direction[counter] = searchResult.stopPoints[i].additionalProperties[1].value;
counter++;
}
}
session.userData.directionArray = direction;
session.beginDialog('/selectDirection');
});
next;
},
function (session, next){
var latitude = session.userData.lat;
var longitude = session.userData.lon;
tfl.stoppoint({ lat: latitude, lon: longitude, stopTypes: 'NaptanBusWayPoint,NaptanBusCoachStation,NaptanPublicBusCoachTram'})
.then(result => {
var naptanId;
var searchResult = JSON.parse(result.text);
var userDirection = session.userData.direction;
var stopPointsNum = searchResult.stopPoints.length;
var busDestination;
var busNumbers = new Array();
for(var i=0; i<stopPointsNum; i++){
if(searchResult.stopPoints[i].lines.length != 0){
busDestination = searchResult.stopPoints[i].additionalProperties[1].value;
if(busDestination.includes(userDirection)){
session.userData.naptanId = searchResult.stopPoints[i].id;
for(var j=0; j<searchResult.stopPoints[i].lines.length; j++){
busNumbers[j] = searchResult.stopPoints[i].lines[j].name;
}
session.userData.busArray = busNumbers;
session.beginDialog('/selectBus');
break;
}
}
}
});
next;
},
function(session){
var naptanId = session.userData.naptanId;
var busnum = session.userData.busnum;
var direction = session.userData.direction;
tfl.stoppoint.byIdArrivals(naptanId)
.then(result => {
var searchResult = JSON.parse(result.text);
var i = searchResult.length-1;
session.send(busnum + " towards " + direction);
for(i; i>=0; i--){
if(searchResult[i].lineName == busnum){
var lineName = searchResult[i].lineName;
var destinationName = searchResult[i].destinationName;
var arrivalTime = searchResult[i].expectedArrival;
var time = new Date(arrivalTime);
session.send(time.getHours()+1 + ":" + time.getMinutes());
console.log(time.getHours()+1 + ":" + time.getMinutes() + " " + lineName + " to " + destinationName);
}
}
});
session.endDialog();
}
]);
///Not in use
// bot.dialog('/setLocation', [
// function (session) {
// builder.Prompts.text(session, "Send me your current location, if you can't type: pass");
// },
// function (session, results) {
// if(session.message.entities.length != 0){
// session.userData.lat = session.message.entities[0].geo.latitude;
// session.userData.lon = session.message.entities[0].geo.longitude;
// session.endDialog();
// }else if(results.response == "pass" || results.response == "Pass"){
// session.beginDialog('/noLocation');
// }else{
// session.send("Didn't get your location");
// session.endDialog();
// }
// }
// ]);