-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
948 lines (795 loc) · 33.4 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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/* eslint-disable func-names */
/* eslint-disable no-console */
const Alexa = require('ask-sdk');
const Moment = require('moment-timezone');
// Update 2018/9/10 - If you see any module errors such as:
//
// serviceClientFactory.getUpsServiceClient is not a function
//
// try deleting the modules in your node_modules folder and run `npm install` again.
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const speechText = getWelcomeMessage(sessionAttributes)
+ " "
+ getPrompt(sessionAttributes);
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withAskForPermissionsConsentCard(permissions)
.getResponse();
},
};
const LaunchRequestWithConsentTokenHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === "LaunchRequest"
&& handlerInput.requestEnvelope.context.System.user.permissions
&& handlerInput.requestEnvelope.context.System.user.permissions.consentToken;
},
async handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const speechText = getWelcomeMessage(sessionAttributes)
+ " "
+ getPrompt(sessionAttributes);
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
const SIPRecommendationIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'RecommendationIntent'
&& handlerInput.requestEnvelope.request.dialogState !== 'COMPLETED';
},
handle(handlerInput) {
let currentIntent = handlerInput.requestEnvelope.request.intent;
const { responseBuilder } = handlerInput;
const result = disambiguateSlot(getSlotValues(currentIntent.slots));
console.log('disambiguateSlot:', JSON.stringify(result));
if (result) {
responseBuilder
.speak(result.prompt)
.reprompt(result.prompt)
.addElicitSlotDirective(result.slotName, currentIntent);
} else {
responseBuilder.addDelegateDirective(currentIntent);
}
console.log('RESPONSE:', JSON.stringify(responseBuilder.getResponse()));
return responseBuilder
.getResponse();
}
};
// const CustomerProvidedMealRecommendationIntentHandler = {
// canHandle(handlerInput) {
// return handlerInput.requestEnvelope.request.type === "IntentRequest"
// && handlerInput.requestEnvelope.request.intent.name === "RecommendationIntent"
// && handlerInput.requestEnvelope.request.intent.slots.meal.value;
// },
// handle(handlerInput) {
// }
// };
const SuggestMealRecommendationIntentHandler = {
canHandle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const slots = ["timeOfDay", "cuisine", "allergies", "diet"];
console.log('SuggestMealRecommendationIntent - meals:', sessionAttributes.recommendations.current.meals.length);
console.log('SuggestMealRecommendationIntent - meals:', JSON.stringify(sessionAttributes.recommendations.current.meals));
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'RecommendationIntent'
&& !handlerInput.requestEnvelope.request.intent.slots.meal.value
&& intentSlotsHaveBeenFilled(handlerInput.requestEnvelope.request.intent, slots)
&& !intentSlotsNeedDisambiguation(handlerInput.requestEnvelope.request.intent, slots);
},
handle(handlerInput) {
console.log('SuggestMealRecommendationIntent:', handlerInput.requestEnvelope.request);
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const currentIntent = handlerInput.requestEnvelope.request.intent;
// TODO: Do the look up here!
sessionAttributes.recommendations.current.meals = ["Domi Maeuntang", "Mae Un Tang", "Daegu Jorim"];
attributesManager.setSessionAttributes(sessionAttributes);
console.log('currentIntent.slots:', JSON.stringify(currentIntent.slots));
return handlerInput.responseBuilder
.speak("Great, I've found 3 meals: Domi Maeuntang, Mae Un Tang and Daegu Jorim which sounds best?")
.reprompt('Which sounds best Domi Maeuntang, Mae Un Tang or Daegu Jorim?')
.addElicitSlotDirective('meal', currentIntent)
.getResponse();
}
};
// TODO: handler for meals containing ingredients that conflict with their allergies and diet.
// TODO: remove this since we no longer need it.
const promptForDeliveryOption = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'RecommendationIntent'
&& handlerInput.requestEnvelope.request.intent.slots.meal.value
&& !handlerInput.requestEnvelope.request.intent.slots.deliveryOption.value;
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('Which would like, eat in, eat out, or make it?')
.reprompt('Would like to eat in, eat out, or make it?')
.addElicitSlotDirective('deliveryOption')
.getResponse();
}
};
const CRecommendationIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === "IntentRequest"
&& handlerInput.requestEnvelope.request.intent.name === "RecommendationIntent"
&& handlerInput.requestEnvelope.request.dialogState === "COMPLETED";
},
handle(handlerInput) {
console.log("COMPLETED RecommendationIntent");
const currentIntent = handlerInput.requestEnvelope.request.intent;
const slotValues = getSlotValues(currentIntent.slots);
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
sessionAttributes.recommendations.previous = slotValues.meal.synonym;
sessionAttributes[currentIntent.name] = undefined;
console.log("deleting slot data for:", currentIntent.name);
console.log("after delete:", JSON.stringify(sessionAttributes));
attributesManager.setSessionAttributes(sessionAttributes);
let speechText = "";
// TODO: split this into different completed handlers
if (slotValues.deliveryOption.statusCode === "ER_SUCCESS_MATCH") {
if (slotValues.deliveryOption.resolvedValues[0] !== "make") {
const address = sessionAttributes.profile.location.address;
if (address.zip || address.city && address.state) {
// TODO: look up where the restaurants would be
console.log("look up the restaurants");
speechText = "There's 2 restaurants close by korean bamboo and One pot. Which would you like?";
} else {
console.log("We need to elicit for address");
speechText = "To find a restaurant close by I need to know your address. What city do you live in?";
}
} else {
// TODO prompt for portion
speechText = "Which would you like a small, medium, or large portion size?";
}
} else {
// TODO: validate input for options - if we don't know ER_SUCCESS_NO_MATCH ask again
speechText = "Which would you like? to eat out, order delivery, or cook";
return handlerInput.responseBuilder
.addElicitSlotDirective("deliveryOption")
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
},
};
// TODO: remove this
// const GetMealIntentHandler = {
// canHandle(handlerInput) {
// return handlerInput.requestEnvelope.request.type === "IntentRequest"
// && handlerInput.requestEnvelope.request.intent.name === "GetMealIntent";
// },
// handle(handlerInput) {
// return handlerInput.responseBuilder
// .speak("Hello there")
// .getResponse();
// }
// };
// TODO: remove this
const LookupRestaurantIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === "IntentRequest"
&& handlerInput.requestEnvelope.request.intent.name === "LookupRestaurantIntent";
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak("I've sent Korean Bamboo's address to the Alexa App. Bon apetit!")
.getResponse();
}
};
const InProgressCaptureAddressIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === "IntentRequest"
&& handlerInput.requestEnvelope.request.intent.name === "CaptureAddressIntent"
&& handlerInput.requestEnvelope.request.dialogState !== "COMPLETED";
},
handle(handlerInput) {
return handlerInput.responseBuilder
.addDelegateDirective()
.getResponse();
}
};
const InProgressHasZipCaptureAddressIntentHandler = {
canHandle(handlerInput) {
const currentIntent = handlerInput.requestEnvelope.request.intent;
return handlerInput.requestEnvelope.request.type === "IntentRequest"
&& currentIntent.name === "CaptureAddressIntent"
&& intentSlotsHaveBeenFilled(currentIntent, ["zip"])
&& handlerInput.requestEnvelope.request.dialogState !== "COMPLETED";
},
handle(handlerInput) {
const currentIntent = handlerInput.requestEnvelope.request.intent;
const slotValues = getSlotValues(currentIntent.slots);
let speechText = "There's 2 restaurants close to " + slotValues.zip.synonym;
speechText += " Korean Bamboo and One pot. Which would you like?";
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
const InProgressHasCityStateCaptureAddressIntentHandler = {
canHandle(handlerInput) {
const currentIntent = handlerInput.requestEnvelope.request.intent;
return handlerInput.requestEnvelope.request.type === "IntentRequest"
&& currentIntent.name === "CaptureAddressIntent"
&& intentSlotsHaveBeenFilled(currentIntent, ["city", "state"])
&& handlerInput.requestEnvelope.request.dialogState !== "COMPLETED";
},
handle(handlerInput) {
const currentIntent = handlerInput.requestEnvelope.request.intent;
const slotValues = getSlotValues(currentIntent.slots);
let speechText = "There's 2 restaurants close to " + slotValues.city.synonym
+ ", "
+ slotValues.state.synonym
+ " Korean Bamboo and One pot. Which would you like?";
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'This is the foodie. I will find the best meal and restaurant recommendations for you. To get started say I\'m hungry';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('The Foodie', 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!';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('The Foodie', 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}`);
console.log(error.stack);
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();
},
};
/* RESPONSE INTERCEPTORS */
// This interceptor loads our profile from persistent storage into the session
// attributes.
const NewSessionRequestInterceptor = {
async process(handlerInput) {
console.log('request:', JSON.stringify(handlerInput.requestEnvelope.request));
if (handlerInput.requestEnvelope.session.new) {
const attributesManager = handlerInput.attributesManager;
let sessionAttributes = attributesManager.getSessionAttributes();
const persistentAttributes = await attributesManager.getPersistentAttributes();
console.log('persistentAttributes:', JSON.stringify(persistentAttributes));
if (!persistentAttributes.profile) {
console.log('Initializing new profile...');
sessionAttributes.isNew = true;
sessionAttributes.profile = initializeProfile();
sessionAttributes.recommendations = initializeRecommendations();
} else {
console.log('Restoring profile from persistent store.');
sessionAttributes.isNew = false;
sessionAttributes = persistentAttributes;
}
console.log("set sessionAttributes to:",JSON.stringify(sessionAttributes));
attributesManager.setSessionAttributes(sessionAttributes);
}
}
};
const SetTimeOfDayInterceptor = {
async process(handlerInput) {
const { requestEnvelope, serviceClientFactory, attributesManager } = handlerInput;
const sessionAttributes = attributesManager.getSessionAttributes();
// look up the time of day if we don't know it already.
if (!sessionAttributes.timeOfDay) {
const deviceId = requestEnvelope.context.System.device.deviceId;
const upsServiceClient = serviceClientFactory.getUpsServiceClient();
const timezone = await upsServiceClient.getSystemTimeZone(deviceId);
const currentTime = getCurrentTime(timezone);
const timeOfDay = getTimeOfDay(currentTime);
sessionAttributes.timeOfDay = timeOfDay;
sessionAttributes.profile.location.timezone = timezone;
attributesManager.setSessionAttributes(sessionAttributes);
console.log("SetTimeOfDayInterceptor - currentTime:", currentTime);
console.log("SetTimeOfDayInterceptor - timezone:", timezone);
console.log('SetTimeOfDayInterceptor - time of day:', timeOfDay);
console.log('SetTimeOfDayInterceptor - sessionAttributes', JSON.stringify(sessionAttributes));
}
}
};
const HasConsentTokenRequestInterceptor = {
async process(handlerInput) {
const { requestEnvelope, serviceClientFactory, attributesManager } = handlerInput;
const sessionAttributes = attributesManager.getSessionAttributes();
if (handlerInput.requestEnvelope.context.System.user.permissions
&& handlerInput.requestEnvelope.context.System.user.permissions.consentToken
&& (!sessionAttributes.profile.location.address.city
|| !sessionAttributes.profile.location.address.state
|| !sessionAttributes.profile.location.address.zip)) {
const { deviceId } = requestEnvelope.context.System.device;
const deviceAddressServiceClient = serviceClientFactory.getDeviceAddressServiceClient();
const address = await deviceAddressServiceClient.getFullAddress(deviceId);
console.log(JSON.stringify(address));
if (address.postalCode) {
sessionAttributes.profile.location.address.zip = address.postalCode;
} else if (address.city && address.stateOrRegion) {
sessionAttributes.profile.location.address.city = address.city;
sessionAttributes.profile.location.address.state = address.stateOrRegion;
}
attributesManager.setSessionAttributes(sessionAttributes);
console.log('HasConsentTokenRequestInterceptor - sessionAttributes', JSON.stringify(sessionAttributes));
}
}
};
// This interceptor initializes our slots with the values from the user profile.
const RecommendationIntentStartedRequestInterceptor = {
process(handlerInput) {
if (handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'RecommendationIntent'
&& handlerInput.requestEnvelope.request.dialogState === "STARTED") {
console.log("recommendationIntentStartedRequestInterceptor:", "Initialize the session attributes for the intent.");
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const profile = sessionAttributes.profile;
// handlerInput is passed by reference so any modification we make in
// our interceptor will be present in our intent handler's canHandle and
// handle function
const updatedIntent = handlerInput.requestEnvelope.request.intent;
updatedIntent.slots.name.value = profile.name || undefined;
updatedIntent.slots.diet.value = profile.diet || undefined;
updatedIntent.slots.allergies.value = profile.allergies || undefined;
updatedIntent.slots.timeOfDay.value = sessionAttributes.timeOfDay || undefined;
console.log(JSON.stringify(updatedIntent));
}
}
};
// This interceptor looks at the slots belonging to the request.
// If allergies or diet have been provided, it will store them in the user
// profile stored in the session attributes. When the skill closes, this
// information will be saved.
const RecommendationIntentCaptureSlotToProfileInterceptor = {
process(handlerInput) {
const intentName = "RecommendationIntent";
const slots = [ "allergies", "diet"];
console.log('recommendationIntentCaptureSlotToProfileInterceptor');
saveNewlyFilledSlotsToSessionAttributes(handlerInput, intentName, slots, (sessionAttributes, slotName, newlyFilledSlot) => {
sessionAttributes.profile[slotName] = newlyFilledSlot.synonym;
});
}
};
// This interceptor looks at the slots belonging to the request.
// If zip, city or state have been provided, it will store them in the user
// location attributes. When the skill closes, this information will be saved.
const CaptureAddressIntentCaptureSlotsToProfileInterceptor = {
process(handlerInput) {
const intentName = "CaptureAddressIntent";
const slots = ["zip", "city", "state"];
console.log('CaptureAddressIntentCaptureSlotsToProfileInterceptor call saveNewlyFilledSlotsToSessionAttributes');
saveNewlyFilledSlotsToSessionAttributes(handlerInput, intentName, slots, (sessionAttributes, slotName, newlyFilledSlot) => {
sessionAttributes.profile.location.address[slotName] = newlyFilledSlot.synonym;
});
}
};
// given an intent name and a set of slots, saveNewlyFilledSlotsToSessionAttributes
// will save newly filled values of the given slots into the session attributes.
// The callback allows you to set the slot value into session attributes however
// you want.
function saveNewlyFilledSlotsToSessionAttributes(handlerInput, intentName, slots, callback) {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const currentIntent = handlerInput.requestEnvelope.request.intent;
if (handlerInput.requestEnvelope.request.type === "IntentRequest"
&& currentIntent.name === intentName) {
const previousIntent = sessionAttributes[currentIntent.name];
console.log('CALL intentHasNewlyFilledSlots IN saveNewlyFilledSlotsToSessionAttributes ');
const newlyFilledSlots = intentHasNewlyFilledSlots(previousIntent, currentIntent, slots);
console.log('saveNewlyFilledSlotsToSessionAttributes');
// We only save if the slot(s) has been filled with something new.
if (newlyFilledSlots.found) {
for (let slotName in newlyFilledSlots.slots) {
console.log('inserting:',
slotName, JSON.stringify(newlyFilledSlots.slots[slotName]),
JSON.stringify(sessionAttributes));
callback(sessionAttributes, slotName, newlyFilledSlots.slots[slotName]);
}
attributesManager.setSessionAttributes(sessionAttributes);
}
}
}
// This interceptor handles intent switching during dialog management by
// syncing the previously collected slots stored in the session attributes
// with the current intent. The slots currently collected take precedence so
// the user is able to overidde previously collected slots.
const DialogManagementStateInterceptor = {
process(handlerInput) {
const currentIntent = handlerInput.requestEnvelope.request.intent;
if (handlerInput.requestEnvelope.request.type === "IntentRequest"
&& handlerInput.requestEnvelope.request.dialogState !== "COMPLETED") {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
// If there are no session attributes we've never entered dialog management
// for this intent before.
if (sessionAttributes[currentIntent.name]) {
let currentIntentSlots = sessionAttributes[currentIntent.name].slots;
for (let key in currentIntentSlots) {
// we let the current intent's values override the session attributes
// that way the user can override previously given values.
// this includes anything we have previously stored in their profile.
if (currentIntentSlots[key].value && !currentIntent.slots[key].value) {
currentIntent.slots[key] = currentIntentSlots[key];
}
}
}
sessionAttributes[currentIntent.name] = currentIntent;
attributesManager.setSessionAttributes(sessionAttributes);
}
}
};
/* Response INTERCEPTORS */
// This Response interceptor detects if the skill is going to exit and saves the
// session attributes into the persistent store.
const SessionWillEndInterceptor = {
async process(handlerInput, responseOutput) {
// let shouldEndSession = responseOutput.shouldEndSession;
// shouldEndSession = (typeof shouldEndSession == "undefined" ? true : shouldEndSession);
const requestType = handlerInput.requestEnvelope.request.type;
const ses = (typeof responseOutput.shouldEndSession == "undefined" ? true : responseOutput.shouldEndSession);
console.log('responseOutput:', JSON.stringify(responseOutput));
if(ses && !responseOutput.directives || requestType === 'SessionEndedRequest') {
// if(shouldEndSession || requestType == 'SessionEndedRequest') {
console.log('SessionWillEndInterceptor', 'end!');
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const persistentAttributes = await attributesManager.getPersistentAttributes();
persistentAttributes.profile = sessionAttributes.profile;
persistentAttributes.recommendations = sessionAttributes.recommendations;
persistentAttributes.recommendations.current.meals = [];
console.log(JSON.stringify(sessionAttributes));
attributesManager.setPersistentAttributes(persistentAttributes);
attributesManager.savePersistentAttributes(persistentAttributes);
}
}
};
/* CONSTANTS */
const permissions = ['read::alexa:device:all:address'];
const requiredSlots = {
allergies: true,
meal: true,
cuisine: true,
diet: true,
deliveryOption: true,
timeOfDay: true
};
/* HELPER FUNCTIONS */
function initializeProfile() {
return {
name: "",
allergies: "",
diet: "",
location: {
address: {
city: "",
state: "",
zip: ""
},
timezone: ""
}
};
}
function initializeRecommendations() {
return {
previous: {
meal: "",
restaurant: ""
},
current: {
meals: [],
restaurants: []
}
};
}
// gets the welcome message based upon the context of the skill.
function getWelcomeMessage(sessionAttributes) {
let speechText = "";
if (sessionAttributes.isNew) {
speechText += "<say-as interpret-as=\"interjection\">Howdy!</say-as> ";
speechText += "Welcome to The Foodie! ";
speechText += "I'll help you find the right food right now. ";
speechText += "To make that easier, you can give me permission to access your location, ";
speechText += "just check the Alexa app. ";
} else {
speechText += "Welcome back!! ";
const timeOfDay = sessionAttributes.timeOfDay;
if (timeOfDay) {
speechText += getTimeOfDayMessage(timeOfDay);
} else {
speechText += "It's time to stuff your face with delicious food. ";
}
if (sessionAttributes.recommendations.previous.meal) {
speechText += "It looks like last time you had " + sessionAttributes.recommendations.previous.meal + ". ";
speechText += "I wonder what it will be today. ";
}
}
return speechText;
}
function getTimeOfDayMessage(timeOfDay) {
const messages = timeOfDayMessages[timeOfDay];
return randomPhrase(messages);
}
function randomPhrase(phraseList) {
let i = Math.floor(Math.random() * phraseList.length);
return(phraseList[i]);
}
const timeOfDayMessages = {
breakfast: [
"It looks like it's breakfast. ",
"<say-as interpret-as=\"interjection\">cock a doodle doo</say-as> It's time for breakfast. ",
"Good morning! Time for breakfast"
],
brunch: [
"<say-as interpret-as=\"interjection\">cock a doodle doo</say-as> Let's get some brunch! ",
"It's time for brunch. "
],
lunch: [
"Lunch time! ",
"Time for lunch. "
],
dinner: [
"It's dinner time. ",
"It's supper time. "
],
midnight: [
"<say-as interpret-as=\"interjection\">wowza</say-as> You're up late! You looking for a midnight snack? ",
"It's time for a midnight snack. "
]
};
// gets the prompt based upon the context of the skill.
function getPrompt(sessionAttributes) {
let speechText = "How rude of me. I forgot to ask. What's your name?";
if (!sessionAttributes.isNew) {
speechText = "Let's narrow it down. What flavors do you feel like? You can say things like spicy, savory, greasy, and fresh.";
}
return speechText;
}
// given the slots object from the JSON Request to the skill, builds a simplified
// object which simplifies inpecting slots for entity resultion matches.
function getSlotValues(slots) {
const slotValues = {};
for (let key in slots) {
if (slots.hasOwnProperty(key)) {
slotValues[key] = {
synonym: slots[key].value || null ,
resolvedValues: (slots[key].value ? [slots[key].value] : []),
statusCode: null,
};
let statusCode = (((((slots[key] || {} )
.resolutions || {})
.resolutionsPerAuthority || [])[0] || {} )
.status || {})
.code;
let authority = ((((slots[key] || {})
.resolutions || {})
.resolutionsPerAuthority || [])[0] || {})
.authority;
slotValues[key].authority = authority;
// any value other than undefined then entity resolution was successful
if (statusCode) {
slotValues[key].statusCode = statusCode;
// we have resolved value(s)!
if (slots[key].resolutions.resolutionsPerAuthority[0].values) {
let resolvedValues = slots[key].resolutions.resolutionsPerAuthority[0].values;
slotValues[key].resolvedValues = [];
for (let i = 0; i < resolvedValues.length; i++) {
slotValues[key].resolvedValues.push({
value: resolvedValues[i].value.name,
id: resolvedValues[i].value.id
});
}
}
}
}
}
return slotValues;
}
function getNewSlots(previous, current) {
const previousSlotValues = getSlotValues(previous);
const currentSlotValues = getSlotValues(current);
let newlyCollectedSlots = {};
for(let slotName in previousSlotValues) {
// resolvedValues and statusCode are dependent on our synonym so we only
// need to check if there's a difference of synonyms.
if (previousSlotValues[slotName].synonym !== currentSlotValues[slotName].synonym){
newlyCollectedSlots[slotName] = currentSlotValues[slotName];
}
}
return newlyCollectedSlots;
}
// intentHasNewlyFilledSlots given a previous and current intent and a set of
// slots, this function will compare the previous intent's slots with current
// intent's slots to determine what's new. The results are filtered by the
// provided array of "slots". For example if you wanted to determine if there's
// a new value for the "state" and "city" slot you would pass the previous and
// current intent and an array containing both strings. If previous is undefined,
// all filled slots are treated as newly filled.
// Returns:
// {
// found: (true | false)
// slots: object of slots returned from getSlots
// }
function intentHasNewlyFilledSlots(previous, intent, slots) {
let newSlots;
// if we don't have a previous intent then all non-empty intent's slots are
// newly filled!
if (!previous) {
const slotValues = getSlotValues(intent.slots);
newSlots = {};
for (let slotName in slotValues) {
if (slotValues[slotName].synonym) {
newSlots[slotName] = slotValues[slotName];
}
}
} else {
newSlots = getNewSlots(previous.slots, intent.slots);
}
const results = {
found: false,
slots: {}
};
slots.forEach(slot => {
if(newSlots[slot]) {
results.slots[slot] = newSlots[slot];
results.found = true;
}
});
return results;
}
function buildDisambiguationPrompt(resolvedValues) {
let output = "Which would you like";
resolvedValues.forEach((resolvedValue, index) => {
output += `${(index === resolvedValues.length - 1) ? ' or ' : ' '}${resolvedValue.value}`;
});
output += "?";
return output;
}
function disambiguateSlot(slots) {
let result;
for(let slotName in slots) {
if (slots[slotName].resolvedValues.length > 1 && requiredSlots[slotName]) {
console.log('disambiguate:', slots[slotName]);
result = {
slotName: slotName,
prompt: buildDisambiguationPrompt(slots[slotName].resolvedValues)
};
break;
}
}
return result;
}
// given an intent and an array slots, intentSlotsHaveBeenFilled will determine
// if all of the slots in the array have been filled.
// Returns:
// (true | false)
function intentSlotsHaveBeenFilled(intent, slots){
const slotValues = getSlotValues(intent.slots);
console.log('slot values:', JSON.stringify(slotValues));
let result = true;
slots.forEach(slot => {
console.log('intentSlotsHaveBeenFilled:', slot);
if (!slotValues[slot].synonym) {
result = false;
}
});
return result;
}
function intentSlotsNeedDisambiguation(intent, slots) {
const slotValues = getSlotValues(intent.slots);
let result = false;
slots.forEach(slot => {
console.log(slotValues[slot].resolvedValues.length);
if(slotValues[slot].resolvedValues.length > 1) {
result = true;
}
});
console.log("intentSlotsNeedDisambiguation", result);
return result;
}
function getCurrentTime(location) {
const currentTime = Moment.utc().tz(location);
return currentTime;
}
function getTimeOfDay(currentTime) {
const currentHour = currentTime.hours();
const currentMinutes = currentTime.minutes();
const weightedHour = (currentMinutes >= 45) ? currentHour + 1 : currentHour;
let timeOfDay = "midnight";
if (weightedHour >= 6 && weightedHour <= 10) {
timeOfDay = "breakfast";
} else if (weightedHour == 11) {
timeOfDay = "brunch";
} else if (weightedHour >= 12 && weightedHour <= 16) {
timeOfDay = "lunch";
} else if (weightedHour >= 17 && weightedHour <= 23) {
timeOfDay = "dinner";
}
return timeOfDay;
}
const skillBuilder = Alexa.SkillBuilders.standard();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestWithConsentTokenHandler,
LaunchRequestHandler,
SuggestMealRecommendationIntentHandler,
// promptForDeliveryOption,
SIPRecommendationIntentHandler,
CRecommendationIntentHandler,
LookupRestaurantIntentHandler,
// GetMealIntentHandler,
InProgressHasZipCaptureAddressIntentHandler,
InProgressHasCityStateCaptureAddressIntentHandler,
InProgressCaptureAddressIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler
)
.addRequestInterceptors(
NewSessionRequestInterceptor,
SetTimeOfDayInterceptor,
HasConsentTokenRequestInterceptor,
RecommendationIntentStartedRequestInterceptor,
RecommendationIntentCaptureSlotToProfileInterceptor,
CaptureAddressIntentCaptureSlotsToProfileInterceptor,
DialogManagementStateInterceptor
)
.addResponseInterceptors(SessionWillEndInterceptor)
.addErrorHandlers(ErrorHandler)
//.withPersistenceAdapter()
//.withApiClient(new Alexa.DefaultApiClient())
.withAutoCreateTable(true)
.withTableName("theFoodie")
.lambda();