11# -*- coding: utf-8 -*-
22
3- # This sample demonstrates handling intents from an Alexa skill and using the
4- # Alexa Skills Kid SDK (v2)
5- # Please visit https://alexa.design/cookbook for additional examples on
6- # implementing slots, dialog management,
3+ # This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK for Python.
4+ # Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
75# session persistence, api calls, and more.
8-
6+ # This sample is built using the handler classes approach in skill builder.
97import logging
8+ import ask_sdk_core .utils as ask_utils
109
11- from ask_sdk_core .skill_builder import CustomSkillBuilder
12- from ask_sdk_core .dispatch_components import (
13- AbstractRequestHandler , AbstractExceptionHandler
14- )
15- from ask_sdk_core .utils import is_request_type , is_intent_name
10+ from ask_sdk_core .skill_builder import SkillBuilder
11+ from ask_sdk_core .dispatch_components import AbstractRequestHandler
12+ from ask_sdk_core .dispatch_components import AbstractExceptionHandler
13+ from ask_sdk_core .handler_input import HandlerInput
1614
17- sb = CustomSkillBuilder ()
15+ from ask_sdk_model import Response
1816
19- logger = logging .getLogger ("main" )
17+ logger = logging .getLogger (__name__ )
2018logger .setLevel (logging .INFO )
2119
2220
23- class LaunchRequestIntentHandler (AbstractRequestHandler ):
24- """
25- Handler for Skill Launch
26- """
21+ class LaunchRequestHandler (AbstractRequestHandler ):
22+ """Handler for Skill Launch."""
2723 def can_handle (self , handler_input ):
28- return is_request_type ("LaunchRequest" )(handler_input )
24+ # type: (HandlerInput) -> bool
25+
26+ return ask_utils .is_request_type ("LaunchRequest" )(handler_input )
2927
3028 def handle (self , handler_input ):
29+ # type: (HandlerInput) -> Response
3130 speak_output = "Hello! Welcome to cake walk. That was a piece of cake! Bye!"
3231
33- handler_input .response_builder .speak (speak_output )
34- return handler_input .response_builder .response
32+ return (
33+ handler_input .response_builder
34+ .speak (speak_output )
35+ #.ask(speak_output)
36+ .response
37+ )
3538
3639
3740class HelloWorldIntentHandler (AbstractRequestHandler ):
38- """
39- Handler for the HelloWorld Intent
40- """
41+ """Handler for Hello World Intent."""
4142 def can_handle (self , handler_input ):
42- return is_intent_name ("HelloWorldIntent" )
43-
43+ # type: (HandlerInput) -> bool
44+ return ask_utils .is_intent_name ("HelloWorldIntent" )(handler_input )
45+
4446 def handle (self , handler_input ):
47+ # type: (HandlerInput) -> Response
4548 speak_output = "Hello World!"
4649
47- handler_input .response_builder .speak (speak_output )
48- return handler_input .response_builder .response
50+ return (
51+ handler_input .response_builder
52+ .speak (speak_output )
53+ # .ask("add a reprompt if you want to keep the session open for the user to respond")
54+ .response
55+ )
4956
5057
5158class HelpIntentHandler (AbstractRequestHandler ):
52- """
53- Handler for AMAZON.HelpIntent
54- """
59+ """Handler for Help Intent."""
5560 def can_handle (self , handler_input ):
56- return is_intent_name ("AMAZON.HelpIntent" )(handler_input )
61+ # type: (HandlerInput) -> bool
62+ return ask_utils .is_intent_name ("AMAZON.HelpIntent" )(handler_input )
5763
5864 def handle (self , handler_input ):
65+ # type: (HandlerInput) -> Response
5966 speak_output = "You can say hello to me! How can I help?"
6067
61- handler_input .response_builder .speak (
62- speak_output
63- ).ask (speak_output )
64- return handler_input .response_builder .response
68+ return (
69+ handler_input .response_builder
70+ .speak (speak_output )
71+ .ask (speak_output )
72+ .response
73+ )
6574
6675
67- class CancelAndStopIntentHandler (AbstractRequestHandler ):
68- """
69- Handler for AMAZON.CancelIntent and AMAZON.StopIntent
70- """
76+ class CancelOrStopIntentHandler (AbstractRequestHandler ):
77+ """Single handler for Cancel and Stop Intent."""
7178 def can_handle (self , handler_input ):
72- return is_intent_name ("AMAZON.CancelIntent" )(handler_input ) \
73- and is_intent_name ("AMAZON.StopIntent" )(handler_input )
79+ # type: (HandlerInput) -> bool
80+ return (ask_utils .is_intent_name ("AMAZON.CancelIntent" )(handler_input ) or
81+ ask_utils .is_intent_name ("AMAZON.StopIntent" )(handler_input ))
7482
7583 def handle (self , handler_input ):
84+ # type: (HandlerInput) -> Response
7685 speak_output = "Goodbye!"
77- handler_input .response_builder .speak (speak_output )
78- return handler_input .response_builder .response
86+
87+ return (
88+ handler_input .response_builder
89+ .speak (speak_output )
90+ .response
91+ )
7992
8093
8194class SessionEndedRequestHandler (AbstractRequestHandler ):
82- """
83- Handler for SessionEndedRequest
84- """
95+ """Handler for Session End."""
8596 def can_handle (self , handler_input ):
86- return is_request_type ("SessionEndedRequest" )(handler_input )
97+ # type: (HandlerInput) -> bool
98+ return ask_utils .is_request_type ("SessionEndedRequest" )(handler_input )
8799
88100 def handle (self , handler_input ):
89- # Any cleanup logic goes here
101+ # type: (HandlerInput) -> Response
102+
103+ # Any cleanup logic goes here.
104+
90105 return handler_input .response_builder .response
91106
92107
93- class CatchAllExceptionHandler (AbstractExceptionHandler ):
108+ class IntentReflectorHandler (AbstractRequestHandler ):
109+ """The intent reflector is used for interaction model testing and debugging.
110+ It will simply repeat the intent the user said. You can create custom handlers
111+ for your intents by defining them above, then also adding them to the request
112+ handler chain below.
94113 """
95- Catch all exception handler, log exception and
96- respond with custom message.
114+ def can_handle (self , handler_input ):
115+ # type: (HandlerInput) -> bool
116+ return ask_utils .is_request_type ("IntentRequest" )(handler_input )
117+
118+ def handle (self , handler_input ):
119+ # type: (HandlerInput) -> Response
120+ intent_name = ask_utils .get_intent_name (handler_input )
121+ speak_output = "You just triggered " + intent_name + "."
122+
123+ return (
124+ handler_input .response_builder
125+ .speak (speak_output )
126+ # .ask("add a reprompt if you want to keep the session open for the user to respond")
127+ .response
128+ )
129+
130+
131+ class CatchAllExceptionHandler (AbstractExceptionHandler ):
132+ """Generic error handling to capture any syntax or routing errors. If you receive an error
133+ stating the request handler chain is not found, you have not implemented a handler for
134+ the intent being invoked or included it in the skill builder below.
97135 """
98136 def can_handle (self , handler_input , exception ):
137+ # type: (HandlerInput, Exception) -> bool
99138 return True
100139
101140 def handle (self , handler_input , exception ):
141+ # type: (HandlerInput, Exception) -> Response
142+ logger .error (exception , exc_info = True )
102143
103- speak_output = "Sorry, I couldn't understand what you said. Please try again."
104- handler_input .response_builder .speak (speak_output ).ask (speak_output )
105- return handler_input .response_builder .response
144+ speak_output = "Sorry, I had trouble doing what you asked. Please try again."
106145
107- # register request / intent handlers
108- sb .add_request_handler (LaunchRequestIntentHandler ())
109- sb .add_request_handler (HelpIntentHandler ())
146+ return (
147+ handler_input .response_builder
148+ .speak (speak_output )
149+ .ask (speak_output )
150+ .response
151+ )
152+
153+ # The SkillBuilder object acts as the entry point for your skill, routing all request and response
154+ # payloads to the handlers above. Make sure any new handlers or interceptors you've
155+ # defined are included below. The order matters - they're processed top to bottom.
156+
157+
158+ sb = SkillBuilder ()
159+
160+ sb .add_request_handler (LaunchRequestHandler ())
110161sb .add_request_handler (HelloWorldIntentHandler ())
111- sb .add_request_handler (CancelAndStopIntentHandler ())
162+ sb .add_request_handler (HelpIntentHandler ())
163+ sb .add_request_handler (CancelOrStopIntentHandler ())
112164sb .add_request_handler (SessionEndedRequestHandler ())
165+ sb .add_request_handler (IntentReflectorHandler ()) # make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
113166
114- # register exception handlers
115167sb .add_exception_handler (CatchAllExceptionHandler ())
116168
117- lambda_handler = sb .lambda_handler ()
169+ lambda_handler = sb .lambda_handler ()
0 commit comments