-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (110 loc) · 3.9 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
require('isomorphic-fetch');
const uuidv4 = require("uuid/v4");
var env = process.env.NODE_ENV || 'development';
if(env !== 'production') {
require('dotenv').load({ path: '.env.' + env });
}
var applicationId = process.env.APPLICATION_ID;
var mtaStatusURL = process.env.MTA_STATUS_URL;
var _ = require('lodash');
var levenshtein = require('fast-levenshtein');
var Alexa = require('alexa-sdk');
var currentMTAStatus = require('./current-mta-status.js');
var statusToSpeech = require('./status-to-speech.js');
var fetchStatus = function(callback) {
return fetch(mtaStatusURL).then(function(response) {
return response.text()
}).then(function(body) {
currentMTAStatus(body, callback);
});
};
const affectedServiceStatusesBuilder = (statuses) => {
let notGoodService = status => status.status !== 'GOOD SERVICE';
let affectedServices = statuses
.filter(notGoodService)
return _(affectedServices)
.groupBy('status')
.map((lines, status, collection) => {
lines = lines.map(status => status.nameGroup);
return statusToSpeech(lines, status);
}).value();
};
var fullStatusUpdateHandler = function() {
fetchStatus(statuses => {
let affectedServiceStatuses = affectedServiceStatusesBuilder(statuses);
if(affectedServiceStatuses.length === 0) {
this.emit(':tell', 'Good service on all lines, what a rare day in NYC');
} else {
affectedServiceStatuses.push('Good service on all other lines. ');
this.emit(':tell', affectedServiceStatuses.join('\n'));
}
});
};
var handlers = {
statusOfLine: function () {
var self = this;
var nameGroup = self.event.request.intent.slots.subwayLineOrGroup.value;
var closestStatus = null;
var badQueryResponse = function() {
self.emit(':tell', "Sorry, I didn't hear a subway line I understand");
}
if(nameGroup) {
fetchStatus(function(statuses) {
if(nameGroup.length > 1) {
closestStatus = _.minBy(statuses, function(status) {
return levenshtein.get(status.nameGroup, nameGroup.toUpperCase());
});
} else {
closestStatus = _.find(statuses, function(status) {
return status.nameGroup.search(nameGroup.toUpperCase()) !== -1;
});
}
if(closestStatus) {
if(closestStatus.description) {
var serviceStatus = `${statusToSpeech(closestStatus.nameGroup, closestStatus.status)}I've added a card with the details on the Alexa App.`
self.emit(':tellWithCard', serviceStatus, `Subway Status for ${closestStatus.nameGroup}`, closestStatus.description);
} else {
self.emit(':tell', statusToSpeech(closestStatus.nameGroup, closestStatus.status));
}
} else {
badQueryResponse();
}
});
} else {
badQueryResponse();
}
},
fullStatusUpdate: fullStatusUpdateHandler,
Unhandled: fullStatusUpdateHandler
};
exports.flashBriefingHandler = (event, context, callback) => {
fetchStatus(statuses => {
let affectedServiceStatuses = affectedServiceStatusesBuilder(statuses);
let message;
if(affectedServiceStatuses.length === 0) {
message = 'Good service on all lines, what a rare day in NYC';
} else {
affectedServiceStatuses.push('Good service on all other lines. ')
message = affectedServiceStatuses.join("");
}
callback(null, {
statusCode: 200,
body: JSON.stringify({
uid: uuidv4(),
titleText: "Current NYC Subway Status",
mainText: message,
redirectionUrl: "http://www.mta.info/mta-service-status-widget",
updateDate: new Date().toISOString()
})
});
});
};
exports.handler = function(event, context, callback){
if(env === 'production') {
console.log(event.request); // Log to Cloudwatch
}
var alexa = Alexa.handler(event, context);
alexa.appId = applicationId;
alexa.registerHandlers(handlers);
alexa.execute();
};