Search code examples
node.jsalexa-skills-kitdice

Amazon alexa skill game, managing program flow


I am creating a simple amazon alexa game. I have all the intents I need for the game, but my question is how do I make sure you can only choose valid options? Example: If alexa ask me a yes or no answer how to a promt for this? Currently: If you answer with 10 example, it will say "You just triggered NumberIntent", I want it to say "Please choose a valid option" and then repromt until it gets a yes or no. I am currently using the canhandle on the intents but it doesn't help for the program crashing.

Would be nice if anyone can help me!


Solution

  • You shouldn't try to force the user to respond to a specific question.

    Vocal design is different than visual design. On a website, I can click wherever I want.

    Alexa's interaction and vocal design in general is like a deck of card. Each card is an intent.
    And as a user, I can choose to pick any card whenever I want.

    As a user, I can say :

    • repeat the question
    • ask for help
    • launch another specific intent
    • or maybe I just want to quit the skill.

    The card picked won't be the one you expect. It will go to either an intent you've defined or AMAZON.FallbackIntent

    So be careful with that.
    Maybe If I say something different, it's because I want to do something different.


    If you still want to implement it on specific intent, you need to adapt the logic based on the user's progression.

    • After you ask the user a question, you save the progression in the sessionAttribute
    
    const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
    sessionAttributes.progression = "EXPECT_YES_OR_NO";
    
    • Within your intent, you add a logic to detect if the user should first response to yes or no
    canHandle(handlerInput) {
        return (
          Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
          Alexa.getIntentName(handlerInput.requestEnvelope) ===
            "MySpecificIntent"
        );
    },
    handle(handlerInput) {
        const { responseBuilder } = handlerInput;
        const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
    
        if(sessionAttributes.progression === "EXPECT_YES_OR_NO") {
            const utt = "I didn't catch that, do you like StackOverflow? You can say yes or no."
            return responseBuilder.speak(utt).reprompt(utt).getResponse();
        }
    },
    

    I recommend you to follow alexa skill sample tutorial zero to hero it summarise everything you need to know about developing a skill on Alexa with examples & videos.