-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
319 lines (241 loc) · 13 KB
/
lambda_function.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
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
# -*- coding: utf-8 -*-
"""Simple fact sample app."""
import random
import logging
import json
import prompts
import os
import boto3
import time
from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.dispatch_components import (
AbstractRequestHandler, AbstractExceptionHandler,
AbstractRequestInterceptor, AbstractResponseInterceptor)
from ask_sdk_core.utils import is_request_type, is_intent_name
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model.ui import SimpleCard
from ask_sdk_model import Response
# --------------------------------------------------------------------- #
# Clase Status donde guardaremos el progreso globals
# Clase Result donde guardaremos el progreso de cada pregunta
from status import *
from result import *
# Configuración de la persistencia
from ask_sdk_dynamodb.adapter import DynamoDbAdapter
from ask_sdk_core.skill_builder import CustomSkillBuilder
ddb_region = os.environ.get('DYNAMODB_PERSISTENCE_REGION')
ddb_table_name = os.environ.get('DYNAMODB_PERSISTENCE_TABLE_NAME')
ddb_resource = boto3.resource('dynamodb', region_name=ddb_region)
dynamodb_adapter = DynamoDbAdapter(table_name=ddb_table_name, create_table=False, dynamodb_resource=ddb_resource)
# sb = SkillBuilder() // En nuestro caso debe ser CustomSkillBuilder
sb = CustomSkillBuilder(persistence_adapter = dynamodb_adapter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
DELAY = {
'low' :[4.130, 4.560, 3.000, 2.370, 3.090, 4.060, 2.550, 4.160, 6.010, 3.570, 3.490, 3.110, 1.570, 3.490, 5.100],
'normal' :[3.480, 3.530, 2.090, 1.580, 2.510, 3.060, 2.040, 3.110, 4.520, 3.010, 2.530, 2.150, 1.520, 2.570, 4.030], # Tiempos (x1) Normal
'fast' :[]
}
SCORE_Yes_15 = [NO, YES, YES, YES, NO, YES, NO, YES, YES, YES, NO, YES, NO, YES, YES]
class YesIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.YesIntent")(handler_input)
def handle(self, handler_input):
session_attr = handler_input.attributes_manager.session_attributes
status_obj = Status(status = session_attr['objeto'])
tiempo_actual = time.time()
delay = DELAY['normal'][status_obj.current_item-1] # -1 Pues la lista empieza en 0
result = status_obj.results[status_obj.current_item]
result.answer = YES
result.time = tiempo_actual - (status_obj.t_ini_q + delay)
session_attr['objeto'] = status_obj.to_dict()
return NextIntentHandler().handle(handler_input)
class NoIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return is_intent_name("AMAZON.NoIntent")(handler_input)
def handle(self, handler_input):
session_attr = handler_input.attributes_manager.session_attributes
status_obj = Status(status = session_attr['objeto'])
tiempo_actual = time.time()
delay = DELAY['normal'][status_obj.current_item-1]
result = status_obj.results[status_obj.current_item]
result.answer = NO
result.time = tiempo_actual - (status_obj.t_ini_q + delay)
status_obj.t_ini_q = tiempo_actual
if status_obj.current_item < Q1:
session_attr['objeto'] = status_obj.to_dict()
return RepeatIntentHandler().handle(handler_input)
else:
session_attr['objeto'] = status_obj.to_dict()
return NextIntentHandler().handle(handler_input)
class StartTestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("LaunchRequest")(handler_input)
def handle(self, handler_input):
data = handler_input.attributes_manager.request_attributes["_"]
persistence_attr = handler_input.attributes_manager.persistent_attributes
handler_input.attributes_manager.session_attributes = persistence_attr
session_attr = handler_input.attributes_manager.session_attributes
if 'objeto' in persistence_attr:
return RepeatIntentHandler().handle(handler_input)
else:
session_attr['objeto'] = Status(t_ini = time.time()).to_dict()
status_obj = Status(status = session_attr['objeto'])
return handler_input.response_builder.speak(data[prompts.KEYS[WELLCOME]]).ask(data[prompts.KEYS[WELLCOME]]).response
class NextIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return is_intent_name("AMAZON.NextIntent")(handler_input)
def handle(self, handler_input):
data = handler_input.attributes_manager.request_attributes["_"]
session_attr = handler_input.attributes_manager.session_attributes
status_obj = Status(status = session_attr['objeto'])
if (status_obj.current_item < Q15):
status_obj.current_item += 1
status_obj.t_ini_q = time.time()
session_attr['objeto'] = status_obj.to_dict()
handler_input.attributes_manager.persistent_attributes = session_attr
handler_input.attributes_manager.save_persistent_attributes()
return (
handler_input.response_builder
.speak(data[prompts.KEYS[status_obj.current_item]])
.ask(data[prompts.KEYS[status_obj.current_item]])
.response
)
else:
Current_ITEM = str(status_obj.current_item)
lista_ans = results_to_list_answers(status_obj)
final_score = set_final_score( lista_ans, SCORE_Yes_15)
status_obj = update_results_score(lista_ans, SCORE_Yes_15, status_obj)
status_obj.final_score = final_score
status_obj.t_end = time.time()
skill_total_time = status_obj.t_end - status_obj.t_ini
session_attr['objeto'] = status_obj.to_dict() # actualizar la sesion con el objeto
session_attr['objeto_t_ini'] = format_time(status_obj.t_ini)
session_attr['objeto_t_end'] = format_time(status_obj.t_end)
session_attr['objeto_t_total'] = format_time(skill_total_time)
handler_input.attributes_manager.persistent_attributes = session_attr
handler_input.attributes_manager.save_persistent_attributes()
return handler_input.response_builder.speak(data[prompts.GET_Bye]).response
class RepeatIntentHandler(AbstractRequestHandler):
"""Handler for Repeat Intent."""
def can_handle(self, handler_input):
return is_intent_name("AMAZON.RepeatIntent")(handler_input)
def handle(self, handler_input):
data = handler_input.attributes_manager.request_attributes["_"]
session_attr = handler_input.attributes_manager.session_attributes
status_obj = Status(status = session_attr['objeto'])
if status_obj.current_item >= Q1:
status_obj.results[status_obj.current_item].repe += 1
session_attr['objeto'] = status_obj.to_dict() # actualizar la sesion con el objeto
return handler_input.response_builder.speak(data[prompts.KEYS[status_obj.current_item]]).ask(data[prompts.KEYS[status_obj.current_item]]).response
class HelpIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.HelpIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
logger.info("In HelpIntentHandler")
# get localization data
data = handler_input.attributes_manager.request_attributes["_"]
speech = data[prompts.HELP_MESSAGE]
reprompt = data[prompts.HELP_REPROMPT]
handler_input.response_builder.speak(speech).ask(
reprompt).set_card(SimpleCard(
data[prompts.SKILL_NAME], speech))
return handler_input.response_builder.response
class CancelOrStopIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return (is_intent_name("AMAZON.CancelIntent")(handler_input) or
is_intent_name("AMAZON.StopIntent")(handler_input))
def handle(self, handler_input):
# type: (HandlerInput) -> Response
logger.info("In CancelOrStopIntentHandler")
# get localization data
data = handler_input.attributes_manager.request_attributes["_"]
speech = data[prompts.STOP_MESSAGE]
handler_input.response_builder.speak(speech)
return handler_input.response_builder.response
class FallbackIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
#return is_intent_name("AMAZON.FallbackIntent")(handler_input)
return True
def handle(self, handler_input):
# type: (HandlerInput) -> Response
logger.info("In FallbackIntentHandler")
return RepeatIntentHandler().handle(handler_input)
class LocalizationInterceptor(AbstractRequestInterceptor):
def process(self, handler_input):
locale = handler_input.request_envelope.request.locale
logger.info("Locale is {}".format(locale))
# localized strings stored in language_strings.json
with open("language_strings.json") as language_prompts:
language_data = json.load(language_prompts)
# set default translation data to broader translation
if locale[:2] in language_data:
data = language_data[locale[:2]]
# if a more specialized translation exists, then select it instead
# example: "fr-CA" will pick "fr" translations first, but if "fr-CA" translation exists,
# then pick that instead
if locale in language_data:
data.update(language_data[locale])
else:
data = language_data[locale]
handler_input.attributes_manager.request_attributes["_"] = data
class SessionEndedRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("SessionEndedRequest")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
logger.info("In SessionEndedRequestHandler")
logger.info("Session ended reason: {}".format(
handler_input.request_envelope.request.reason))
return handler_input.response_builder.response
# Exception Handler
class CatchAllExceptionHandler(AbstractExceptionHandler):
def can_handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> bool
return True
def handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> Response
logger.info("In CatchAllExceptionHandler")
logger.error(exception, exc_info=True)
data = handler_input.attributes_manager.request_attributes["_"]
# handler_input.response_builder.speak(EXCEPTION_MESSAGE).ask(
handler_input.response_builder.speak(data[prompts.GET_EXCEPTION_TEST]).ask(
data[prompts.GET_EXCEPTION_TEST])
return handler_input.response_builder.response
# Request and Response loggers
class RequestLogger(AbstractRequestInterceptor):
"""Log the alexa requests."""
def process(self, handler_input):
# type: (HandlerInput) -> None
logger.info("Alexa Request: {}".format(
handler_input.request_envelope.request))
class ResponseLogger(AbstractResponseInterceptor):
"""Log the alexa responses."""
def process(self, handler_input, response):
# type: (HandlerInput, Response) -> None
logger.debug("Alexa Response: {}".format(response))
# Register intent handlers
sb.add_request_handler(YesIntentHandler())
sb.add_request_handler(NoIntentHandler())
sb.add_request_handler(StartTestHandler())
sb.add_request_handler(NextIntentHandler())
sb.add_request_handler(RepeatIntentHandler())
sb.add_request_handler(HelpIntentHandler())
sb.add_request_handler(CancelOrStopIntentHandler())
sb.add_request_handler(FallbackIntentHandler())
sb.add_request_handler(SessionEndedRequestHandler())
# Register exception handlers
sb.add_exception_handler(CatchAllExceptionHandler())
# Register request and response interceptors
sb.add_global_request_interceptor(LocalizationInterceptor())
sb.add_global_request_interceptor(RequestLogger())
sb.add_global_response_interceptor(ResponseLogger())
# Handler name that is used on AWS lambda
lambda_handler = sb.lambda_handler()