-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAlexaBaseHandler.py
More file actions
executable file
·150 lines (132 loc) · 4.81 KB
/
Copy pathAlexaBaseHandler.py
File metadata and controls
executable file
·150 lines (132 loc) · 4.81 KB
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
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)
self._card_title = "test Response"
self._card_output = "test card output"
self._speech_output = ''
# If the user either does not reply to the welcome message or says something
# that is not understood, they will be prompted again with this text.
self._reprompt_text = ''
self._should_end_session = True
self._speech_output = ''
@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):
"""
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': self._speech_output
},
'card': {
'type': 'Simple',
'title': self._card_title,
'content': self._card_output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': self._reprompt_text
}
},
'shouldEndSession': self._should_end_session
}
def _build_response(self, session_attributes):
"""
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': self._build_speechlet_response()
}
@staticmethod
def _get_session_attribute(session):
if 'attributes' in session:
return session['attributes']
else:
return dict()