# -*- coding: utf-8 -*- # This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK for Python. # Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management, # session persistence, api calls, and more. # This sample is built using the handler classes approach in skill builder. import requests import csv import logging import ask_sdk_core.utils as ask_utils from ask_sdk_core.skill_builder import SkillBuilder 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 logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) global ordernum ordernum = 0 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 = "Welcome to Linguini." return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) #_________________________Alejandro Rodriguez's functions______________________________ class MenuIntentHandler(AbstractRequestHandler): """Handler for Menu Intent.""" def can_handle(self, handler_input): return ask_utils.is_intent_name("MenuIntent")(handler_input) def handle(self, handler_input): url = 'http://136.145.231.48:5000/retrieveMenu' menuitems = requests.get(url).json() categories = "" Maxsize = len(menuitems['bebidas']) + len(menuitems['comidas']) counter = 0 for BigCategory in menuitems: for category in menuitems[BigCategory]: if counter == (Maxsize - 1): categories += "and " + category + "." else: categories += category + ", " counter = counter + 1 speak_output = "The menu categories are: " + categories return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) class PizzaIntentHandler(AbstractRequestHandler): """Handler for Pizza Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("PizzaIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response url = 'http://136.145.231.48:5000/retrieveMenu' menuitems = requests.get(url).json() categories = "" Maxsize = len(menuitems['comidas']['Pizza']) counter = 0 for item in menuitems['comidas']['Pizza']: if counter == (Maxsize - 1): categories += "and " + menuitems['comidas']['Pizza'][item]['nombre'] + "." else: categories += menuitems['comidas']['Pizza'][item]['nombre'] + ", " counter = counter + 1 speak_output = "The pizzas categories are: " + categories return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) class PastaIntentHandler(AbstractRequestHandler): """Handler for Pasta Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("PastaIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response url = 'http://136.145.231.48:5000/retrieveMenu' menuitems = requests.get(url).json() categories = "" Maxsize = len(menuitems['comidas']['Pasta']) counter = 0 for item in menuitems['comidas']['Pasta']: if counter == (Maxsize - 1): categories += "and " + menuitems['comidas']['Pasta'][item]['nombre'] + "." else: categories += menuitems['comidas']['Pasta'][item]['nombre'] + ", " counter = counter + 1 speak_output = "The pastas categories are: " + categories return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) class HamburgerIntentHandler(AbstractRequestHandler): """Handler for Pasta Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("HamburgerIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response url = 'http://136.145.231.48:5000/retrieveMenu' menuitems = requests.get(url).json() categories = "" Maxsize = len(menuitems['comidas']['Hamburguesa']) counter = 0 for item in menuitems['comidas']['Hamburguesa']: if counter == (Maxsize - 1): categories += "and " + menuitems['comidas']['Hamburguesa'][item]['nombre'] + "." else: categories += menuitems['comidas']['Hamburguesa'][item]['nombre'] + ", " counter = counter + 1 speak_output = "The Hamburger categories are: " + categories return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) class DrinksIntentHandler(AbstractRequestHandler): """Handler for Pasta Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("DrinkIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response url = 'http://136.145.231.48:5000/retrieveMenu' menuitems = requests.get(url).json() categories = "" Maxsize = len(menuitems['bebidas']['']) counter = 0 for item in menuitems['comidas']['Hamburguesa']: if counter == (Maxsize - 1): categories += "and " + menuitems['comidas']['Hamburguesa'][item]['nombre'] + "." else: categories += menuitems['comidas']['Hamburguesa'][item]['nombre'] + ", " counter = counter + 1 speak_output = "The Hamburger categories are: " + categories return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) #________________________________________________________________ #Esta proxima seccion de codigo fue hecha por Pablo Loyola class LinguiniOrderFoodIntentHandler(AbstractRequestHandler): """Handler for Linguini Order Food Intent.""" global ordernum ordernum = ordernum + 1 global food_dict global order def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("LinguiniOrderFoodIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response slots = handler_input.request_envelope.request.intent.slots food_type = slots["food_type"].value food_category = slots["food_category"].value speak_output = f"One {food_type} {food_category} added to your order. What else would you like to add?" food = f"{food_type} {food_category}" if food == "gardenia pasta": orderstr = 'orden' + str(ordernum) order[orderstr] = 2 else: orderstr = 'orden' + str(ordernum) order[ordernum] = 1 if food in order_names: order_names[food] += 1 else: order_names[food] = 1 handler_input.response_builder.speak(speak_output).ask("What else would you like to add?") return (handler_input.response_builder.response) class LinguiniOrderDrinkIntentHandler(AbstractRequestHandler): """Handler for Linguini Order Drink Intent.""" global ordernum ordernum = ordernum + 1 global drink_dict global order global order_names def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("LinguiniOrderDrinkIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response slots = handler_input.request_envelope.request.intent.slots drink_type = slots["drink_type"].value speak_output = f"One {drink_type} added to your order. What else would you like to add?" if drink_type == "water": orderstr = 'orden' + str(ordernum) order[orderstr] = 10 else: orderstr = 'orden' + str(ordernum) order[orderstr] = 9 if drink_type in order_names: order_names[drink_type] += 1 else: order_names[drink_type] = 1 handler_input.response_builder.speak(speak_output).ask("What else would you like to add?") return (handler_input.response_builder.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 CancelIntentHandler(AbstractRequestHandler): """Handler for Cancel Intent.""" global order def can_handle(self, handler_input): # type: (HandlerInput) -> bool return (ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input)) def handle(self, handler_input): # type: (HandlerInput) -> Response speak_output = "Your order has been canceled." order.clear() return ( handler_input.response_builder .speak(speak_output) .ask("reprompt") .response ) class LinguiniOrderNothingIntentHandler(AbstractRequestHandler): """Handler for Stop Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return (ask_utils.is_intent_name("LinguiniOrderNothingIntent")(handler_input)) def handle(self, handler_input): # type: (HandlerInput) -> Response speak_output = "Alright! Your order is complete. Would you like to review, finish, or cancel your order" return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response ) class LinguiniOrderReviewIntentHandler(AbstractRequestHandler): global order """Handler for Hello World Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("LinguiniOrderReviewIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response speak_output = "You have " for i in order_names: speak_output += f"{order_names[i]} {i}, " speak_output = speak_output[:-2] speak_output += ". If everything seems okay, say finish to submit order or say cancel to cancel and restart order." 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 LinguiniOrderFinishIntentHandler(AbstractRequestHandler): global order global order_names """Handler for Hello World Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("LinguiniOrderFinishIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response #Send order to Restaurant lmao full_order = [{'nombre': 'Aranpompano','celular': '(555)-555-5555', 'ordenCompleta': order, 'peticiones': {}}] #full_order = [{'nombre': 'Scott Pilgrim','celular': '(555)-555-5555', 'ordenCompleta': {'orden1':1, 'orden2': 10}, 'peticiones': {}}] try: r = requests.post('http://136.145.231.48:5000/papelonDeComida', json=full_order) except: r = None if r == None: speak_output = "Sorry, I couldn't connect to the restaurant. Please try again later." elif(r.status_code == 200): speak_output = f'Alright! {r.text}' else: speak_output = "Uh oh! I couldn't place your order, please try again later." #REPLACE THIS WHEN API IS IMPLEMENTED order.clear() order_names.clear() #REPLACE THIS WHEN API IS IMPLEMENTED 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 = "Las categorias son: pizzas, pastas, ensaladas, postres y por ultimo las bebidas. Cual desea saber mas?" 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 = "Tenga buen dia!" 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 ) # The SkillBuilder object acts as the entry point for your skill, routing all request and response # payloads to the handlers above. Make sure any new handlers or interceptors you've # defined are included below. The order matters - they're processed top to bottom. sb = SkillBuilder() sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(DrinksIntentHandler()) sb.add_request_handler(MenuIntentHandler()) sb.add_request_handler(PastaIntentHandler()) sb.add_request_handler(PizzaIntentHandler()) sb.add_request_handler(HamburgerIntentHandler()) sb.add_request_handler(LinguiniOrderFoodIntentHandler()) sb.add_request_handler(LinguiniOrderDrinkIntentHandler()) sb.add_request_handler(LinguiniOrderNothingIntentHandler()) sb.add_request_handler(LinguiniOrderReviewIntentHandler()) sb.add_request_handler(LinguiniOrderFinishIntentHandler()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(CancelOrStopIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) sb.add_request_handler(IntentReflectorHandler()) # THIS ONE HAS TO GO LAST sb.add_exception_handler(CatchAllExceptionHandler()) lambda_handler = sb.lambda_handler()