|
@@ -0,0 +1,373 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
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,
|
|
5
|
+# session persistence, api calls, and more.
|
|
6
|
+# This sample is built using the handler classes approach in skill builder.
|
|
7
|
+import logging
|
|
8
|
+import requests
|
|
9
|
+import ask_sdk_core.utils as ask_utils
|
|
10
|
+
|
|
11
|
+#from ask_sdk_s3.adapter import S3Adapter
|
|
12
|
+#s3_adapter = S3Adapter(bucket_name=os.environ["S3_PERSISTENCE_BUCKET"])
|
|
13
|
+
|
|
14
|
+from ask_sdk_core.skill_builder import SkillBuilder
|
|
15
|
+from ask_sdk_core.dispatch_components import AbstractRequestHandler
|
|
16
|
+from ask_sdk_core.dispatch_components import AbstractExceptionHandler
|
|
17
|
+from ask_sdk_core.handler_input import HandlerInput
|
|
18
|
+
|
|
19
|
+from ask_sdk_model import Response
|
|
20
|
+
|
|
21
|
+logger = logging.getLogger(__name__)
|
|
22
|
+logger.setLevel(logging.INFO)
|
|
23
|
+
|
|
24
|
+ordernum = 0
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+class LaunchRequestHandler(AbstractRequestHandler):
|
|
30
|
+ """Handler for Skill Launch."""
|
|
31
|
+ def can_handle(self, handler_input):
|
|
32
|
+ # type: (HandlerInput) -> bool
|
|
33
|
+
|
|
34
|
+ return ask_utils.is_request_type("LaunchRequest")(handler_input)
|
|
35
|
+
|
|
36
|
+ def handle(self, handler_input):
|
|
37
|
+ # type: (HandlerInput) -> Response
|
|
38
|
+ speak_output = "What would you like to order?"
|
|
39
|
+
|
|
40
|
+ return (
|
|
41
|
+ handler_input.response_builder
|
|
42
|
+ .speak(speak_output)
|
|
43
|
+ .ask(speak_output)
|
|
44
|
+ .response
|
|
45
|
+ )
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+class HelloWorldIntentHandler(AbstractRequestHandler):
|
|
49
|
+ """Handler for Hello World Intent."""
|
|
50
|
+ def can_handle(self, handler_input):
|
|
51
|
+ # type: (HandlerInput) -> bool
|
|
52
|
+ return ask_utils.is_intent_name("HelloWorldIntent")(handler_input)
|
|
53
|
+
|
|
54
|
+ def handle(self, handler_input):
|
|
55
|
+ # type: (HandlerInput) -> Response
|
|
56
|
+ speak_output = "Hello World!"
|
|
57
|
+
|
|
58
|
+ return (
|
|
59
|
+ handler_input.response_builder
|
|
60
|
+ .speak(speak_output)
|
|
61
|
+ # .ask("add a reprompt if you want to keep the session open for the user to respond")
|
|
62
|
+ .response
|
|
63
|
+ )
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+class LinguiniOrderFoodIntentHandler(AbstractRequestHandler):
|
|
67
|
+ """Handler for Linguini Order Food Intent."""
|
|
68
|
+ global ordernum
|
|
69
|
+ ordernum = ordernum + 1
|
|
70
|
+ global food_dict
|
|
71
|
+ global order
|
|
72
|
+ def can_handle(self, handler_input):
|
|
73
|
+ # type: (HandlerInput) -> bool
|
|
74
|
+ return ask_utils.is_intent_name("LinguiniOrderFoodIntent")(handler_input)
|
|
75
|
+
|
|
76
|
+ def handle(self, handler_input):
|
|
77
|
+ # type: (HandlerInput) -> Response
|
|
78
|
+ slots = handler_input.request_envelope.request.intent.slots
|
|
79
|
+ food_type = slots["food_type"].value
|
|
80
|
+ food_category = slots["food_category"].value
|
|
81
|
+ speak_output = f"One {food_type} {food_category} added to your order. What else would you like to add?"
|
|
82
|
+ food = f"{food_type} {food_category}"
|
|
83
|
+
|
|
84
|
+ if food == "gardenia pasta":
|
|
85
|
+ orderstr = 'orden' + str(ordernum)
|
|
86
|
+ order[orderstr] = 2
|
|
87
|
+ else:
|
|
88
|
+ orderstr = 'orden' + str(ordernum)
|
|
89
|
+ order[ordernum] = 1
|
|
90
|
+
|
|
91
|
+ if food in order_names:
|
|
92
|
+ order_names[food] += 1
|
|
93
|
+ else:
|
|
94
|
+ order_names[food] = 1
|
|
95
|
+
|
|
96
|
+ handler_input.response_builder.speak(speak_output).ask("What else would you like to add?")
|
|
97
|
+ return (handler_input.response_builder.response)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+class LinguiniOrderDrinkIntentHandler(AbstractRequestHandler):
|
|
101
|
+ """Handler for Linguini Order Drink Intent."""
|
|
102
|
+ global ordernum
|
|
103
|
+ ordernum = ordernum + 1
|
|
104
|
+ global drink_dict
|
|
105
|
+ global order
|
|
106
|
+ global order_names
|
|
107
|
+ def can_handle(self, handler_input):
|
|
108
|
+ # type: (HandlerInput) -> bool
|
|
109
|
+ return ask_utils.is_intent_name("LinguiniOrderDrinkIntent")(handler_input)
|
|
110
|
+
|
|
111
|
+ def handle(self, handler_input):
|
|
112
|
+ # type: (HandlerInput) -> Response
|
|
113
|
+ slots = handler_input.request_envelope.request.intent.slots
|
|
114
|
+ drink_type = slots["drink_type"].value
|
|
115
|
+ speak_output = f"One {drink_type} added to your order. What else would you like to add?"
|
|
116
|
+
|
|
117
|
+ if drink_type == "water":
|
|
118
|
+ orderstr = 'orden' + str(ordernum)
|
|
119
|
+ order[orderstr] = 10
|
|
120
|
+ else:
|
|
121
|
+ orderstr = 'orden' + str(ordernum)
|
|
122
|
+ order[orderstr] = 9
|
|
123
|
+
|
|
124
|
+ if drink_type in order_names:
|
|
125
|
+ order_names[drink_type] += 1
|
|
126
|
+ else:
|
|
127
|
+ order_names[drink_type] = 1
|
|
128
|
+ handler_input.response_builder.speak(speak_output).ask("What else would you like to add?")
|
|
129
|
+ return (handler_input.response_builder.response)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+class HelpIntentHandler(AbstractRequestHandler):
|
|
133
|
+ """Handler for Help Intent."""
|
|
134
|
+ def can_handle(self, handler_input):
|
|
135
|
+ # type: (HandlerInput) -> bool
|
|
136
|
+ return ask_utils.is_intent_name("AMAZON.HelpIntent")(handler_input)
|
|
137
|
+
|
|
138
|
+ def handle(self, handler_input):
|
|
139
|
+ # type: (HandlerInput) -> Response
|
|
140
|
+ speak_output = "You can say hello to me! How can I help?"
|
|
141
|
+
|
|
142
|
+ return (
|
|
143
|
+ handler_input.response_builder
|
|
144
|
+ .speak(speak_output)
|
|
145
|
+ .ask(speak_output)
|
|
146
|
+ .response
|
|
147
|
+ )
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+class CancelIntentHandler(AbstractRequestHandler):
|
|
151
|
+ """Handler for Cancel Intent."""
|
|
152
|
+ global order
|
|
153
|
+ global order_names
|
|
154
|
+ def can_handle(self, handler_input):
|
|
155
|
+ # type: (HandlerInput) -> bool
|
|
156
|
+ return (ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input))
|
|
157
|
+
|
|
158
|
+ def handle(self, handler_input):
|
|
159
|
+ # type: (HandlerInput) -> Response
|
|
160
|
+ speak_output = "Your order has been canceled."
|
|
161
|
+ order.clear()
|
|
162
|
+ order_names.clear()
|
|
163
|
+ return (
|
|
164
|
+ handler_input.response_builder
|
|
165
|
+ .speak(speak_output)
|
|
166
|
+ .ask("reprompt")
|
|
167
|
+ .response
|
|
168
|
+ )
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+class StopIntentHandler(AbstractRequestHandler):
|
|
172
|
+ """Handler for Stop Intent."""
|
|
173
|
+ def can_handle(self, handler_input):
|
|
174
|
+ # type: (HandlerInput) -> bool
|
|
175
|
+ return (ask_utils.is_intent_name("AMAZON.StopIntent")(handler_input))
|
|
176
|
+
|
|
177
|
+ def handle(self, handler_input):
|
|
178
|
+ # type: (HandlerInput) -> Response
|
|
179
|
+ speak_output = "Alright! Your order is complete. Would you like to review, finish, or cancel your order?"
|
|
180
|
+
|
|
181
|
+ return (
|
|
182
|
+ handler_input.response_builder
|
|
183
|
+ .speak(speak_output)
|
|
184
|
+ .ask(speak_output)
|
|
185
|
+ .response
|
|
186
|
+ )
|
|
187
|
+
|
|
188
|
+class LinguiniOrderNothingIntentHandler(AbstractRequestHandler):
|
|
189
|
+ """Handler for Stop Intent."""
|
|
190
|
+ def can_handle(self, handler_input):
|
|
191
|
+ # type: (HandlerInput) -> bool
|
|
192
|
+ return (ask_utils.is_intent_name("LinguiniOrderNothingIntent")(handler_input))
|
|
193
|
+
|
|
194
|
+ def handle(self, handler_input):
|
|
195
|
+ # type: (HandlerInput) -> Response
|
|
196
|
+ speak_output = "Alright! Your order is complete. Would you like to review, finish, or cancel your order"
|
|
197
|
+
|
|
198
|
+ return (
|
|
199
|
+ handler_input.response_builder
|
|
200
|
+ .speak(speak_output)
|
|
201
|
+ .ask(speak_output)
|
|
202
|
+ .response
|
|
203
|
+ )
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+class LinguiniOrderReviewIntentHandler(AbstractRequestHandler):
|
|
207
|
+ global order
|
|
208
|
+ """Handler for Hello World Intent."""
|
|
209
|
+ def can_handle(self, handler_input):
|
|
210
|
+ # type: (HandlerInput) -> bool
|
|
211
|
+ return ask_utils.is_intent_name("LinguiniOrderReviewIntent")(handler_input)
|
|
212
|
+
|
|
213
|
+ def handle(self, handler_input):
|
|
214
|
+ # type: (HandlerInput) -> Response
|
|
215
|
+ speak_output = "You have "
|
|
216
|
+ for i in order_names:
|
|
217
|
+ speak_output += f"{order_names[i]} {i}, "
|
|
218
|
+ speak_output = speak_output[:-2]
|
|
219
|
+ speak_output += ". If everything seems okay, say finish to submit order or say cancel to cancel and restart order."
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+ return (
|
|
223
|
+ handler_input.response_builder
|
|
224
|
+ .speak(speak_output)
|
|
225
|
+ .ask("add a reprompt if you want to keep the session open for the user to respond")
|
|
226
|
+ .response
|
|
227
|
+ )
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+class LinguiniOrderFinishIntentHandler(AbstractRequestHandler):
|
|
231
|
+ global order
|
|
232
|
+ global order_names
|
|
233
|
+ """Handler for Hello World Intent."""
|
|
234
|
+ def can_handle(self, handler_input):
|
|
235
|
+ # type: (HandlerInput) -> bool
|
|
236
|
+ return ask_utils.is_intent_name("LinguiniOrderFinishIntent")(handler_input)
|
|
237
|
+
|
|
238
|
+ def handle(self, handler_input):
|
|
239
|
+ # type: (HandlerInput) -> Response
|
|
240
|
+ #Send order to Restaurant lmao
|
|
241
|
+
|
|
242
|
+ full_order = [{'nombre': 'Aranpompano','celular': '(555)-555-5555', 'ordenCompleta': order, 'peticiones': {}}]
|
|
243
|
+ #full_order = [{'nombre': 'Scott Pilgrim','celular': '(555)-555-5555', 'ordenCompleta': {'orden1':1, 'orden2': 10}, 'peticiones': {}}]
|
|
244
|
+
|
|
245
|
+ try:
|
|
246
|
+ r = requests.post('http://136.145.231.48:5000/papelonDeComida', json=full_order)
|
|
247
|
+ except:
|
|
248
|
+ r = None
|
|
249
|
+
|
|
250
|
+ if r == None:
|
|
251
|
+ speak_output = "Sorry, I couldn't connect to the restaurant. Please try again later."
|
|
252
|
+
|
|
253
|
+ elif(r.status_code == 200):
|
|
254
|
+ speak_output = f'Alright! {r.text}'
|
|
255
|
+ else:
|
|
256
|
+ speak_output = "Uh oh! I couldn't place your order, please try again later."
|
|
257
|
+
|
|
258
|
+ #REPLACE THIS WHEN API IS IMPLEMENTED
|
|
259
|
+ order.clear()
|
|
260
|
+ order_names.clear()
|
|
261
|
+ #REPLACE THIS WHEN API IS IMPLEMENTED
|
|
262
|
+
|
|
263
|
+ return (
|
|
264
|
+ handler_input.response_builder
|
|
265
|
+ .speak(speak_output)
|
|
266
|
+ # .ask("add a reprompt if you want to keep the session open for the user to respond")
|
|
267
|
+ .response
|
|
268
|
+ )
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+class SessionEndedRequestHandler(AbstractRequestHandler):
|
|
272
|
+ """Handler for Session End."""
|
|
273
|
+ def can_handle(self, handler_input):
|
|
274
|
+ # type: (HandlerInput) -> bool
|
|
275
|
+ return ask_utils.is_request_type("SessionEndedRequest")(handler_input)
|
|
276
|
+
|
|
277
|
+ def handle(self, handler_input):
|
|
278
|
+ # type: (HandlerInput) -> Response
|
|
279
|
+
|
|
280
|
+ # Any cleanup logic goes here.
|
|
281
|
+
|
|
282
|
+ return handler_input.response_builder.response
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+class IntentReflectorHandler(AbstractRequestHandler):
|
|
286
|
+ """The intent reflector is used for interaction model testing and debugging.
|
|
287
|
+ It will simply repeat the intent the user said. You can create custom handlers
|
|
288
|
+ for your intents by defining them above, then also adding them to the request
|
|
289
|
+ handler chain below.
|
|
290
|
+ """
|
|
291
|
+ def can_handle(self, handler_input):
|
|
292
|
+ # type: (HandlerInput) -> bool
|
|
293
|
+ return ask_utils.is_request_type("IntentRequest")(handler_input)
|
|
294
|
+
|
|
295
|
+ def handle(self, handler_input):
|
|
296
|
+ # type: (HandlerInput) -> Response
|
|
297
|
+ intent_name = ask_utils.get_intent_name(handler_input)
|
|
298
|
+ speak_output = "You just triggered " + intent_name + "."
|
|
299
|
+
|
|
300
|
+ return (
|
|
301
|
+ handler_input.response_builder
|
|
302
|
+ .speak(speak_output)
|
|
303
|
+ # .ask("add a reprompt if you want to keep the session open for the user to respond")
|
|
304
|
+ .response
|
|
305
|
+ )
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+class CatchAllExceptionHandler(AbstractExceptionHandler):
|
|
309
|
+ """Generic error handling to capture any syntax or routing errors. If you receive an error
|
|
310
|
+ stating the request handler chain is not found, you have not implemented a handler for
|
|
311
|
+ the intent being invoked or included it in the skill builder below.
|
|
312
|
+ """
|
|
313
|
+ def can_handle(self, handler_input, exception):
|
|
314
|
+ # type: (HandlerInput, Exception) -> bool
|
|
315
|
+ return True
|
|
316
|
+
|
|
317
|
+ def handle(self, handler_input, exception):
|
|
318
|
+ # type: (HandlerInput, Exception) -> Response
|
|
319
|
+ logger.error(exception, exc_info=True)
|
|
320
|
+
|
|
321
|
+ speak_output = "Sorry, I had trouble doing what you asked. Please try again."
|
|
322
|
+
|
|
323
|
+ return (
|
|
324
|
+ handler_input.response_builder
|
|
325
|
+ .speak(speak_output)
|
|
326
|
+ .ask(speak_output)
|
|
327
|
+ .response
|
|
328
|
+ )
|
|
329
|
+
|
|
330
|
+# The SkillBuilder object acts as the entry point for your skill, routing all request and response
|
|
331
|
+# payloads to the handlers above. Make sure any new handlers or interceptors you've
|
|
332
|
+# defined are included below. The order matters - they're processed top to bottom.
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+sb = SkillBuilder()
|
|
336
|
+try:
|
|
337
|
+ resp = requests.get('http://136.145.231.48:5000/retrieveMenu')
|
|
338
|
+ if resp.status_code == 200:
|
|
339
|
+ menu_full = resp.json()
|
|
340
|
+ food_dict = menu_full['comidas']
|
|
341
|
+ pasta_dict = comida['Pasta']
|
|
342
|
+ drink_dict = menu_full['bebidas']
|
|
343
|
+ else:
|
|
344
|
+ menu_full = {}
|
|
345
|
+ food_dict = {}
|
|
346
|
+ pasta_dict = {}
|
|
347
|
+ drink_dict = {}
|
|
348
|
+except:
|
|
349
|
+ menu_full = {}
|
|
350
|
+ food_dict = {}
|
|
351
|
+ pasta_dict = {}
|
|
352
|
+ drink_dict = {}
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+order = {}
|
|
356
|
+order_names = {}
|
|
357
|
+
|
|
358
|
+sb.add_request_handler(LaunchRequestHandler())
|
|
359
|
+#sb.add_request_handler(HelloWorldIntentHandler())
|
|
360
|
+#sb.add_request_handler(HelpIntentHandler())
|
|
361
|
+sb.add_request_handler(LinguiniOrderFoodIntentHandler())
|
|
362
|
+sb.add_request_handler(LinguiniOrderDrinkIntentHandler())
|
|
363
|
+sb.add_request_handler(LinguiniOrderNothingIntentHandler())
|
|
364
|
+sb.add_request_handler(CancelIntentHandler())
|
|
365
|
+sb.add_request_handler(StopIntentHandler())
|
|
366
|
+sb.add_request_handler(LinguiniOrderReviewIntentHandler())
|
|
367
|
+sb.add_request_handler(LinguiniOrderFinishIntentHandler())
|
|
368
|
+sb.add_request_handler(SessionEndedRequestHandler())
|
|
369
|
+sb.add_request_handler(IntentReflectorHandler()) # make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
|
|
370
|
+
|
|
371
|
+sb.add_exception_handler(CatchAllExceptionHandler())
|
|
372
|
+
|
|
373
|
+lambda_handler = sb.lambda_handler()
|