-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
321 lines (236 loc) · 9.81 KB
/
lambda_function.py
File metadata and controls
321 lines (236 loc) · 9.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# -*- coding: utf-8 -*-
""" Jersey Beers Alexa Skill! Returns the tap list for your favorite brewery """
# pylint: disable-msg=R0911, W0401, R1705, W0613
from controllers import brewerylist # for clarity
from models.breweries import * # instantiate all our brewery page scrapers
from models.breweries.custom import * # instantiate the custom scraped breweries
from models import cloudredis, setuplogging
SKILL_NAME = "Jersey Beers"
HELP_MESSAGE = "You can ask what is on tap at select breweries by name, " + \
"or you can say exit... What can I help you with?"
HELP_REPROMPT = "What can I help you with?"
STOP_MESSAGE = "Goodbye!"
FALLBACK_MESSAGE = "The Jersey Beers skill can't help you with that. " + \
"It can help you discover what beers are on tap " + \
"at select breweries in North Jersey"
FALLBACK_REPROMPT = 'What can I help you with?'
HOME_BREWERY_SET = 'Your home brewery has been set to {0}'
CANNOT_SET_HOME = 'Sorry, I cannot set {0} as your home brewery'
NO_HOME_BREWERY_SET = 'Sorry, no home brewery has been set. You can set your home brewery' + \
'by saying ask Jersey Beers to set my home brewery to a brewery'
CURRENT_HOME_BREWERY = "Your current home brewery is {0}"
ERROR_NO_BREWERY = "I'm sorry, you must specify a brewery"
ERROR_BREWERY_PAGE = "Sorry, I'm having problems reading that breweries tap list. /" \
"I have notified the proper authorities"
def lambda_handler(event, context):
""" App entry point """
setuplogging.initialize_logging(mocking=False) # make sure logging is setup
setuplogging.LOGGING_HANDLER(f'EVENT{event}') # log the event
if event['session']['new']:
on_session_started()
if event['request']['type'] == "LaunchRequest":
return on_launch(event['request'])
elif event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'])
elif event['request']['type'] == "SessionEndedRequest":
return on_session_ended()
return None
# --------------- Response handlers -----------------
def on_intent(request, session):
""" called on receipt of an Intent """
intent_name = request['intent']['name']
# initialize our redis server if needed
if cloudredis.REDIS_SERVER is None:
cloudredis.initialize_cloud_redis(injected_server=None)
# process the intents
if intent_name == "GetTapListIntent":
return get_taplist_response(request['intent'])
elif intent_name == 'ListBreweries':
return list_of_breweries_response()
elif intent_name == 'SetHomeBrewery':
return set_home_brewery(request, session)
elif intent_name == 'GetHomeTapList':
return get_home_brewery_taplist(request, session)
elif intent_name == 'GetHomeBrewery':
return get_home_brewery(request, session)
elif intent_name == "AMAZON.HelpIntent":
return get_help_response()
elif intent_name == "AMAZON.StopIntent":
return get_stop_response()
elif intent_name == "AMAZON.CancelIntent":
return get_stop_response()
elif intent_name == "AMAZON.FallbackIntent":
return get_fallback_response()
return get_help_response()
def get_home_brewery_taplist(request: dict, session: dict):
"""get the taplist for our home brewery"""
aws_user_id = session['user']['userId']
brewery = brewerylist.BREWERY_PAGES.get_home_brewery(user_id=aws_user_id)
if not brewery: # didn't find a home
return response(speech_response(NO_HOME_BREWERY_SET, True))
# okay, we have a home brewery, so lets get the tap list
mocked = False
if 'mocked' in request['intent']:
mocked = request['intent']['mocked']
taplist_intent = {"slots":{"brewery":{"value": brewery.decode('utf-8')}}, "mocked": mocked}
return get_taplist_response(taplist_intent)
def set_home_brewery(request: dict, session: dict):
"""set the home brewery for the user"""
try:
brewery = request['intent']['slots']['brewery']['value']
aws_user_id = session['user']['userId']
success = brewerylist.BREWERY_PAGES.add_home_brewery(brewery_name=brewery,
user_id=aws_user_id)
if success:
return response(speech_response(HOME_BREWERY_SET.format(brewery), True))
# some problem, tell the user. TBD validate brewery & other things,
# perhaps ask for clarification
setuplogging.LOGGING_HANDLER(f"SetHomeBrewery, brewery not found:\"{brewery}\"")
return response(speech_response(CANNOT_SET_HOME.format(brewery), True))
except KeyError:
return response(speech_response(ERROR_NO_BREWERY, True))
def get_home_brewery(request: dict, session: dict):
"""get the home brewery for the user"""
aws_user_id = session['user']['userId']
brewery = brewerylist.BREWERY_PAGES.get_home_brewery(user_id=aws_user_id)
if not brewery: # didn't find a home
return response(speech_response(NO_HOME_BREWERY_SET, True))
# some problem, tell the user. TBD validate brewery & other things,
# perhaps ask for clarification
return response(speech_response(CURRENT_HOME_BREWERY.format(brewery), True))
def list_of_breweries_response():
"""Return a list of breweries that we support"""
list_of_breweries = brewerylist.BREWERY_PAGES.ssml_brewery_list()
return response(speech_response(list_of_breweries, True))
def get_taplist_response(intent: dict):
""" return the taplist """
brewery_name: str = 'unknown'
try:
brewery_name = intent['slots']['brewery']['value']
bobj, brewery_id = brewerylist.BREWERY_PAGES.find_brewery(brewery_name=brewery_name)
# if we couldn't find the brewery, respond with the list of breweries we know
if brewery_id is None or bobj is None:
setuplogging.LOGGING_HANDLER(f"GetTapList, brewery not found: \"{brewery_name}\"")
return list_of_breweries_response()
if 'mocked' in intent:
bobj.mocking = intent['mocked']
bobj.fetch_taplist(brewery=brewery_id)
beer_string = bobj.ssml_taplist()
return response(speech_response_ssml(beer_string, True))
except KeyError:
return response(speech_response(ERROR_NO_BREWERY, True))
except Exception: # pylint: disable=broad-exception-caught
setuplogging.LOGGING_HANDLER(f"PAGELOAD failure!! brewery \"{brewery_name}\"")
return response(speech_response(ERROR_BREWERY_PAGE, True))
def get_help_response():
""" get and return the help string """
speech_message = HELP_MESSAGE
return response(speech_response_prompt(speech_message, speech_message, False))
def get_launch_response():
""" get and return the help string """
return response(speech_response(HELP_MESSAGE, False))
def get_stop_response():
""" end the session, user wants to quit """
speech_output = STOP_MESSAGE
return response(speech_response(speech_output, True))
def get_fallback_response():
""" end the session, user wants to quit """
speech_output = FALLBACK_MESSAGE
return response(speech_response(speech_output, True))
def on_session_started():
"""" called when the session starts """
#print("on_session_started")
def on_session_ended():
""" called on session ends """
#print("on_session_ended")
def on_launch(request):
""" called on Launch, we reply with a launch message """
return get_launch_response()
# --------------- Speech response handlers -----------------
def speech_response_ssml(output, endsession):
""" create a simple json response """
return {
'outputSpeech': {
'type': 'SSML',
'ssml': '<speak>' + output + '</speak>'
},
'shouldEndSession': endsession
}
def speech_response(output, endsession):
""" create a simple json response """
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'shouldEndSession': endsession
}
# def dialog_response(endsession):
#
# """ create a simple json response with card """
#
# return {
# 'version': '1.0',
# 'response':{
# 'directives': [
# {
# 'type': 'Dialog.Delegate'
# }
# ],
# 'shouldEndSession': endsession
# }
# }
# def speech_response_with_card(title, output, cardcontent, endsession):
#
# """ create a simple json response with card """
#
# return {
# 'card': {
# 'type': 'Simple',
# 'title': title,
# 'content': cardcontent
# },
# 'outputSpeech': {
# 'type': 'PlainText',
# 'text': output
# },
# 'shouldEndSession': endsession
# }
# def response_ssml_text_and_prompt(output, endsession, reprompt_text):
#
# """ create a Ssml response with prompt """
#
# return {
# 'outputSpeech': {
# 'type': 'SSML',
# 'ssml': "<speak>" +output +"</speak>"
# },
# 'reprompt': {
# 'outputSpeech': {
# 'type': 'SSML',
# 'ssml': "<speak>" +reprompt_text +"</speak>"
# }
# },
# 'shouldEndSession': endsession
# }
def speech_response_prompt(output, reprompt_text, endsession):
""" create a simple json response with a prompt """
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': endsession
}
def response(speech_message) -> dict:
""" create a simple json response """
return {
'version': '1.0',
'response': speech_message
}