-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAlexaBaseHandler.py
172 lines (148 loc) · 5.5 KB
/
AlexaBaseHandler.py
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
import abc
import logging
class AlexaBaseHandler(object):
"""
Base class for a python Alexa Skill Set. Concrete implementations
are expected to implement the abstract methods.
See the following for Alexa details:
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/handling-requests-sent-by-alexa
"""
__metaclass__ = abc.ABCMeta
def __init__(self):
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
@abc.abstractmethod
def on_launch(self, launch_request, session):
"""
Implement the LaunchRequest. Called when the user issues a:
Alexa, open <invocation name>
:param launch_request:
:param session:
:return: the output of _build_response
"""
pass
@abc.abstractmethod
def on_session_started(self, session_started_request, session):
pass
@abc.abstractmethod
def on_intent(self, intent_request, session):
"""
Implement the IntentRequest
:param intent_request:
:param session:
:return: the output of _build_response
"""
pass
@abc.abstractmethod
def on_session_ended(self, session_end_request, session):
"""
Implement the SessionEndRequest
:param session_end_request:
:param session:
:return: the output of _build_response
"""
pass
@abc.abstractmethod
def on_processing_error(self, event, context, exc):
"""
If an unexpected error occurs during the process_request method
this handler will be invoked to give the concrete handler
an opportunity to respond gracefully
:param exc exception instance
:return: the output of _build_response
"""
pass
def process_request(self, event, context):
"""
Helper method to process the input Alexa request and
dispatch to the appropriate on_ handler
:param event:
:param context:
:return: response from the on_ handler
"""
# if its a new session, run the new session code
try:
response = None
if event['session']['new']:
self.on_session_started({'requestId': event['request']['requestId']}, event['session'])
# regardless of whether its new, handle the request type
if event['request']['type'] == "LaunchRequest":
response = self.on_launch(event['request'], event['session'])
elif event['request']['type'] == "IntentRequest":
response = self.on_intent(event['request'], event['session'])
elif event['request']['type'] == "SessionEndedRequest":
response = self.on_session_ended(event['request'], event['session'])
except Exception as exc:
response = self.on_processing_error(event, context, exc)
return response
# --------------- Helpers that build all of the responses ----------------------
def _build_speechlet_response(self, card_title, card_output, speech_output, reprompt_text, should_end_session):
"""
Internal helper method to build the speechlet portion of the response
:param card_title:
:param card_output:
:param speech_output:
:param reprompt_text:
:param should_end_session:
:return:
"""
return {
'outputSpeech': {
'type': 'PlainText',
'text': speech_output
},
'card': {
'type': 'Simple',
'title': card_title,
'content': card_output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
def _build_response(self, session_attributes, speechlet_response):
"""
Internal helper method to build the Alexa response message
:param session_attributes:
:param speechlet_response:
:return: properly formatted Alexa response
"""
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
}
def _is_intent(self, intent_name, intent_request):
return self._get_intent_name(intent_request) == intent_name
def _get_intent(self, intent_request):
if 'intent' in intent_request:
return intent_request['intent']
else:
return None
def _get_intent_name(self, intent_request):
intent = self._get_intent(intent_request)
intent_name = None
if intent is not None and 'name' in intent:
intent_name = intent['name']
return intent_name
def _slot_exists(self, slot_name, intent_request):
intent = self._get_intent(intent_request)
if intent is not None:
return slot_name in intent['slots']
else:
return False
def _get_slot_value(self, slot_name, intent_request):
value = None
try:
if self._slot_exists(slot_name, intent_request):
intent = self._get_intent(intent_request)
value = intent['slots'][slot_name]['value']
else:
value = None
except Exception as exc:
self.logger.error("Error getting slot value for slot_name={0}".format(slot_name))
return value