forked from CamiWilliams/RestaurantSkill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
328 lines (295 loc) · 10.3 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
/* eslint-disable func-names */
/* eslint-disable no-console */
const Alexa = require('ask-sdk-core');
const HAPPY_HOUR = [
{
dish: "Wine",
description: "Half pour glass of wine for $4.00, full pour glass for $6.00.",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/wine.mp4"
},
{
dish: "Beers",
description: "Beers on tap for half off.",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/beer.mp4"
},
{
dish: "Appetizers",
description: "All appetizer menu items for $5.00",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/fries.mp4"
},
{
dish: "Desserts",
description: "All dessert menu items for $5.00",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/cake.mp4"
}
];
const SPECIALS = [
{
dish: "Filet Mignon",
description: "Our signature filet mignon steak, served with mashed potatoes and asparagus.",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/steak.mp4"
},
{
dish: "Pasta Burrata",
description: "Fresh pasta with vine-ripened tomatoes, garlic, basil, and mozzarella.",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/pasta.mp4"
},
{
dish: "Vegetarian Paella",
description: "Valencian rice dish with artichoke hearts, green beans, peas and saffron.",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/paella.mp4"
},
{
dish: "Lobster Tails",
description: "Rich meaty lobster tails with a peanut garlic sauce and butter.",
vid: "https://s3.amazonaws.com/apl-community-code/restaurant/lobster.mp4"
}
];
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
let speechText = "Welcome to the Alexa Cafe! "
+ "To get started, you can ask for things like restaurant hours, specials, or happy hour. ";
let reprompt = "What would you like to learn about? "
if (supportsAPL(handlerInput)) {
handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: require('./launchRequest.json')
})
.addDirective(require('./commands.json'));
}
return handlerInput.responseBuilder
.speak(speechText + reprompt)
.reprompt(reprompt)
.withSimpleCard('The Alexa Cafe', speechText)
.getResponse();
},
};
const HappyHourIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HappyHourIntent';
},
handle(handlerInput) {
let speechText = "Here's what we have on happy hour today: "
+ "Wine, Beer, Appetizers, and Desserts. ";
let reprompt = "What else would you like to learn about? "
if (supportsAPL(handlerInput)) {
handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: require('./hh.json'),
datasources: {
"restaurantData": {
"properties": {
"title": "HAPPY HOUR",
"items": HAPPY_HOUR
}
}
}
});
}
return handlerInput.responseBuilder
.speak(speechText + reprompt)
.reprompt(reprompt)
.withSimpleCard('The Alexa Cafe', speechText)
.getResponse();
},
};
const SpecialsIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'SpecialsIntent';
},
handle(handlerInput) {
let speechText = "Here's what we have as our specials today: "
+ "Filet Mignon, Pasta Burrata, Paella, and Lobster Tails. ";
let reprompt = "What else would you like to learn about? "
if (supportsAPL(handlerInput)) {
handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: require('./hh.json'),
datasources: {
"restaurantData": {
"properties": {
"title": "SPECIALS",
"items": SPECIALS
}
}
}
});
}
return handlerInput.responseBuilder
.speak(speechText + reprompt)
.reprompt(reprompt)
.withSimpleCard('The Alexa Cafe', speechText)
.getResponse();
},
};
const ItemIntentHandler = {
canHandle(handlerInput) {
return (handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'ItemIntent')
|| (handlerInput.requestEnvelope.request.type === 'Alexa.Presentation.APL.UserEvent'
&& handlerInput.requestEnvelope.request.arguments.length > 0);
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
let speechText = "Here's more information about : "
let reprompt = "What else would you like to learn about? "
//default
let selectedFood = "Filet Mignon";
let item = SPECIALS[0];
if (request.type === 'Alexa.Presentation.APL.UserEvent') {
selectedFood = request.arguments[0];
} else {
selectedFood = handlerInput.requestEnvelope.request.intent.slots.item.resolutions.resolutionsPerAuthority[0].values[0].value.name;
}
let index = 0;
//check if happy hour item
while (index < 4) {
if (selectedFood == HAPPY_HOUR[index].dish) {
item = HAPPY_HOUR[index];
break;
}
index++;
}
index = 0;
//check if special item
while (index < 4) {
if (selectedFood == SPECIALS[index].dish) {
item = SPECIALS[index];
break;
}
index++;
}
speechText += item.dish + ". " + item.description + ". ";
if (supportsAPL(handlerInput)) {
handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
token: "VideoPlayerToken",
document: require('./item.json'),
datasources: {
"restaurantData": {
"properties": {
"item": item
}
}
}
});
}
return handlerInput.responseBuilder
.speak(speechText + reprompt)
.reprompt(reprompt)
.withSimpleCard('The Alexa Cafe', speechText)
.getResponse();
},
};
const AboutIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AboutIntent';
},
handle(handlerInput) {
let speechText = "Here's more information about the Alexa Cafe : "
+ "We are open everyday from 3 to 9PM. We are located in downtown Seattle. "
+ "Our phone number is 555 123 4567. Please call ahead for parties greater than 6. ";
let reprompt = "What else would you like to learn about? "
if (supportsAPL(handlerInput)) {
handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: require('./launchRequest.json')
}).addDirective(require('./commands.json'));
}
return handlerInput.responseBuilder
.speak(speechText + reprompt)
.reprompt(reprompt)
.withSimpleCard('The Alexa Cafe', speechText)
.getResponse();
},
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'To get started, you can ask for things like restaurant hours, specials, or happy hour. ';
if (supportsAPL(handlerInput)) {
handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: require('./launchRequest.json')
}).addDirective(require('./commands.json'));
}
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('The Alexa Cafe', speechText)
.getResponse();
},
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speechText = 'Goodbye! Stay Hungry!';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('The Alexa Cafe', speechText)
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.reprompt('Sorry, I can\'t understand the command. Please say again.')
.getResponse();
},
};
//----------------------------------------------------------------------
//-----------------------------APL HELPER-------------------------------
//----------------------------------------------------------------------
function supportsAPL(handlerInput) {
const supportedInterfaces =
handlerInput.requestEnvelope.context.System.device.supportedInterfaces;
const aplInterface = supportedInterfaces['Alexa.Presentation.APL'];
return aplInterface != null && aplInterface != undefined;
}
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestHandler,
HappyHourIntentHandler,
SpecialsIntentHandler,
ItemIntentHandler,
AboutIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();