# -*- coding: utf-8 -*- import logging import ask_sdk_core.utils as ask_utils from ask_sdk_core.skill_builder import SkillBuilder from ask_sdk_core.skill_builder import CustomSkillBuilder from ask_sdk_core.dispatch_components import AbstractRequestHandler from ask_sdk_core.dispatch_components import AbstractExceptionHandler from ask_sdk_core.handler_input import HandlerInput from ask_sdk_model import Response from ask_sdk_s3.adapter import S3Adapter s3_adapter = S3Adapter(bucket_name="martinig-alexathon") logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) class HasBirthdayLaunchRequestHandler(AbstractRequestHandler): """Handler for launch after they have set their birthday""" def can_handle(self, handler_input): # extract persistent attributes and check if they are all present attr = handler_input.attributes_manager.persistent_attributes attributes_are_present = ("ano" in attr and "mes" in attr and "dia" in attr) return attributes_are_present and ask_utils.is_request_type("LaunchRequest")(handler_input) def handle(self, handler_input): attr = handler_input.attributes_manager.persistent_attributes ano = attr['ano'] mes = attr['mes'] dia = attr['dia'] speak_output = "Bem vindo de volta, eu lembro que nasceu dia {dia} de {mes} de {ano}".format(dia=dia,mes=mes,ano=ano) handler_input.response_builder.speak(speak_output) return handler_input.response_builder.response class LaunchRequestHandler(AbstractRequestHandler): """Handler for Skill Launch.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_request_type("LaunchRequest")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response speak_output = "Bem vindo, vou te ajudar no seu aniversário,quando você nasceu?" speak_output_reprompt = "quando você nasceu?" return ( handler_input.response_builder .speak(speak_output) .ask(speak_output_reprompt) .response ) class CaptureBirthdayIntentHandler(AbstractRequestHandler): """Handler for Hello World Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("CaptureBirthdayIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response slots = handler_input.request_envelope.request.intent.slots dia = slots["dia"].value mes = slots["mes"].value ano = slots["ano"].value speak_output = "Obrigado, vou lembrar que nasceu dia em {dia} de {mes} de {ano}".format(dia=dia, mes=mes, ano=ano) attributes_manager = handler_input.attributes_manager birthday_attributes = { "ano": ano, "mes": mes, "dia": dia } attributes_manager.persistent_attributes = birthday_attributes attributes_manager.save_persistent_attributes() return ( handler_input.response_builder .speak(speak_output) # .ask("add a reprompt if you want to keep the session open for the user to respond") .response ) class HelpIntentHandler(AbstractRequestHandler): """Handler for Help Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("AMAZON.HelpIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response speak_output = "You can say hello to me! How can I help?" return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) class CancelOrStopIntentHandler(AbstractRequestHandler): """Single handler for Cancel and Stop Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return (ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input) or ask_utils.is_intent_name("AMAZON.StopIntent")(handler_input)) def handle(self, handler_input): # type: (HandlerInput) -> Response speak_output = "Goodbye!" return ( handler_input.response_builder .speak(speak_output) .response ) class SessionEndedRequestHandler(AbstractRequestHandler): """Handler for Session End.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_request_type("SessionEndedRequest")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response # Any cleanup logic goes here. return handler_input.response_builder.response class IntentReflectorHandler(AbstractRequestHandler): """The intent reflector is used for interaction model testing and debugging. It will simply repeat the intent the user said. You can create custom handlers for your intents by defining them above, then also adding them to the request handler chain below. """ def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_request_type("IntentRequest")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response intent_name = ask_utils.get_intent_name(handler_input) speak_output = "You just triggered " + intent_name + "." return ( handler_input.response_builder .speak(speak_output) # .ask("add a reprompt if you want to keep the session open for the user to respond") .response ) class CatchAllExceptionHandler(AbstractExceptionHandler): """Generic error handling to capture any syntax or routing errors. If you receive an error stating the request handler chain is not found, you have not implemented a handler for the intent being invoked or included it in the skill builder below. """ def can_handle(self, handler_input, exception): # type: (HandlerInput, Exception) -> bool return True def handle(self, handler_input, exception): # type: (HandlerInput, Exception) -> Response logger.error(exception, exc_info=True) speak_output = "Sorry, I had trouble doing what you asked. Please try again." return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) sb = CustomSkillBuilder(persistence_adapter=s3_adapter) sb.add_request_handler(HasBirthdayLaunchRequestHandler()) sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(CaptureBirthdayIntentHandler()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(CancelOrStopIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) sb.add_request_handler(IntentReflectorHandler()) sb.add_exception_handler(CatchAllExceptionHandler()) lambda_handler = sb.lambda_handler()