-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda.js
1892 lines (1626 loc) · 85.8 KB
/
lambda.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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This skill provides details about hurricanes through 2020, both prior years as well as current
*/
var aws = require('aws-sdk');
// this is used to track the application
const APP_ID = 'amzn1.echo-sdk-ams.app.709af9ef-d5eb-48dd-a90a-0dc48dc822d6';
// Get this years storm names
const atlanticStorms = require("data/atlantic2020.json");
const pacificStorms = require("data/pacific2020.json");
// current years storm names that have already occurred
const currYearStormArray = require("data/currStormData.json");
// storm names that historical information is available for in the datasets
// [ToDo: Add hurricane Omar, Paloma, Hanna]
const stormDetailAvail = require("data/stormDetailAvail.json");
// array of storm facts
const stormFacts = require("data/stormFacts.json");
// location of the storm dataset
var stormDataBucket = 'hurricane-data';
// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = function (event, context) {
try {
console.log("event.session.application.applicationId=" + event.session.application.applicationId);
/**
* This validates that the applicationId matches what is provided by Amazon.
*/
/*
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.709af9ef-d5eb-48dd-a90a-0dc48dc822d6") {
context.fail("Invalid Application ID");
}
*/
console.log(JSON.stringify(event));
if (event.session.new) {
onSessionStarted({requestId: event.request.requestId}, event.session);
}
if (event.request.type === "LaunchRequest") {
onLaunch(event.request,
event.session,
event.context,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "IntentRequest") {
onIntent(event.request,
event.session,
event.context,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "Display.ElementSelected") {
console.log("Display Element Selected Event");
onSessionEnded(event.request, event.session);
context.succeed();
} else if (event.request.type === "SessionEndedRequest") {
console.log("Session Ended: " + JSON.stringify(event.request));
onSessionEnded(event.request, event.session);
context.succeed();
// this was added to handle the Can Fulfill Intent Request feature
} else if (event.request.type === "CanFulfillIntentRequest") {
console.log("can fulfill request received ");
onFulfillRequest(event.request, event.session, event.context,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildNoSessionResponse(speechletResponse));
});
}
} catch (e) {
context.fail("Exception: " + e);
}
};
/**
* Called when the session starts.
*/
function onSessionStarted(sessionStartedRequest, session) {
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId +
", sessionId=" + session.sessionId);
}
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, context, callback) {
console.log("onLaunch requestId=" + launchRequest.requestId + ", sessionId=" + session.sessionId);
// need to determine which type of device is being used to remain backward compatibility
var device = {};
if (context) {
console.log("Supported Interfaces:" + JSON.stringify(context.System.device.supportedInterfaces));
if (context.System.device.supportedInterfaces.Display) {
device.type = "Show";
} else {
device.type = "Legacy";
}
} else {
console.log("Test Dummy - no device info");
device.type = "Test";
}
// Dispatch to your skill's launch.
getWelcomeResponse(session, device, callback);
}
// Called when Alexa is polling for more detail
function onFulfillRequest(intentRequest, session, context, callback) {
console.log("processing on fulfillment request.");
handleCanFulfillRequest(intentRequest, session, callback);
}
/**
* Called when the user specifies an intent for this skill. This drives
* the main logic for the function.
*/
function onIntent(intentRequest, session, context, callback) {
console.log("onIntent requestId=" + intentRequest.requestId +
", sessionId=" + session.sessionId);
// need to determine which type of device is being used to remain backward compatibility
var device = {};
if (context) {
console.log("Supported Interfaces:" + JSON.stringify(context.System.device.supportedInterfaces));
if (context.System.device.supportedInterfaces.Display) {
device.type = "Show";
} else {
device.type = "Legacy";
}
} else {
console.log("Test Dummy - no device info");
device.type = "Test";
}
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
country = intentRequest.locale;
dialogState = intentRequest.dialogState;
// Dispatch to the individual skill handlers
if ("ListStormNames" === intentName) {
getStormNames(intent, session, device, callback);
} else if ("SetOceanPreference" === intentName) {
setOceanInSession(intent, session, device, callback);
} else if ("StormsFromPriorYears" == intentName && intent.slots.Date.value == 2020) {
getCurrentYearHistory(intent, session, device, callback);
} else if ("StormsFromPriorYears" == intentName && intent.slots.Date.value != 2020) {
getWhichYear(intent, session, device, callback);
} else if ("ThisYearsStorms" === intentName || "AMAZON.YesIntent" === intentName) {
console.log("Intent Name: " + intentName + " From: " + country);
getThisYearStorm(intent, session, device, callback);
} else if ("CurrentYearHistory" === intentName) {
getCurrentYearHistory(intent, session, device, callback);
} else if ("CompleteListOfStorms" === intentName) {
getCompleteList(intent, session, device, callback);
} else if ("GetStormDetail" === intentName) {
getStormDetail(intent, session, device, callback);
} else if ("GiveStormFact" === intentName || "AMAZON.MoreIntent" === intentName) {
getStormFact(intent, session, device, callback);
} else if ("StormStrength" === intentName) {
getHurricaneStrength(intent, session, device, callback);
} else if ("TropicalStormStrength" === intentName) {
getTropicalStormStrength(intent, session, device, callback);
} else if ("DifferenceStorms" === intentName) {
getDifferenceStorms(intent, session, device, callback);
} else if ("StormDistance" === intentName) {
getStormDistance(intent, dialogState, session, device, callback);
} else if ("AMAZON.StartOverIntent" === intentName || "AMAZON.PreviousIntent" === intentName) {
getWelcomeResponse(session, device, callback);
} else if ("AMAZON.HelpIntent" === intentName) {
getHelpResponse(device, callback);
} else if ("AMAZON.RepeatIntent" === intentName || "AMAZON.NextIntent" === intentName) {
getWelcomeResponse(session, device, callback);
} else if ("AMAZON.CancelIntent" === intentName || "AMAZON.NoIntent" === intentName) {
handleSessionEndRequest(session, device, callback);
} else if ("AMAZON.StopIntent" === intentName) {
handleSessionStopRequest(session, device, callback);
} else {
throw "Invalid intent";
}
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session) {
console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId +
", sessionId=" + session.sessionId);
}
// --------------- Base Functions that are invoked based on standard utterances -----------------------
// this is the function that gets called to format the response to the user when they first boot the app
function getWelcomeResponse(session, device, callback) {
var sessionAttributes = {};
sessionAttributes.lastRequest = "Welcome";
var shouldEndSession = false;
var cardTitle = "Welcome to Hurricane Center";
var cardOutput = "No Current Storms in either the Atlantic or Pacific Ocean.";
var speechOutput = "Welcome to the Hurricane Center, the best source for information " +
"related to tropical storms, past or present. There are no active tropical storms " +
"right now, but if you would like to learn more about storms, please say something " +
"like tell me a storm fact.";
var repromptText = "Please tell me how I can help you by saying phrases like, " +
"list storm names or storm history for 2013.";
var activeStorms = false;
var activeStormData = [];
console.log("Get Welcome Message - Device Type: " + JSON.stringify(device.type));
// check current data to see if there are active storms and if so change the welcome message
var s3 = new aws.S3();
var getParams = {Bucket : stormDataBucket,
Key : 'currStorms.json'};
s3.getObject(getParams, function(err, data) {
if(err)
console.log('Error getting history data : ' + err);
else {
var returnData = eval('(' + data.Body + ')');
//
if (returnData[0].activeStorms === false) {
console.log('welcome message - no active storms');
} else {
console.log('there is an active storm: ' + JSON.stringify(returnData[0].storms));
// parse through the array and build an appropriate welcome message
speechOutput = "Welcome to the Hurricane Center. ";
var storms = returnData[0].storms;
var activeStormAtlantic = false;
var activeStormPacific = false;
var activeStorms = 0;
var currentStormLocation = "";
var currentStormLoc = [];
var activeStormNames = []
// rotate through the array of current storm data to determine where the active storms are
for (i = 0; i < storms.length; i++) {
//console.log('storm data: ' + JSON.stringify(returnData[0].storms[i]));
if (storms[i].formed) {
activeStormNames.push(returnData[0].storms[i].stormType + " " + returnData[0].storms[i].stormName);
activeStorms++;
currentStormLocation = returnData[0].storms[i].location.proximity;
currentStormLoc.push(returnData[0].storms[i].location.proximity);
if (storms[i].ocean == "Atlantic")
activeStormAtlantic = true;
else
activeStormPacific = true;
var activeStormDetail = {};
activeStormDetail.stormType = returnData[0].storms[i].stormType;
activeStormDetail.stormName = returnData[0].storms[i].stormName;
activeStormDetail.ocean = returnData[0].storms[i].ocean;
activeStormDetail.locationLat = returnData[0].storms[i].location.lat;
activeStormDetail.locationLong = returnData[0].storms[i].location.long;
activeStormDetail.peakWinds = returnData[0].storms[i].peakWinds;
activeStormDetail.pressure = returnData[0].storms[i].pressure;
activeStormData.push(activeStormDetail);
}
}
console.log("total number of storms: " + activeStorms);
console.log("storm names: " + JSON.stringify(activeStormNames));
//speechOutput = speechOutput + "<break time=\"1s\"/>";
// build a different message depending on where the oceans are at and how many are active
if (activeStormAtlantic === true && activeStormPacific === false) {
if (activeStorms === 1) {
console.log("Current Location: " + currentStormLoc[0]);
speechOutput = speechOutput + activeStormNames[0] + " is currently active, approximately " + currentStormLoc[0];
} else if (activeStorms === 2) {
speechOutput = speechOutput + activeStormNames[0] + " is currently active, approximately " + currentStormLoc[0] + ". " +
activeStormNames[1] + " is currently active, approximately " + currentStormLoc[1] + " ";
} else if (activeStorms === 3) {
speechOutput = speechOutput + activeStormNames[0] + " is currently active, approximately " + currentStormLoc[0] + ". " +
activeStormNames[1] + " is currently active, approximately " + currentStormLoc[1] + ". " +
activeStormNames[2] + " is currently active, approximately " + currentStormLoc[2] + ". ";
} else {
speechOutput = speechOutput + "There are currently " + activeStorms + " storms active in the Atlantic Ocean. ";
}
cardOutput = speechOutput;
} else if (activeStormAtlantic === false && activeStormPacific === true) {
if (activeStorms === 1) {
speechOutput = speechOutput + activeStormNames[0] + " is currently active in the Pacific Ocean. ";
} else if (activeStorms === 2) {
speechOutput = speechOutput + activeStormNames[0] + " and " + activeStormNames[1] + " are currently " +
"active in the Pacific Ocean. ";
} else {
speechOutput = speechOutput + "There are currently " + activeStorms + " storms active in the Pacific Ocean. ";
}
cardOutput = speechOutput;
} else if (activeStormAtlantic === true && activeStormPacific === true) {
if (activeStorms === 2) {
speechOutput = speechOutput + activeStormNames[0] + " is currently active, approximately " + currentStormLoc[0] + ". " +
activeStormNames[1] + " is currently active, approximately " + currentStormLoc[1] + ".";
cardOutput = speechOutput;
} else if (activeStorms === 3) {
speechOutput = speechOutput + activeStormNames[0] + " is currently active, approximately " + currentStormLoc[0] + ". " +
activeStormNames[1] + " is currently active, approximately " + currentStormLoc[1] + ". " +
activeStormNames[2] + " is currently active, approximately " + currentStormLoc[2];
cardOutput = speechOutput;
} else {
speechOutput = speechOutput + "There are " + activeStorms + " active storms, including " +
activeStormNames[0] + " and " + activeStormNames[1] + ". ";
cardOutput = "Current Storms\n";
for (var k = 0; k < activeStormNames.length; k++) {
cardOutput = cardOutput + activeStormNames[k] + " " + currentStormLoc[k] + "\n";
}
}
}
speechOutput = speechOutput + "Please say something like, tell me about " + activeStormNames[0] + ", to hear the forecast on a single storm. " +
"Or say yes to hear them all.";
repromptText = "There are currently active tropical storms. To hear specific forecast details about them, just say yes.";
}
}
//if (device.type === "Legacy") {
if (device.type === "Legacy" || activeStormData.length === 0) {
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, cardOutput, repromptText, device, shouldEndSession));
} else {
console.log("Rendering for Echo Show - Active Storms: " + JSON.stringify(activeStorms));
callback(sessionAttributes,
buildVisualListResponse(cardTitle, speechOutput, cardOutput, repromptText, activeStormData, shouldEndSession));
}
});
}
// this is the function that handles broad requests coming in natively from Alexa
function handleCanFulfillRequest(intentRequest, session, callback) {
var sessionAttributes = {};
const intentName = intentRequest.intent.name;
console.log("Can Fulfill Request for intent name:" + intentName);
// depending on the intent determine if a response can be provided
if ("ListStormNames" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", null));
} else if ("SetOceanPreference" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("NO", buildSlotDetail("Ocean", intentRequest.intent.slots)));
} else if ("StormsFromPriorYears" == intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", buildSlotDetail("Date", intentRequest.intent.slots)));
} else if ("ThisYearsStorms" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", null));
} else if ("CurrentYearHistory" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", null));
} else if ("CompleteListOfStorms" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", null));
} else if ("GetStormDetail" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", buildSlotDetail("Storm", intentRequest.intent.slots)));
} else if ("GiveStormFact" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", null));
} else if ("StormStrength" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", buildSlotDetail("HurricaneStrength", intentRequest.intent.slots)));
} else if ("TropicalStormStrength" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", null));
} else if ("DifferenceStorms" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", null));
} else if ("StormDistance" === intentName) {
callback(sessionAttributes,
buildFulfillQueryResponse("YES", buildSlotDetail("StormDistance", intentRequest.intent.slots)));
} else {
// this handles all the other scenarios - i.e. Scroll Down Intent - that make no sense
console.log("No match on intent name: " + intentName);
callback(sessionAttributes, buildFulfillQueryResponse("NO", null));
}
}
// this is the function that gets called to format the response to the user when they ask for help
function getHelpResponse(device, callback) {
var sessionAttributes = {};
var cardTitle = "Help";
// this will be what the user hears after asking for help
var speechOutput = "The Hurricane Center provides information about tropical storms. " +
"If you would like to hear about storms from this year, say What are storms from this year. " +
"For information related to storms from prior years, please say What is the storm history " +
"for a specific year. If you would like to hear a fact about tropical storms, please say " +
"tell me a storm fact.";
console.log("Get Help Response");
// if the user still does not respond, they will be prompted with this additional information
var repromptText = "Please tell me how I can help you by saying phrases like, " +
"list storm names or storm history.";
var shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, shouldEndSession));
}
// this is the function that describes the difference between a hurricane and tropical storm
function getDifferenceStorms(intent, session, device, callback) {
var sessionAttributes = {};
var cardTitle = "Storm Strength";
console.log("Get difference betwen tropical storm and a hurricane requested.");
const speechOutput = "Peak wind speed is what differentiates storm types. " +
"A tropical storm has wind speeds ranging from 39 to 73 miles per hour. " +
"Anything at 74 miles per hour and above is classified as a hurricane. " +
"Hurricanes start off as tropical storms, and if conditions are favorable, " +
"the winds gain in strength and the storm grows into a hurricane. " +
"Weaker storms that have wind speeds below 39 miles per hour are called Tropical Depressions. " +
"If you would like to know about different strengths of hurricanes, just say something like, " +
"How strong are the winds on a category three hurricane?";
const cardOutput = "Tropical Storms have wind speed from 39 to 73 miles per hour.";
// if the user still does not respond, they will be prompted with this additional information
const repromptText = "Please tell me how I can help you by saying phrases like, " +
"list storm names or storm history.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, cardOutput, repromptText, device, false));
}
// this is the function that handles describing how far away a particular storm is from a location
function getStormDistance(intent, dialogState, session, device, callback) {
var sessionAttributes = {};
console.log("Get location question for a hurricane or tropical storm.");
var cardTitle = "Storm Distance";
var speechOutput = "";
// this will change based on the current storm
const currStormName = "hector";
const currStormStatus = "Hurricane";
// if the user still does not respond, they will be prompted with this additional information
const repromptText = "Please tell me how I can help you by saying phrases like, " +
"list storm names or storm history.";
if (intent.slots.Storm.value) {
if (intent.slots.Storm.value.toLowerCase() === currStormName) {
// check current data to see if there are active storms and if so change the welcome message
console.log("Checking on status for Hurricane Hector");
var s3 = new aws.S3();
var getParams = {Bucket : stormDataBucket, Key : 'currStorms.json'};
s3.getObject(getParams, function(err, data) {
console.log("retrieving S3 object.");
if(err) {
console.log('Error getting history data : ' + err);
speeechOutput = "Sorry, I'm having trouble accessing storm data right now.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, false));
} else {
var returnData = eval('(' + data.Body + ')');
// sucessfully returned s3 object - now see if there are active storms
if (returnData[0].activeStorms === false) {
console.log('data retrieved - no active storms');
speechOutput = "Sorry, Hurricane Hector is no longer active in the Pacific Ocean.";
} else {
console.log('there is an active storm: ' + JSON.stringify(returnData[0].storms));
var storms = returnData[0].storms;
console.log('storm location: ' + JSON.stringify(storms[0].location));
var locationData = storms[0].location;
speechOutput = storms[0].stormType + " " + storms[0].stormName + " is " + locationData.distance +
" miles from " + locationData.name + ". ";
speechOutput = speechOutput + "Would you like the complete forecast for " +
storms[0].stormType + " " + storms[0].stormName + "?";
}
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, false));
}
});
} else {
speechOutput = "Sorry, " + intent.slots.Storm.value + " is not currently an active storm. " +
"If you would like information on Hurricane Hector, please let me know.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, false));
}
} else {
speechOutput = "Which storm are you trying to find details on? If you would like information " +
"on Hurricane Hector, please let me know.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, false));
}
}
// this is the function that handles describing a tropical storm
function getTropicalStormStrength(intent, session, device, callback) {
var sessionAttributes = {};
var cardTitle = "Storm Strength";
console.log("Get Tropical Storm Strength requested.");
const speechOutput = "A tropical storm has wind speeds ranging from 39 to 73 miles per hour. " +
"Anything at 74 miles per hour and above is classified as a hurricane. " +
"If you would like to know about different strengths of hurricanes, just say something like, " +
"How strong are the winds on a category three hurricane?";
const cardOutput = "Tropical Storms have wind speed from 39 to 73 miles per hour.";
// if the user still does not respond, they will be prompted with this additional information
const repromptText = "Please tell me how I can help you by saying phrases like, " +
"list storm names or storm history.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, cardOutput, repromptText, device, false));
}
// this is the function that handles how strong a hurricane winds are
function getHurricaneStrength(intent, session, device, callback) {
var sessionAttributes = {};
var cardTitle = "Storm Strength";
console.log("Get Hurricane Strength requested.");
var speechOutput = "A category x hurricane is y miles per hour.";
// check the slot data and provide a response accordingly
if (intent.slots.HurricaneStrength.value === "1") {
cardOutput = "A category one hurricane is between 74 and 95 miles per hour.";
speechOutput = "A category one hurricane is between 74 and 95 miles per hour. " +
"What other information can I help you on? For example, ask me for a storm fact.";
} else if (intent.slots.HurricaneStrength.value === "2") {
cardOutput = "A category two hurricane is between 96 and 110 miles per hour.";
speechOutput = "A category two hurricane is between 96 and 110 miles per hour. " +
"What other information can I help you on? For example, ask me for a storm fact.";
} else if (intent.slots.HurricaneStrength.value === "3") {
cardOutput = "A category three hurricane is between 111 and 129 miles per hour.";
speechOutput = "A category three hurricane is between 111 and 129 miles per hour. " +
"What other information can I help you on? For example, ask me for a storm fact.";
} else if (intent.slots.HurricaneStrength.value === "4") {
cardOutput = "A category four hurricane is between 130 and 156 miles per hour.";
speechOutput = "A category four hurricane is between 130 and 156 miles per hour. " +
"What other information can I help you on? For example, ask me for a storm fact.";
} else if (intent.slots.HurricaneStrength.value === "5") {
cardOutput = "A category five hurricane is 157 miles per hour and above.";
speechOutput = "A category five hurricane is 157 miles per hour and above. " +
"What other information can I help you on? For example, ask me for a storm fact.";
} else {
console.log("Invalid hurricane level provided");
cardOutput = "The Saffir-Simpson Scale is a range from one to five.";
speechOutput = "Sorry, hurricanes are classified in a range from one to five. " +
"Please provide me the hurricane strength by saying something like, " +
"How strong are the winds on a category three hurricane?";
}
// if the user still does not respond, they will be prompted with this additional information
const repromptText = "Please tell me how I can help you by saying phrases like, " +
"list storm names or storm history.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, cardOutput, repromptText, device, false));
}
// this is the function that gets called to format the response when the user is done
function handleSessionEndRequest(session, device, callback) {
var cardTitle = "Thanks for using Hurricane Center";
var speechOutput = "Thank you for using Hurricane Center. ";
//"If this skill was useful, please " +
//"let others know by writing a review in the Alexa app.";
// Setting this to true ends the session and exits the skill.
var shouldEndSession = true;
console.log("Prior Intent:" + JSON.stringify(session.attributes));
console.log("User Terminated Session");
callback({}, buildSpeechletResponse(cardTitle, speechOutput, speechOutput, null, device, shouldEndSession));
}
// this is the function that gets called to format the response when the user requests stop
function handleSessionStopRequest(session, device, callback) {
var sessionAttributes = {};
sessionAttributes.lastRequest = "Stop";
const cardTitle = "Thanks for using Hurricane Center";
var speechOutput = "Okay, is there any other information I can provide you about tropical storms? " +
"For example, you can say, tell me a storm fact.";
const repromptText = "What other information would you like on tropical storms? For example, you can " +
"say, What are the storms for this year?";
let shouldEndSession = false;
console.log("Prior Intent:" + JSON.stringify(session.attributes));
if (session.attributes) {
if (session.attributes.lastRequest === "Stop") {
shouldEndSession = true;
console.log("User Requested Stopping Twice. End Session.");
speechOutput = "Thanks for using Hurricane Center.";
}
}
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, shouldEndSession));
}
// Sets the ocean in the session and prepares the speech to reply to the user.
function setOceanInSession(intent, session, device, callback) {
var cardTitle = "Hurricane Center";
var preferredOcean = intent.slots.Ocean;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = false;
var speechOutput = "";
console.log("Setting ocean preference");
console.log("preferred ocean : " + JSON.stringify(preferredOcean));
console.log("Session data: " + JSON.stringify(session));
if ("Atlantic" == preferredOcean.value || "pacific" == preferredOcean.value) {
console.log("setting ocean preference");
var ocean = preferredOcean.value.charAt(0).toUpperCase() + preferredOcean.value.slice(1);
sessionAttributes = storeOceanAttributes(ocean);
// first make sure that there are session attributes to check
if (session.attributes) {
if (session.attributes.request === "GetStormNames") {
console.log("Reading storm names from prior request");
speechOutput = speechOutput + "The first five storm names for the " + ocean + " Ocean will be ";
if (ocean == "Atlantic")
currentYearStorms = atlanticStorms[0];
else
currentYearStorms = pacificStorms[0];
console.log("current year storms: " + JSON.stringify(currentYearStorms));
speechOutput = speechOutput +
currentYearStorms.stormNames[0] + ", " +
currentYearStorms.stormNames[1] + ", " +
currentYearStorms.stormNames[2] + ", " +
currentYearStorms.stormNames[3] + ", and " +
currentYearStorms.stormNames[4] + ". ";
cardOutput = ocean + " Ocean\n" +
currentYearStorms.stormNames[0] + "\n" +
currentYearStorms.stormNames[1] + "\n" +
currentYearStorms.stormNames[2] + "\n" +
currentYearStorms.stormNames[3] + "\n" +
currentYearStorms.stormNames[4] + "\n";
speechOutput = speechOutput + "If you would like the complete list, say complete list of this years storms.";
repromptText = "Would you like more information? If you would like a complete list of this years " +
"storms, say Complete list of this years storms. If you would like storm history from prior years " +
"please say Storm History.";
} else {
speechOutput = "Okay. My understanding is that you want information on the " + ocean + " ocean. " +
"Would you like to hear about this years storms, or storms from prior years?";
repromptText = "Here is the storm information for the " + ocean + " ocean.";
}
} else {
speechOutput = "Okay. My understanding is that you want information on the " + ocean + " ocean. " +
"Would you like to hear about this years storms, or storms from prior years?";
repromptText = "Here is the storm information for the " + ocean + " ocean.";
}
} else {
speechOutput = "I'm not sure which ocean you are looking for. Please try again by " +
"saying Atlantic or Pacific.";
repromptText = "I'm not sure which ocean you want information on. " +
"Please say either Atlantic or Pacific.";
}
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, shouldEndSession));
}
function storeOceanAttributes(ocean) {
return {
ocean: ocean
};
}
// Sets the ocean name in case it has not done so already
function getStormNames(intent, session, device, callback) {
var oceanPreference;
// Setting repromptText to null signifies that we do not want to reprompt the user.
// If the user does not respond or says something that is not understood, the session
// will end.
var sessionAttributes = {};
var shouldEndSession = false;
var speechOutput = "";
var cardTitle = "Hurricane Center";
console.log("Get Storm Names");
// check session data to see if ocean preference has been set
if (session.attributes) {
oceanPreference = session.attributes.ocean;
}
// if a preference has been set, read storm names - else prompt to provide which ocean
if (oceanPreference) {
speechOutput = speechOutput + "The first five storm names for the " + oceanPreference + " Ocean will be ";
if (oceanPreference == "Atlantic")
currentYearStorms = atlanticStorms[0];
else
currentYearStorms = pacificStorms[0];
console.log("current year storms: " + JSON.stringify(currentYearStorms));
speechOutput = speechOutput +
currentYearStorms.stormNames[0] + ", " +
currentYearStorms.stormNames[1] + ", " +
currentYearStorms.stormNames[2] + ", " +
currentYearStorms.stormNames[3] + ", and " +
currentYearStorms.stormNames[4] + ". ";
cardOutput = oceanPreference + " Ocean\n" +
currentYearStorms.stormNames[0] + "\n" +
currentYearStorms.stormNames[1] + "\n" +
currentYearStorms.stormNames[2] + "\n" +
currentYearStorms.stormNames[3] + "\n" +
currentYearStorms.stormNames[4] + "\n";
speechOutput = speechOutput + "If you would like the complete list, say complete list of this years storms.";
repromptText = "Would you like more information? If you would like a complete list of this years " +
"storms, say Complete list of this years storms. If you would like storm history from prior years " +
"please say Storm History.";
sessionAttributes = storeOceanAttributes(oceanPreference);
} else {
sessionAttributes.request = "GetStormNames";
speechOutput = "Which ocean would you like details for, please say, Atlantic Ocean or Pacific Ocean";
repromptText = "Please let me know which ocean you would like details " +
"by saying Atlantic Ocean or Pacific Ocean";
}
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, shouldEndSession));
}
// This highlights the summary of storms for the current year - 2020
function getCurrentYearHistory(intent, session, device, callback) {
var oceanPreference;
var sessionAttributes = {};
var shouldEndSession = false;
var cardTitle = "Storm History for 2020";
console.log("Get Current Year History");
var atlanticTropStorms = 0;
var atlanticHurricanes = 0;
var pacificTropStorms = 0;
var pacificHurricanes = 0;
// rotate through all of the current storms and count categories
for (i = 0; i < currYearStormArray.length; i++) {
if(currYearStormArray[i].ocean == "Atlantic") {
if(currYearStormArray[i].level == "Hurricane") {
atlanticHurricanes += 1;
} else {
atlanticTropStorms += 1;
}
} else {
if(currYearStormArray[i].level == "Hurricane") {
pacificHurricanes += 1;
} else {
pacificTropStorms += 1;
}
}
}
// format response by merging the summary from the array with natural language
var speechOutput = "So far this year there have been " + atlanticHurricanes +
" hurricanes in the Atlantic and " + pacificHurricanes + " in the Pacific. ";
// var speechOutput = "It is still early in the season, and there have been no hurricanes " +
// "in either the Atlantic or Pacific Oceans. " +
// "If you would like to hear about current active storms please say " +
// "Current Storms and I will give a detailed overview of what is currently active. ";
speechOutput = speechOutput + "There have been " + atlanticTropStorms +
" Tropical Storms in the Atlantic and " + pacificTropStorms + " in the Pacific. " +
"If you want information on current storms, please say List Current Storms. ";
// " I have detailed information about Hurricanes Harvey, Irma, and Maria. If you would like details " +
// " please say something like, Tell me about Hurricane Harvey. ";
var cardOutput = "Atlantic Ocean\n" + atlanticHurricanes +
" Hurricanes\n" + atlanticTropStorms +
" Tropical Storms\n" + "Pacific Ocean\n" + pacificHurricanes +
" Hurricanes\n" + pacificTropStorms +
" Tropical Storms\n" +
" Major Storms\n";
const repromptText = "If you would like information about current storms, please " +
"say List Current Storms.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, cardOutput, repromptText, device, shouldEndSession));
}
// This function returns storm history. It checks to make sure that the year one where data
// can be provided back, and if so pulls it from an S3 bucket. The data is parsed and a response
// is formatted
function getWhichYear(intent, session, device, callback) {
var oceanPreference;
var shouldEndSession = false;
var sessionAttributes = {};
var speechOutput = "";
var cardOutput = "";
var repromptText = "";
var cardTitle = "Storm History";
console.log("Retrieve Storm History");
// get what ocean has been requested
if (session.attributes) {
oceanPreference = session.attributes.ocean;
sessionAttributes = storeOceanAttributes(oceanPreference);
//console.log('set default');
} else {
oceanPreference = "Atlantic";
sessionAttributes = storeOceanAttributes(oceanPreference);
}
//console.log("session attributes: " + JSON.stringify(session.attributes));
//console.log("intent attributes: " + JSON.stringify(intent.slots.Date));
if (intent.slots.Date.value) {
requestYear = intent.slots.Date.value;
cardTitle = "Storm History for " + requestYear;
if (requestYear > 1990 && requestYear < 2020) {
var s3 = new aws.S3();
if (oceanPreference == null) {
oceanPreference = "Atlantic";
}
var oceanObject = 'stormHistory' + oceanPreference + '.json';
//var oceanObject = 'stormHistoryAtlTest.json';
var getParams = {Bucket : stormDataBucket,
Key : oceanObject };
console.log('attempt to pull an object from an s3 bucket' + JSON.stringify(getParams));
s3.getObject(getParams, function(err, data) {
if(err)
console.log('Error getting history data : ' + err)
else {
// data retrieval was successfull - now parse through it and provide back in the reponse.
var historyArray = eval('(' + data.Body + ')');
// parse through the history and find the data for the requested year
for (j = 0; j < historyArray.length; j++) {
//console.log('year: ' + historyArray[j].stormYear);
if (historyArray[j].stormYear == requestYear)
var stormHistoryArray = historyArray[j];
}
// build the response back based on stringing together all information for the year
var stormReading = {};
var moreStormData = [];
speechOutput = 'In the ' + oceanPreference + ' ocean ' +
'there were ' + stormHistoryArray.storms.length +
' storms in ' + stormHistoryArray.stormYear + '. ';
var hurricaneNames = [];
var tropicalStormNames = [];
// sort the storm names into separate arrays
for (i = 0; i < stormHistoryArray.storms.length; i++) {
if (stormHistoryArray.storms[i].stormType == "Hurricane")
hurricaneNames.push(stormHistoryArray.storms[i].stormName);
else
tropicalStormNames.push(stormHistoryArray.storms[i].stormName);
if (stormHistoryArray.storms[i].scale != null) {
console.log('more data available on ' + stormHistoryArray.storms[i].stormName);
moreStormData.push(stormHistoryArray.storms[i]);
}
}
// now go through each array and create sentance structure
speechOutput = speechOutput + "The hurricane names are ";
cardOutput = cardOutput + "Hurricanes:\n";
for (i = 0; i < hurricaneNames.length; i++) {
speechOutput = speechOutput + hurricaneNames[i] + ", ";
cardOutput = cardOutput + hurricaneNames[i] + "\n";
}
speechOutput = speechOutput + "The tropical storm names are ";
cardOutput = cardOutput + "\nTropical Storms:\n";
for (i = 0; i < tropicalStormNames.length; i++) {
speechOutput = speechOutput + tropicalStormNames[i] + ", ";
cardOutput = cardOutput + tropicalStormNames[i] + "\n";
}
for (i = 0; i < moreStormData.length; i++) {
cardOutput = cardOutput + 'More data available on ' + stormHistoryArray.storms[i].stormName + "\n";
console.log('storm data on : ' + JSON.stringify(moreStormData[i]));
}
if (moreStormData.length == 0) {
speechOutput = speechOutput + " Would you like to hear information about another year?";
repromptText = "Would you like to hear information about another year? If so, please say " +
"something like tell me about storms from 2007.";
} else {
speechOutput = speechOutput + " I have more information on " + moreStormData[0].stormType +
" " + moreStormData[0].stormName + ". Would you like to hear it? If so, please say " +
"tell me more about " + moreStormData[0].stormName + ".";
repromptText = "Would you like to hear information about another year? If so, please say " +
"something like tell me about storms from 2007.";
}
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, cardOutput, repromptText, device, shouldEndSession));
};
});
} else {
console.log('Year selected for storm history outside of available data');
speechOutput = "Sorry, I don't have information for " + requestYear + ". " +
"I do have information on storms between 1991 and 2020. Please let me " +
"know which year I can provide within that range.";
repromptText = "Please state a year between 1991 and 2020. " +
"For example, say Storms for 2012.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, shouldEndSession));
};
}
else {
console.log("No year provided. Reprompt user to select a year for storm history");
speechOutput = "Which year would you like storm history for? If you would like me to check for " +
"storms that are currently active, please say something like current storms. ";
repromptText = "Please state a year you would like to hear storm history for. " +
"For example say Storms for 2012, or for storms that are currently active, say current storms. ";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, speechOutput, repromptText, device, shouldEndSession));
}
}
// this function gets information about this years storms
function getThisYearStorm(intent, session, device, callback) {
var oceanPreference;
var repromptText = "";
var shouldEndSession = false;
var sessionAttributes = {};
var speechOutput = "";
var cardOutput = "";
var cardTitle = "Storm Information for 2020";
if (session.attributes) {
oceanPreference = session.attributes.ocean;
sessionAttributes = storeOceanAttributes(oceanPreference);
}
console.log("get information about this years storms");
// first check if there are any active storms, and if so provide current details
var s3 = new aws.S3();
var getParams = {Bucket : stormDataBucket,
Key : 'currStorms.json'};
//console.log('attempt to pull an object from an s3 bucket' + JSON.stringify(getParams));
s3.getObject(getParams, function(err, data) {
if(err)
console.log('Error getting history data : ' + err);
else {
var returnData = eval('(' + data.Body + ')');
//console.log('Successfully retrieved history data : ' + data.Body);
//
if (returnData[0].activeStorms === false) {
// if there are no active storms, provide what the names will be
speechOutput = "There aren't any active tropical storms in either ocean right now. ";
if (oceanPreference == null) {
// this logic is processed in case that there is no ocean preference set
speechOutput = speechOutput + "If you would like to hear this years storm names " +
"please let me know which set by saying Atlantic Ocean or Pacific Ocean";