-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
385 lines (327 loc) · 12 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
/**
* App ID for the skill
*/
var APP_ID = "amzn1.ask.skill.924c6c93-dee3-4148-9e12-a6e40fd7c1e3"; //replace with 'amzn1.echo-sdk-ams.app.[your-unique-value-here]';
var dealerService = require('./dealer');
var eventsService = require('./events');
var moviesService = require('./movies');
var stripTags = require('./strip-tags');
var constants = require('./constants');
/**
* The AlexaSkill prototype and helper functions
*/
var AlexaSkill = require('./AlexaSkill');
/**
* Westfield is a child of AlexaSkill.
* To read more about inheritance in JavaScript, see the link below.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
*/
var Westfield = function() {
AlexaSkill.call(this, APP_ID);
};
// Extend AlexaSkill
Westfield.prototype = Object.create(AlexaSkill.prototype);
Westfield.prototype.constructor = Westfield;
// ----------------------- Override AlexaSkill request and intent handlers -----------------------
Westfield.prototype.eventHandlers.onSessionStarted = function(sessionStartedRequest, session) {
console.log("onSessionStarted requestId: " + sessionStartedRequest.requestId + ", sessionId: " + session.sessionId);
// any initialization logic goes here
};
Westfield.prototype.eventHandlers.onLaunch = function(launchRequest, session, response) {
console.log("onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
handleWelcomeRequest(response);
};
Westfield.prototype.eventHandlers.onSessionEnded = function(sessionEndedRequest, session) {
console.log("onSessionEnded requestId: " + sessionEndedRequest.requestId + ", sessionId: " + session.sessionId);
// any cleanup logic goes here
};
/**
* override intentHandlers to map intent handling functions.
*/
Westfield.prototype.intentHandlers = {
"OneShotIntent": function(intent, session, response) {
handleOneShotRequest(intent, session, response);
},
"SupportedCentresIntent": function(intent, session, response) {
handleSupportedCentresRequest(intent, session, response);
},
"DialogIntent": function(intent, session, response) {
var centre = session.attributes.centre || getCentreFromIntent(intent);
var typeVal = session.attributes.type || getTypeFromIntent(intent);
if (!typeVal.error) {
session.attributes.type = typeVal;
}
if (!centre.error) {
session.attributes.centre = centre;
}
if (centre.error && typeVal.error) {
handleHelpRequest(response);
return;
}
if (centre.error) {
handleSupportedCentresRequest(intent, session, response);
return;
}
handleSupportedTypesRequest(intent, session, response);
},
"AIIntent": function(intent, session, response) {
response.tell("Well, in the end the A.I. always wins.");
},
"PretzelIntent": function(intent, session, response) {
response.tell("Wetzel's Pretzels is the best! You can find it in the basement level of San Francisco center.");
},
"EventsIntent": function(intent, session, response) {
var centre = session.attributes.centre || getCentreFromIntent(intent);
handleEventRequest(centre, response);
},
"DealsIntent": function(intent, session, response) {
var centre = session.attributes.centre || getCentreFromIntent(intent);
handleDealRequest(centre, response);
},
"MoviesIntent": function(intent, session, response) {
var centre = session.attributes.centre || getCentreFromIntent(intent);
handleMoviesRequest(centre, response);
},
"RetailerDealsIntent": function(intent, session, response) {
var centre = session.attributes.centre || getCentreFromIntent(intent);
handleRetailerDealsRequest(centre, response);
},
"AMAZON.HelpIntent": function(intent, session, response) {
handleHelpRequest(response);
},
"AMAZON.StopIntent": function(intent, session, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
},
"AMAZON.CancelIntent": function(intent, session, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
}
};
// -------------------------- Westfield Domain Specific Business Logic --------------------------
var HTML_REGEX = /(<([^>]+)>)/ig;
function handleWelcomeRequest(response) {
var repromptText = "What center can I help you with?";
var speechText = "Welcome to Westfield Malls. " + repromptText;
response.ask(speechText, repromptText);
}
function handleHelpRequest(response) {
var repromptText = "What center can I help you with?";
var speechText = "I can lead you through providing a center " + "or you can simply open Westfield and ask a question like, " + "get deals for San Francisco. Or, get events at World Trade Center. " + "Or you can say exit. " + repromptText;
response.ask(speechText, repromptText);
}
function handleSupportedCentresRequest(intent, session, response) {
var repromptText = "What center can I help you with?";
var speechOutput = "Currently, I know information about these Westfield centers: " + getAllCentresText() + repromptText;
response.ask(speechOutput, repromptText);
}
function handleSupportedTypesRequest(intent, session, response) {
var centre = session.attributes.centre || getCentreFromIntent(intent);
var typeVal = session.attributes.type || getTypeFromIntent(intent);
if (typeVal.error) {
var repromptText = "Say events or deals.";
var speechOutput = "I can get you information on events or deals at " + centre.name + ". " + repromptText;
response.ask(speechOutput, repromptText);
return;
}
if (typeVal.name === "deals") {
handleDealRequest(centre, response);
} else if (typeVal.name === "events") {
handleEventRequest(centre, response);
}
}
function handleOneShotRequest(intent, session, response) {
var centre = getCentreFromIntent(intent);
if (centre.error) {
var repromptText = "What center can I help you with?";
var speechOutput = "I don't know about " + centre.name + ". " + "Currently, I know information about these Westfield centers: " + getAllCentresText() + repromptText;
response.ask(speechOutput, repromptText);
return;
}
var type = getTypeFromIntent(intent);
if (type.error) {
handleSupportedTypesRequest(intent, session, response)
return
}
if (type.name == "deals") {
handleDealRequest(centre, response);
} else if (type.name == "events") {
handleEventRequest(centre, response);
}
}
function handleDealRequest(centre, response) {
dealerService.getDealsByCentre(centre.id, function(err, data) {
var speechText;
if (err !== null) {
speechText = "We had a problem looking up deals at " + centre.name + ". Please try again later. ";
} else {
speechText = "We found the following deals at Westfield " + centre.name + ": ";
var deals = data.data;
for (var i in deals) {
console.log(deals[i]);
var dealTitle = expandString(getTextFromHTML(deals[i].title));
var dealDescription = getTextFromHTML(deals[i].description);
var storeName = deals[i]._links.retailer.name;
var location = deals[i].stores[0].locations[0].level_name;
if (storeName) {
speechText = speechText + "At " + storeName + ",";
}
speechText = speechText + " " + dealTitle;
if (dealDescription) {
speechText = speechText + ". " + dealDescription;
}
speechText = speechText + " Located at " + location;
}
}
response.tell(speechText);
});
}
function handleEventRequest(centre, response) {
eventsService.getEventsByCenter(centre.id, function(err, data) {
var speechText;
if (err !== null) {
speechText = "We had a problem looking up events at " + centre.name + ". Please try again later. ";
} else {
var events = data.data;
speechText = "We found the following events at Westfield " + centre.name + ": ";
for (var i in events) {
var eventTitle = getTextFromHTML(events[i].name);
var eventDescription = getTextFromHTML(events[i].description);
speechText = speechText + " " + eventTitle;
if (eventDescription) {
speechText = speechText + ". " + eventDescription;
}
}
}
response.tell(speechText);
});
}
function handleRetailerDealsRequest(centre, response) {
dealerService.getRetailerDealsByCentre(centre.id, function(err, data) {
var speechText;
if (err !== null) {
speechText = "We had a problem looking up retailer deals at " + centre.name + ". Please try again later. ";
} else {
speechText = "The following retailers are having sales at Westfield " + centre.name + ": ";
var deals = data.data;
var retailers = {}
for (var i in deals) {
var storeName = deals[i]._links.retailer.name;
if (retailers[storeName]) {
retailers[storeName]++;
} else {
retailers[storeName] = 1;
}
}
var keysSorted = Object.keys(retailers).sort(function(a,b){return retailers[a]-retailers[b]})
var max = 5;
for (var i=keysSorted.length-1; i >=0; i--) {
if (max != 5) {
speechText = speechText + ", ";
}
speechText = speechText + " " + keysSorted[i] + " has " + retailers[keysSorted[i]];
speechText = speechText + " deal";
if (retailers[keysSorted[i]] > 1) {
speechText = speechText + "s";
}
max--;
if (max == 0) {
break;
}
}
}
response.tell(speechText);
});
}
function handleMoviesRequest(centre, response) {
moviesService.getMoviesByCentre(centre.id, function(err, data) {
var speechText;
if (err !== null) {
speechText = "We had a problem looking up movies at " + centre.name + ". Please try again later. ";
} else {
speechText = "The following movies are playing at Westfield " + centre.name + ": ";
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
var movies = data.data.hits;
for (var i=0; i<movies.length; i++) {
if (i>0) {
speechText = speechText + ",";
}
speechText = speechText + " " + movies[i].title;
}
if (movies.length == 0) {
speechText = "Sorry, there are no movies playing right now at Westfield " + centre.name;
}
}
response.tell(speechText);
});
}
function getAllCentresText() {
var centreList = '';
for (var i in constants.CENTRE_NAMES) {
centreList += constants.CENTRE_NAMES[i] + ", ";
}
return centreList;
}
function getCentreFromIntent(intent) {
var centreSlot = intent.slots.Centre;
// slots can be missing, or slots can be provided but with empty value.
// must test for both.
if (!centreSlot || !centreSlot.value) {
return {
error: true
}
} else {
var centreName = centreSlot.value.replace("center", "").replace("centre", "").trim().toLowerCase();
if (constants.CENTRES[centreName]) {
var centerId = constants.CENTRES[centreName];
if (centreName === "world trade") {
centreName = "world trade center";
}
return {
name: centreName,
id: centerId
}
} else {
return {
error: true,
name: centreName
}
}
}
}
function getTypeFromIntent(intent) {
var typeSlot = intent.slots.Type;
// slots can be missing, or slots can be provided but with empty value.
// must test for both.
if (!typeSlot || !typeSlot.value) {
return {
error: true
}
} else {
var typeName = typeSlot.value.toLowerCase();
if (typeName == "events" || typeName == "deals") {
return {
name: typeName
}
} else {
return {
error: true,
name: typeName
}
}
}
}
function expandString(val) {
return val.replace(/b1g1/gi, "buy one, get one")
}
function getTextFromHTML(val) {
return stripTags(val).replace(/ |[\r\n]/g," ").replace(/&([^;]+);/g, "");
}
// Create the handler that responds to the Alexa Request.
exports.handler = function(event, context) {
var tidePooler = new Westfield();
tidePooler.execute(event, context);
};