1
+ // This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
2
+ // Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
3
+ // session persistence, api calls, and more.
4
+ const Alexa = require ( 'ask-sdk-core' ) ;
5
+
6
+ const LaunchRequestHandler = {
7
+ canHandle ( handlerInput ) {
8
+ return handlerInput . requestEnvelope . request . type === 'LaunchRequest' ;
9
+ } ,
10
+ handle ( handlerInput ) {
11
+ const speechText = 'Welcome, you can say Hello or Help. Which would you like to try?' ;
12
+ return handlerInput . responseBuilder
13
+ . speak ( speechText )
14
+ . reprompt ( speechText )
15
+ . getResponse ( ) ;
16
+ }
17
+ } ;
18
+
19
+ const HelloWorldIntentHandler = {
20
+ canHandle ( handlerInput ) {
21
+ return handlerInput . requestEnvelope . request . type === 'IntentRequest'
22
+ && handlerInput . requestEnvelope . request . intent . name === 'HelloWorldIntent' ;
23
+ } ,
24
+ handle ( handlerInput ) {
25
+ const speechText = 'Hello World!' ;
26
+ return handlerInput . responseBuilder
27
+ . speak ( speechText )
28
+ //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
29
+ . getResponse ( ) ;
30
+ }
31
+ } ;
32
+
33
+ const HelpIntentHandler = {
34
+ canHandle ( handlerInput ) {
35
+ return handlerInput . requestEnvelope . request . type === 'IntentRequest'
36
+ && handlerInput . requestEnvelope . request . intent . name === 'AMAZON.HelpIntent' ;
37
+ } ,
38
+ handle ( handlerInput ) {
39
+ const speechText = 'You can say hello to me! How can I help?' ;
40
+
41
+ return handlerInput . responseBuilder
42
+ . speak ( speechText )
43
+ . reprompt ( speechText )
44
+ . getResponse ( ) ;
45
+ }
46
+ } ;
47
+
48
+ const CancelAndStopIntentHandler = {
49
+ canHandle ( handlerInput ) {
50
+ return handlerInput . requestEnvelope . request . type === 'IntentRequest'
51
+ && ( handlerInput . requestEnvelope . request . intent . name === 'AMAZON.CancelIntent'
52
+ || handlerInput . requestEnvelope . request . intent . name === 'AMAZON.StopIntent' ) ;
53
+ } ,
54
+ handle ( handlerInput ) {
55
+ const speechText = 'Goodbye!' ;
56
+ return handlerInput . responseBuilder
57
+ . speak ( speechText )
58
+ . getResponse ( ) ;
59
+ }
60
+ } ;
61
+
62
+ const FallbackIntentHandler = {
63
+ canHandle ( handlerInput ) {
64
+ return handlerInput . requestEnvelope . request . type === 'IntentRequest'
65
+ && handlerInput . requestEnvelope . request . intent . name === 'AMAZON.FallbackIntent' ;
66
+ } ,
67
+ handle ( handlerInput ) {
68
+ const speechText = `Sorry, I don't know about that. Please try again.` ;
69
+
70
+ return handlerInput . responseBuilder
71
+ . speak ( speechText )
72
+ . reprompt ( speechText )
73
+ . getResponse ( ) ;
74
+ }
75
+ } ;
76
+
77
+ const SessionEndedRequestHandler = {
78
+ canHandle ( handlerInput ) {
79
+ return handlerInput . requestEnvelope . request . type === 'SessionEndedRequest' ;
80
+ } ,
81
+ handle ( handlerInput ) {
82
+ // Any cleanup logic goes here.
83
+ return handlerInput . responseBuilder . getResponse ( ) ;
84
+ }
85
+ } ;
86
+
87
+ // The intent reflector is used for interaction model testing and debugging.
88
+ // It will simply repeat the intent the user said. You can create custom handlers
89
+ // for your intents by defining them above, then also adding them to the request
90
+ // handler chain below.
91
+ const IntentReflectorHandler = {
92
+ canHandle ( handlerInput ) {
93
+ return handlerInput . requestEnvelope . request . type === 'IntentRequest' ;
94
+ } ,
95
+ handle ( handlerInput ) {
96
+ const intentName = handlerInput . requestEnvelope . request . intent . name ;
97
+ const speechText = `You just triggered ${ intentName } ` ;
98
+
99
+ return handlerInput . responseBuilder
100
+ . speak ( speechText )
101
+ //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
102
+ . getResponse ( ) ;
103
+ }
104
+ } ;
105
+
106
+ // Generic error handling to capture any syntax or routing errors. If you receive an error
107
+ // stating the request handler chain is not found, you have not implemented a handler for
108
+ // the intent being invoked or included it in the skill builder below.
109
+ const ErrorHandler = {
110
+ canHandle ( ) {
111
+ return true ;
112
+ } ,
113
+ handle ( handlerInput , error ) {
114
+ console . log ( `~~~~ Error handled: ${ error . message } ` ) ;
115
+ const speechText = `Sorry, there was an error. Please try again.` ;
116
+
117
+ return handlerInput . responseBuilder
118
+ . speak ( speechText )
119
+ . reprompt ( speechText )
120
+ . getResponse ( ) ;
121
+ }
122
+ } ;
123
+
124
+ // This handler acts as the entry point for your skill, routing all request and response
125
+ // payloads to the handlers above. Make sure any new handlers or interceptors you've
126
+ // defined are included below. The order matters - they're processed top to bottom.
127
+ exports . handler = Alexa . SkillBuilders . custom ( )
128
+ . addRequestHandlers (
129
+ LaunchRequestHandler ,
130
+ HelloWorldIntentHandler ,
131
+ HelpIntentHandler ,
132
+ CancelAndStopIntentHandler ,
133
+ FallbackIntentHandler ,
134
+ SessionEndedRequestHandler ,
135
+ IntentReflectorHandler ) // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
136
+ . addErrorHandlers (
137
+ ErrorHandler )
138
+ . lambda ( ) ;
0 commit comments