Skip to content

Commit b89bfc1

Browse files
committed
Updating the code and the guide for Modules 1-4
1 parent 17ada93 commit b89bfc1

16 files changed

Lines changed: 2005 additions & 808 deletions

‎module-1/README.md‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎module-1/en-US.json‎

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
{
2-
"interactionModel": {
3-
"languageModel": {
4-
"invocationName": "cake walk",
5-
"intents": [
6-
{
7-
"name": "AMAZON.CancelIntent",
8-
"samples": []
9-
},
10-
{
11-
"name": "AMAZON.HelpIntent",
12-
"samples": []
13-
},
14-
{
15-
"name": "AMAZON.StopIntent",
16-
"samples": []
17-
},
18-
{
19-
"name": "HelloWorldIntent",
20-
"slots": [],
21-
"samples": [
22-
"hello",
23-
"how are you",
24-
"say hi world",
25-
"say hi",
26-
"hi",
27-
"say hello world",
28-
"say hello"
29-
]
30-
},
31-
{
32-
"name": "AMAZON.NavigateHomeIntent",
33-
"samples": []
34-
}
35-
],
36-
"types": []
37-
}
38-
}
39-
}
2+
"interactionModel": {
3+
"languageModel": {
4+
"invocationName": "cake walk",
5+
"intents": [
6+
{
7+
"name": "AMAZON.CancelIntent",
8+
"samples": []
9+
},
10+
{
11+
"name": "AMAZON.HelpIntent",
12+
"samples": []
13+
},
14+
{
15+
"name": "AMAZON.StopIntent",
16+
"samples": []
17+
},
18+
{
19+
"name": "HelloWorldIntent",
20+
"slots": [],
21+
"samples": [
22+
"hello",
23+
"how are you",
24+
"say hi world",
25+
"say hi",
26+
"hi",
27+
"say hello world",
28+
"say hello"
29+
]
30+
},
31+
{
32+
"name": "AMAZON.NavigateHomeIntent",
33+
"samples": []
34+
}
35+
],
36+
"types": []
37+
}
38+
}
39+
}

‎module-1/lambda_function.py‎

Lines changed: 111 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,169 @@
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.
97
import 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__)
2018
logger.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

3740
class 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

5158
class 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

8194
class 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())
110161
sb.add_request_handler(HelloWorldIntentHandler())
111-
sb.add_request_handler(CancelAndStopIntentHandler())
162+
sb.add_request_handler(HelpIntentHandler())
163+
sb.add_request_handler(CancelOrStopIntentHandler())
112164
sb.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
115167
sb.add_exception_handler(CatchAllExceptionHandler())
116168

117-
lambda_handler = sb.lambda_handler()
169+
lambda_handler = sb.lambda_handler()

‎module-1/requirements.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
ask-sdk-core
1+
boto3==1.9.216
2+
ask-sdk-core==1.11.0

0 commit comments

Comments
 (0)