def Unhandled(self, handlers, persistant_attributes, attributes, slots): persistant_attributes[core.STATE_KEY] = core.States.INITIAL_STATE if attributes['new']: return responder.tell("We've already been talking" \ " but I have no idea what about, so I will exit this session. Please" \ " start over by saying, Alexa launch my cookbook.") else: return responder.tell("Hey, what's up.")
def AMAZON_YesIntent(self, handlers, persistant_attributes, attributes, slots): if 'current_recipe' not in attributes or 'tmp_state' not in attributes: return responder.tell( "Sorry, but I've forgotten which recipe you wanted to save. You'll have to start over.") current_recipe = attributes['current_recipe'] persistant_attributes[core.STATE_KEY] = attributes['tmp_state'] persistant_attributes['recipes'].append(current_recipe) return responder.tell('Recipe overwritten')
def dispatch(self, state, persistant_attributes, attributes, event): request_type = event['request']['type'] intent = "" slots = {} if request_type == requester.Types.LAUNCH: intent = requester.Types.LAUNCH elif request_type == requester.Types.INTENT: if 'slots' in event['request']['intent']: slots = event['request']['intent']['slots'] else: slots = {} intent = event['request']['intent']['name'] elif request_type == requester.Types.END: intent = requester.Types.END else: return responder.tell("I'm not sure what your intent is. Try asking differently") # translate AMAZON\.(.+) into AMAZON_$1 intent = re.sub(r'AMAZON\.(.+)', r'AMAZON_\1', intent) stateful_intent = intent + state # now we want to try to find a handler fo this intent # we first try the exact intent, then that intent without the state # then the unhandled intent with the state, and then unhandled without state if state in self.handlers: if hasattr(self.handlers[state], intent): handler_method = getattr(self.handlers[state], intent) logging.getLogger(core.LOGGER).info("found handler for %s" % stateful_intent) return handler_method(self.handlers, persistant_attributes, attributes, slots) # try intent without state if hasattr(self.handlers[core.States.STATELESS], intent): handler_method = getattr(self.handlers[core.States.STATELESS], intent) logging.getLogger(core.LOGGER).info("found handler for stateless intent") return handler_method(self.handlers, persistant_attributes, attributes, slots) # next try Unhandled for that state if state in self.handlers: if hasattr(self.handlers[state], 'Unhandled'): handler_method = getattr(self.handlers[state], 'Unhandled') logging.getLogger(core.LOGGER).info("found handler for stateful unhandled") attributes['INTENT'] = intent return handler_method(self.handlers, persistant_attributes, attributes, slots) # stateless unhandled is last resort if hasattr(self.handlers[core.States.STATELESS], 'Unhandled'): handler_method = getattr(self.handlers[core.States.STATELESS], 'Unhandled') logging.getLogger(core.LOGGER).info("found handler for stateless unhandled") attributes['INTENT'] = intent return handler_method(self.handlers, persistant_attributes, attributes, slots) logging.getLogger(core.LOGGER).info("found no handlers") return responder.tell("I'm not sure what you want. Try saying start over.")
def FavoriteIntent(self, handlers, persistant_attributes, attributes, slots): if 'current_recipe' in attributes: current_recipe = attributes['current_recipe'] if 'recipes' in persistant_attributes: if current_recipe in persistant_attributes['recipes']: attributes['tmp_state'] = attributes[core.STATE_KEY] attributes[ core.STATE_KEY] = core.States.CONFIRM_OVERWRITE_RECIPE return responder.ask( "This recipe is already in your cookbook. \ If you want to overwrite the existing recipe with this one, \ say yes. Say no to cancel and leave the existing recipe.", "Do you want to overwrite with this recipe?", attributes) else: # it's new, so add it persistant_attributes['recipes'].append(current_recipe) else: # recipe list doesn't exist persistant_attributes['recipes'] = [current_recipe] else: return responder.tell( "I can't favorite a recipe because we're not working on one. \ Try searching for a recipe first")
def InstructionsIntent(self, handlers, persistant_attributes, attributes, slots): # check we've got a working recipe at the moment if 'current_recipe' not in attributes: return responder.tell("I can't start instructions because you haven't picked a recipe.") step_number = attributes.get('step_number', 0) instructions = attributes['current_recipe']['Instructions'] if len(instructions) <= step_number: return responder.tell("this recipe doesn't have any instructions.") instruction = instructions[step_number] card = recipe_reader.instructions_card(attributes['current_recipe']) return responder.ask_with_card( instruction + ". <break time=2/> would you like to hear the next step?", None, "Instructions", card, None, attributes)
def AMAZON_NoIntent(self, handlers, persistant_attributes, attributes, slots): # search for another recipe online? # for now, just give up and reset. persistant_attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell( "That's all the recipes I know about. Try asking to make something different." )
def IngredientsIntent(self, handlers, persistant_attributes, attributes, slots): # check we've got a working recipe at the moment if 'current_recipe' not in attributes: return responder.tell("I can't list ingredients because you haven't picked a recipe.") speech = recipe_reader.ingredients_speech(attributes['current_recipe']) card = recipe_reader.ingredients_card(attributes['current_recipe']) return responder.ask_with_card("The ingredients are. " + speech + ". Do you want to hear \ instructions, or ingredients again?", None, "Ingredients", card, None, attributes)
def AMAZON_YesIntent(self, handlers, persistant_attributes, attributes, slots): # take the time to download the recipe if 'search_recipe_result' not in attributes: return responder.tell( "I've forgotten which recipe you wanted to make. Please start over") name = attributes['search_recipe_result']['Title'] recipe_id = attributes['search_recipe_result']['RecipeID'] recipe = recipes_helper.get_online_recipe(attributes['search_recipe_result']) if not recipe: return responder.tell("Sorry, I couldn't find the recipe %s, with id %s" % (name, recipe_id)) attributes['current_recipe'] = recipe attributes[core.STATE_KEY] = core.States.INGREDIENTS_OR_INSTRUCTIONS return responder.ask("Do you want to start with the ingredients or the instructions?", None, attributes)
def AMAZON_YesIntent(self, handlers, persistant_attributes, attributes, slots): # search for recipe if 'current_recipe_name' not in attributes: return responder.tell( "I'm not sure what recipe you are searching for. Please start over") recipe_name = attributes['current_recipe_name'] recipes = recipes_helper.search_online_recipes(recipe_name) if len(recipes) == 0: persistant_attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell("I don't know of any recipes for that. Try something else") else: attributes[core.STATE_KEY] = core.States.ASK_MAKE_ONLINE best_guess_recipe_name = recipes[0]['Title'] attributes['search_recipe_result'] = recipes[0] return responder.ask( "I found a recipe for " + best_guess_recipe_name + ". Do you want to use that?", None, attributes)
def AMAZON_YesIntent(self, handlers, persistant_attributes, attributes, slots): if 'current_recipe' not in attributes: # TODO: ask "which recipe do you want to favorite return responder.tell( "I don't know which recipe you want me to favorite.") recipe = attributes['current_recipe'] #recipes_helper.favorite_recipe(recipe) #return responder.tell("Recipe favorited.") raise NotImplementedError()
def LaunchRequest(self, handlers, persistant_attributes, attributes, slots): if persistant_attributes['invocations'] == 1: # first time! Say hello! attributes[core.STATE_KEY] = core.States.ASK_TUTORIAL return responder.ask( "Hi, I'm your new cookbook. Would you like start off with a tutorial?", None, attributes) else: if attributes['new']: attributes[core.STATE_KEY] = core.States.NEW_RECIPE return responder.ask("Welcome back. What would you like to make?", None, attributes) else: persistant_attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell("I've already been launched.")
def AMAZON_YesIntent(self, handlers, persistant_attributes, attributes, slots): # search for recipe if 'current_recipe_name' not in attributes: return responder.tell( "I'm not sure what recipe you are searching for. Please start over" ) recipe_name = attributes['current_recipe_name'] recipes = recipes_helper.search_online_recipes(recipe_name) if len(recipes) == 0: persistant_attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell( "I don't know of any recipes for that. Try something else") else: attributes[core.STATE_KEY] = core.States.ASK_MAKE_ONLINE best_guess_recipe_name = recipes[0]['Title'] attributes['search_recipe_result'] = recipes[0] return responder.ask( "I found a recipe for " + best_guess_recipe_name + ". Do you want to use that?", None, attributes)
def AMAZON_YesIntent(self, handlers, persistant_attributes, attributes, slots): # take the time to download the recipe if 'search_recipe_result' not in attributes: return responder.tell( "I've forgotten which recipe you wanted to make. Please start over" ) name = attributes['search_recipe_result']['Title'] recipe_id = attributes['search_recipe_result']['RecipeID'] recipe = recipes_helper.get_online_recipe( attributes['search_recipe_result']) if not recipe: return responder.tell( "Sorry, I couldn't find the recipe %s, with id %s" % (name, recipe_id)) attributes['current_recipe'] = recipe attributes[core.STATE_KEY] = core.States.INGREDIENTS_OR_INSTRUCTIONS return responder.ask( "Do you want to start with the ingredients or the instructions?", None, attributes)
def LaunchRequest(self, handlers, persistant_attributes, attributes, slots): if persistant_attributes['invocations'] == 1: # first time! Say hello! attributes[core.STATE_KEY] = core.States.ASK_TUTORIAL return responder.ask( "Hi, I'm your new cookbook. Would you like start off with a tutorial?", None, attributes) else: if attributes['new']: attributes[core.STATE_KEY] = core.States.NEW_RECIPE return responder.ask( "Welcome back. What would you like to make?", None, attributes) else: persistant_attributes[ core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell("I've already been launched.")
def FavoriteIntent(self, handlers, persistant_attributes, attributes, slots): if 'current_recipe' in attributes: current_recipe = attributes['current_recipe'] if 'recipes' in persistant_attributes: if current_recipe in persistant_attributes['recipes']: attributes['tmp_state'] = attributes[core.STATE_KEY] attributes[core.STATE_KEY] = core.States.CONFIRM_OVERWRITE_RECIPE return responder.ask("This recipe is already in your cookbook. \ If you want to overwrite the existing recipe with this one, \ say yes. Say no to cancel and leave the existing recipe.", "Do you want to overwrite with this recipe?", attributes) else: # it's new, so add it persistant_attributes['recipes'].append(current_recipe) else: # recipe list doesn't exist persistant_attributes['recipes'] = [current_recipe] else: return responder.tell("I can't favorite a recipe because we're not working on one. \ Try searching for a recipe first")
def SessionEndedRequest(self, handlers, persistant_attributes, attributes, slots): persistant_attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell("Goodbye.")
def AMAZON_StartOverIntent(self, handlers, persistant_attributes, attributes, slots): attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell( "Alright, I've reset everything. I'm ready to start a new recipe.")
def AMAZON_StartOverIntent(self, handlers, persistant_attributes, attributes, slots): attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell("Alright, I've reset everything. I'm ready to start a new recipe.")
def AMAZON_YesIntent(self, handlers, persistant_attributes, attributes, slots): return responder.tell( "I am capable of finding recipes and walking you through making them. Try asking how to make pancakes")
def Unhandled(self, handlers, persistant_attributes, attributes, slots): # presist current state and end interaction persistant_attributes[core.STATE_KEY] = attributes[core.STATE_KEY] return responder.tell("I'm confused. Try asking me what you want to make.")
def AMAZON_NoIntent(self, handlers, persistant_attributes, attributes, slots): # search for another recipe online? # for now, just give up and reset. persistant_attributes[core.STATE_KEY] = core.States.INITIAL_STATE return responder.tell( "That's all the recipes I know about. Try asking to make something different.")
def test_tell(self): response = responder.tell("tell") self.assertTrue(responder.is_valid(response))
def test_copy_attributes(self): request = requester.Request() response = responder.tell("test") request.copy_attributes(response) self.assertEqual(request.request['session']['attributes'], response['sessionAttributes'])
def AMAZON_NoIntent(self, handlers, persistant_attributes, attributes, slots): # not sure what to do here...? I'm starting to reach the limits of my currentb # state machine structure I think return responder.tell("Recipe not favorited.")
def AMAZON_NoIntent(self, handlers, persistant_attributes, attributes, slots): #TODO: consider how we might recover/fail if this isn't the case if 'tmp_state' in attributes: persistant_attributes[core.STATE_KEY] = attributes['tmp_state'] return responder.tell('Cancelling overwrite')
def Unhandled(self, handlers, persistant_attributes, attributes, slots): return responder.tell( "I'm sorry, I'm not sure what you mean. Do you want to make something?")
def Unhandled(self, handlers, persistant_attributes, attributes, slots): persistant_attributes[core.STATE_KEY] = attributes[core.STATE_KEY] return responder.tell( "Sorry, I'm not sure what you mean. Do you want to favorite this recipe?" )
def dispatch(self, state, persistant_attributes, attributes, event): request_type = event['request']['type'] intent = "" slots = {} if request_type == requester.Types.LAUNCH: intent = requester.Types.LAUNCH elif request_type == requester.Types.INTENT: if 'slots' in event['request']['intent']: slots = event['request']['intent']['slots'] else: slots = {} intent = event['request']['intent']['name'] elif request_type == requester.Types.END: intent = requester.Types.END else: return responder.tell( "I'm not sure what your intent is. Try asking differently") # translate AMAZON\.(.+) into AMAZON_$1 intent = re.sub(r'AMAZON\.(.+)', r'AMAZON_\1', intent) stateful_intent = intent + state # now we want to try to find a handler fo this intent # we first try the exact intent, then that intent without the state # then the unhandled intent with the state, and then unhandled without state if state in self.handlers: if hasattr(self.handlers[state], intent): handler_method = getattr(self.handlers[state], intent) logging.getLogger(core.LOGGER).info("found handler for %s" % stateful_intent) return handler_method(self.handlers, persistant_attributes, attributes, slots) # try intent without state if hasattr(self.handlers[core.States.STATELESS], intent): handler_method = getattr(self.handlers[core.States.STATELESS], intent) logging.getLogger( core.LOGGER).info("found handler for stateless intent") return handler_method(self.handlers, persistant_attributes, attributes, slots) # next try Unhandled for that state if state in self.handlers: if hasattr(self.handlers[state], 'Unhandled'): handler_method = getattr(self.handlers[state], 'Unhandled') logging.getLogger( core.LOGGER).info("found handler for stateful unhandled") attributes['INTENT'] = intent return handler_method(self.handlers, persistant_attributes, attributes, slots) # stateless unhandled is last resort if hasattr(self.handlers[core.States.STATELESS], 'Unhandled'): handler_method = getattr(self.handlers[core.States.STATELESS], 'Unhandled') logging.getLogger( core.LOGGER).info("found handler for stateless unhandled") attributes['INTENT'] = intent return handler_method(self.handlers, persistant_attributes, attributes, slots) logging.getLogger(core.LOGGER).info("found no handlers") return responder.tell( "I'm not sure what you want. Try saying start over.")
def Unhandled(self, handlers, persistant_attributes, attributes, slots): # presist current state and end interaction persistant_attributes[core.STATE_KEY] = attributes[core.STATE_KEY] return responder.tell( "I'm confused. Try asking me what you want to make.")