How to return to original intent after user says Repeat during an elicit dialog on Alexa using Python?

Viewed 125
class RepeatIntentHandler(AbstractRequestHandler):
    """Handler for repeating the response to the user."""
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return is_intent_name("AMAZON.RepeatIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        #logger.info("In RepeatHandler")
        attr = handler_input.attributes_manager.session_attributes
        response_builder = handler_input.response_builder
        #if "recent_response" in attr:
        cached_response_str = json.dumps(attr["recent_response"])
        cached_response = DefaultSerializer().deserialize(cached_response_str, Response)
        #return cached_response
        return cached_response

I have used this code for repeat intent. when the user said to repeat it will repeat the question, but after repeating, when the user will give a response(give an answer to that repeated question), the flow will not return back to the pervious intent. and my skill was stopped.

I also tried UpdateIntent.

return cached_response.handler_input.response_builder.speak(speak_output).add_directive(delegate_directive.DelegateDirective(updated_intent=Intent(name="QuestionIntent"))).response  

but it will show an error like

AttributeError: 'Response' object has no attribute 'add_directive'

so how to solve this issue?

1 Answers

When you use ElicitDialog, if the user ask to repeat, then send back the directive for the elicitSlotDialogs in the repeatIntent.

Maybe you don't need to use ElicitSlot

Remember that Alexa is like a deck of card, the user can pick any card at anytime. Maybe I will ask you to repeat the question and then I will ask your skill to stop or to do anything else.

Alexa understand a sentence and redirect it to your skill within a specific intent. Only from the user voice, it doesn't have any logic to go back on any intent.

So if it's an Intent with custom slots, it should trigger your intent, but if it's an Intent like Amazon.YesIntent, then you'll have to implement the logic to continue your flow from the session attributes.

Ex:

  • User: Alexa, launch skill A. ---> LaunchRequest
  • Alexa: Do you like coffee ?
  • User: Repeat ---> RepeatIntent
  • Alexa: Do you like coffee ?
  • User: No ---> NoIntent (then in your logic, you have to define that it was the question for coffee)
  • Alexa: Ok, and do you like Tea?
  • User: No ---> NoIntent (Same behavior, you have to know within the NoIntent to save the user preference based on the question he was asked, and for that, use the session attributes to save the question asked: session_attributes["question"] = "userLikeTea")
Related