-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsms-flow.js
617 lines (533 loc) · 18.2 KB
/
sms-flow.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*jslint node: true, indent: 2, white: true, vars: true */
'use strict';
var util = require('util');
var Q = require('q');
var api = require('./ddotapi.js');
var geocoder = require('./geocoder.js');
var Strings = require('./strings.js');
var sman = require('./session-manager.js');
var metrics = require('./metrics.js');
var keywords = {
test: 'test',
near: 'near',
routes: 'routes'
};
// Greetings and shots-in-the-dark
// We should respond to these with something friendly, rather than trying to
// geocode them.
var greetings = [
'TextMyBus',
'Text My Bus',
'Hello',
'Hi',
'Hey',
'How long',
'My Bus',
'mybus',
'50464'
];
var helpKeywords = [
'HELP',
'FitVIP'
];
var stopKeyword = 'STOP';
var identKeyword = 'MYBUS';
var dummyFlowKeyword = 'Woodward and McWarrenson';
function keywordMatches(keyword, str) {
function check(kw) {
var s = str.trimLeft();
return (s.length >= kw.length &&
(s.substring(0, kw.length).toLocaleLowerCase() ===
kw.toLocaleLowerCase()));
}
if (util.isArray(keyword)) {
return keyword.some(check);
}
return check(keyword);
}
// fun(key, value)
function forEachKey(obj, fun) {
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
fun(key, obj[key]);
}
}
}
// For each word, make the first letter uppercase and the other letters
// lowercase.
function toMixedCase(str) {
var arr = str.split(' ');
return arr.map(function (s) {
if (s.length <= 1) {
return s.toLocaleUpperCase();
}
return s[0].toLocaleUpperCase() + s.substr(1).toLocaleLowerCase();
}).join(' ');
}
// Compress whitespace in a string and trim the ends.
function compressWhitespace(str) {
return str.trim().replace(/[\s\n\r]+/g,' ');
}
function startsWith(full, piece, options) {
if (full.length < piece.length) { return false; }
if (options && !options.caseSensitive) {
// Case insensitive match
return full.substring(0, piece.length).toLocaleLowerCase() ===
piece.toLocaleLowerCase();
}
// Match case
return full.substring(0, piece.length) === piece;
}
// Strip the trailing signature from an incoming SMS.
function stripSignature(msg) {
// We check to see if the message has a coda that's been bracketed by one of
// the following srings. The order here matters. If the signature is
// '$$blahblah$$', and we're looking for signatures bracketed by '$', then
// we'll just strip the last two '$' characters. So we need to search for
// '$$'. In other words, we should put the greedier strings first.
var signatureBrackets = [
'$$',
'**',
'$',
'*',
'^.^',
'^^',
'^'
];
function trimBracketed(msg, bracket) {
// Look for bracketing by a string
// So something like: real message *16 KINGS*
// or: real message $$cool signature$$
if (msg.length > bracket.length) {
if(msg.slice(msg.length - bracket.length) === bracket) {
var piece = msg.slice(0, -bracket.length);
var index = piece.lastIndexOf(bracket);
if (index !== -1) {
return piece.slice(0, index);
}
}
}
return msg;
}
// We check to see if the message has a coda that's been bracketed by one of
// the following strings. We try to find bracketed signatures first, because
// we might need to be a little more conservative about separators.
var signatureSeparators = [
'\n',
'^.^'
];
function trimSeparated(msg, separator) {
var index = msg.lastIndexOf(separator);
if (index !== -1) {
return msg.slice(0, index);
}
return msg;
}
var index;
// Look for signatures bracketed by certain characters.
var trimmed = signatureBrackets.reduce(function (prev, current) {
// If we already trimmed successfully, skip the rest.
if (prev !== msg) { return prev; }
return trimBracketed(msg, current);
}, msg);
if (trimmed !== msg) {
return trimmed;
}
trimmed = signatureSeparators.reduce(function (prev, current) {
if (prev !== msg) { return prev; }
return trimSeparated(msg, current);
}, msg);
if (trimmed !== msg) {
return trimmed;
}
return msg;
}
// Strip the agency from the stop ID, leaving only the numeric ID.
// If the ID includes the agency, it will look like 'DDOT_10023', otherwise, it
// will look like '10023'.
function stripAgency(stopId) {
var parts = stopId.split('_');
if (parts.length === 1) {
return parts[0];
}
return parts[1];
}
// Determine if any of the arrival times are based on schedule data (as opposed
// to predicted, real-time data).
function hasSched(arrivals) {
return arrivals.some(function (entry) {
return !entry.predicted;
});
}
function organizeArrivalsByHeadsign(arrivals, now, max) {
var headsigns = {};
var scheduled = false;
arrivals.forEach(function (entry) {
// Skip entries in the past
if (entry.arrival < now) {
return;
}
// Times for this headsign
var times = headsigns[entry.headsign];
if (times === undefined) {
times = [];
headsigns[entry.headsign] = times;
}
// Don't add more arrivals if we've reached the max.
if (max && (times.length >= max)) {
return;
}
// Milliseconds to minutes.
var time = Math.floor((entry.arrival - now) / 60000);
var timeString;
if (entry.predicted) {
timeString = util.format(Strings.TimeMinutes, time);
} else {
// Indicate schedule-only data.
timeString = util.format(Strings.TimeMinutesSchedule, time);
scheduled = true;
}
times.push(timeString);
});
return {
headsigns: headsigns,
scheduled: scheduled
};
}
// Create a string of headsigns and arrival times.
// arrivals: the array of arrivals as returned by ddotapi
// now: the current time as reported by the API
// max (optional): the maximum number of arrivals to include
// stopName: the name of the stop in question
function makeArrivalString(arrivals, now, max, stopName) {
// Filter out predictions for past arrivals, which might see if a bus is
// ahead of schedule.
arrivals = arrivals.filter(function (entry) {
return entry.arrival > now;
});
// Filter out scheduled buses
// (they probably aren't actually on the road)
arrivals = arrivals.filter(function (entry) {
return entry.predicted;
});
// Make the stop name legible.
if (stopName === undefined) {
stopName = max;
}
stopName = toMixedCase(stopName);
if (arrivals.length === 0) {
// If there are no arrivals at all, say so.
return util.format(Strings.NoArrivals, api.lookaheadTime, stopName);
}
// Organize by headsign
var organized = organizeArrivalsByHeadsign(arrivals, now, max);
var headsigns = organized.headsigns;
// Join the various headsigns, omitting the ones with no arrivals.
var arrivalSets = [];
forEachKey(headsigns, function (headsign, times) {
// If there are no arrivals for this headsign, skip.
if (times.length === 0) { return; }
var timeString = times.join(', ');
var arrivalString = util.format(Strings.Arrivals, compressWhitespace(toMixedCase(headsign)), timeString);
arrivalSets.push(arrivalString);
});
if (organized.scheduled) {
return util.format(Strings.MiscWithSched, arrivalSets.join('\n'));
}
return arrivalSets.join('\n');
}
// Session tracking objects
//
// Types of stored conversation context
var conversationTypes = {
// We asked a multiple choice question. We stored the valid responses,
// actions to take, and action parameters
multi: 'multichoice'
};
// Map stored strings to actual action that we should perform
var actions = {
// Get the arrivals for the saved stop ID
arrivalsForStop: function arrivalsForStop(stopId) {
return api.getArrivalsForStop(stopId)
.then(function (data) {
var formatString = Strings.SingleStop;
if (hasSched(data.arrivals)) {
formatString = Strings.SingleStopWithSched;
}
var message = util.format(formatString,
toMixedCase(data.stopName),
makeArrivalString(data.arrivals, data.now, 3, data.stopName));
return message;
});
},
// Get the arrivals for the saved stop ID and present the arrivals that match
// the saved headsign.
// info = JSON.stringify({stopId: 'someID', headsign: 'someHeadsign'})
arrivalsForStopAndHeadsign: function arrivalsForStopAndHeadsign(infoJSON) {
var info = JSON.parse(infoJSON);
return api.getArrivalsForStop(info.stopId)
.then(function (data) {
var formatString = Strings.SingleStopWithId;
var arrivals = data.arrivals.filter(function (item) {
return compressWhitespace(item.headsign) === info.headsign;
});
var message = util.format(formatString,
toMixedCase(data.stopName),
makeArrivalString(arrivals, data.now, 3, data.stopName),
stripAgency(info.stopId));
return message;
});
}
};
function handleTestCommand(cmd) {
console.log('Handling a test command.');
if (keywordMatches(keywords.near, cmd)) {
// Get lon-lat for the specified location
return geocoder.code(cmd.substring(keywords.near.length))
.then(function (coords) {
// Get the nearby stops
return api.getStopsForLocation(coords);
})
.then(function (stops) {
var message = 'nearby stops: ';
message += stops
.map(function (stop) {
return util.format('%s: %s', stop.id.substring('Detroit Department of Transportation_'.length), stop.name);
})
.join('\n');
return message;
});
}
if (keywordMatches(keywords.routes, cmd)) {
console.log('Getting routes');
return api.getRoutes()
.then(function (routes) {
var message = 'Routes:';
var i;
for (i = 0; i < routes.length; i += 1) {
message += ' ' + routes[i].shortName;
}
return message;
});
}
return Q.fcall(function () {
return 'I did not understand the test command';
});
}
function tryContinueMultiConversation(sms, context) {
// Get the index of the selected option
var index = -1;
var i;
for (i = 0; i < context.choices.length; i += 1) {
var choice = context.choices[i];
if (startsWith(sms, choice, {caseSensitive: false}) &&
(sms.length === choice.length ||
sms[choice.length] === ' ' ||
sms[choice.length] === ')')) {
index = i;
break;
}
}
// Do we have a valid response?
if (index !== -1) {
console.log('Continuing conversation with action: ' + context.actions[i]);
// Perform the action. Returns a promise.
return actions[context.actions[i]](context.params[i]);
}
return null;
}
module.exports = (function () {
var self = {};
self.respondToSms = function (sms, id, logEntry) {
// Look for the short code HELP keyword.
if (keywordMatches(helpKeywords, sms)) {
return Q.resolve(Strings.ShortCodeHelp);
}
// Look for the short code STOP keyword.
if (keywordMatches(stopKeyword, sms)) {
return Q.resolve(Strings.ShortCodeStop);
}
// Look for the short code MYBUS keyword.
if (keywordMatches(identKeyword, sms)) {
return Q.resolve(Strings.ShortCodeMyBus);
}
// Look for the short code test flow keyword.
if (keywordMatches(dummyFlowKeyword, sms)) {
return Q.resolve(Strings.ShortCodeDummyFlow);
}
// Look for the test keyword.
if (keywordMatches(keywords.test, sms)) {
return handleTestCommand(sms.substring(keywords.test.length));
}
// If the buses aren't running, well, then the app has an easy job.
if (process.env.NOBUSES) {
return Q.resolve(Strings.NotRunning);
}
// If we're working on the system, let people know.
if (process.env.MAINTENANCE) {
return Q.resolve(Strings.Maintenance);
}
// Look for a greeting or shot-in-the-dark message.
if (keywordMatches(greetings, sms)) {
return Q.resolve(Strings.Greeting);
}
// Track that we got a message
metrics.message(id);
sms = stripSignature(sms);
console.log('Incoming message stripped of signature: ' + sms);
return sman.get(id)
.then(function (entry) {
var promise;
// See if the message is still part of the conversation.
if (entry !== null) {
var context = entry.context;
// We only handle multiple choice right now.
if (context.type === conversationTypes.multi) {
promise = tryContinueMultiConversation(sms, context);
}
}
// If we matched the SMS to a conversation, then we're done here.
if (promise) {
// Track that the user is continuing a conversation
metrics.conversationContinue(id);
logEntry.data.continuation = true;
return promise;
}
// If we didn't match the SMS to a conversation, treat it as an initial
// request.
var number = parseInt(sms, 10);
// TODO: distinguish between zipcodes and stop IDs
if (!isNaN(number) && number.toString() === sms.trim()) {
// We got numeric text. Treat it as a stop ID.
var index = sms.indexOf(' ');
var stopId;
if (index === -1) {
stopId = sms;
} else {
stopId = sms.substring(0, index);
}
// Fetch the arrival time info
var stopPromise = api.getArrivalsForStop(stopId)
.then(function (data) {
return util.format(Strings.SingleStop,
toMixedCase(data.stopName),
makeArrivalString(data.arrivals, data.now, 3, data.stopName));
})
.fail(function (reason) {
console.log(reason.message);
return Strings.GenericFailMessage;
});
// Track that the user sent a stop ID
metrics.stopID(id);
logEntry.data.stopID = stopId;
return stopPromise;
}
// Non-numeric. Treat it as a location.
var geocoderStartTime = Date.now();
// Get lon-lat for the specified location
return geocoder.code(sms)
.then(function (coords) {
console.log(util.format('Geocoder: took %s ms',Date.now() - geocoderStartTime));
logEntry.data.lon = coords.lon;
logEntry.data.lat = coords.lat;
if (coords.meta !== undefined && coords.meta.service !== undefined) {
logEntry.data.geocoder = coords.meta.service;
}
// Get the nearby stops
return api.getStopsForLocation(coords);
})
.then(function (stops) {
if (stops.length === 0) {
throw new Error('No stops found. Probably the geocoder was way off.');
}
console.log('Got nearby stops from API');
// Get arrivals for the nearest 5 stops.
var arrivalPromises = [];
var stopInd;
for (stopInd = 0; stopInd < 5 && stopInd < stops.length; stopInd += 1) {
arrivalPromises.push(api.getHeadsignsForStop(stops[stopInd].id));
}
return Q.allResolved(arrivalPromises)
.then(function (promises) {
console.log('Got arrivals from API');
var stopsAndHeadsigns = [];
var headsignHash = {};
var rejectCount = 0;
promises.forEach(function (promise) {
if (promise.isFulfilled()) {
var stop = promise.valueOf();
var i;
var headsign;
for (i = 0; i < stop.headsigns.length; i += 1) {
// Add unseen headsigns to the list.
headsign = compressWhitespace(stop.headsigns[i]);
if (!headsignHash.hasOwnProperty(headsign)) {
headsignHash[headsign] = true;
stopsAndHeadsigns.push({
stopId: stop.stopId,
headsign: headsign
});
}
}
} else {
rejectCount += 1;
}
});
if (rejectCount === promises.length) {
// All of the promises were rejected.
throw new Error('Unable to get trip headsigns for nearby stops.');
}
if (stopsAndHeadsigns.length === 0) {
// We didn't find any trips at nearby stops. Probably the buses
// have stopped running around there.
return Strings.NoCloseRoutes;
}
// If there's only one headsign, just report arrivals, don't bother with a conversation.
if (stopsAndHeadsigns.length === 1) {
return api.getArrivalsForStop(stopsAndHeadsigns[0].stopId)
.then(function (data) {
var arrivals = data.arrivals.filter(function (item) {
return compressWhitespace(item.headsign) === stopsAndHeadsigns[0].headsign;
});
var message = util.format(Strings.SingleStop,
toMixedCase(data.stopName),
makeArrivalString(arrivals, data.now, 3, data.stopName));
return message;
});
}
// Multiple headsigns, so let's sort them before we present them.
stopsAndHeadsigns.sort(function (a, b) {
var aNum = parseInt(a.headsign.split(' ')[0], 10);
var bNum = parseInt(b.headsign.split(' ')[0], 10);
if (aNum < bNum || isNaN(bNum)) { return -1; }
if (aNum > bNum || isNaN(aNum)) { return 1; }
return 0;
});
// We need context, so we can continue the conversation later.
var context = {
type: conversationTypes.multi,
choices: [],
actions: [],
params: []
};
var letters = ['A', 'B', 'C', 'D', 'E', 'F'];
var optionsText = [];
var i = 0;
while (i < letters.length && i < stopsAndHeadsigns.length) {
optionsText.push(util.format(Strings.Option, letters[i], toMixedCase(stopsAndHeadsigns[i].headsign)));
context.choices.push(letters[i]);
context.actions.push('arrivalsForStopAndHeadsign');
context.params.push(JSON.stringify(stopsAndHeadsigns[i]));
i += 1;
}
// Save the session context.
sman.save(id, context);
var message = Strings.CloseRoutes + '\n' + optionsText.join('\n');
return message;
});
});
});
};
return self;
}());