Search code examples
amazon-web-servicesaws-lambdaalexa-skills-kit

How to get and use confirmation 'yes' or 'no' for Alexa skill intent response


I am developing a Alexa skill in which on launch it will ask Do you want to perform something ?
Depending upon user's reply 'yes' or 'no' I want to launch another intent.

var handlers = {
  'LaunchRequest': function () {
    let prompt = this.t("ASK_FOR_SOMETHING");
    let reprompt = this.t("LAUNCH_REPROMPT");
    this.response.speak(this.t("WELCOME_MSG") + ' ' + prompt).listen(reprompt);
    this.emit(':responseReady');
  },
  "SomethingIntent": function () {
    //Launch this intent if the user's response is 'yes'
  }
};

I did have a look at dialog model and it seems that it will serve the purpose. But I am not sure how to implement it.


Solution

  • The simplest way to do what you're looking for from the skill, is to handle the AMAZON.YesIntent and AMAZON.NoIntent from your skill (make sure to add them to the interaction model as well):

    var handlers = {
      'LaunchRequest': function () {
        let prompt = this.t("ASK_FOR_SOMETHING");
        let reprompt = this.t("LAUNCH_REPROMPT");
        this.response.speak(this.t("WELCOME_MSG") + ' ' + prompt).listen(reprompt);
        this.emit(':responseReady');
      },
      "AMAZON.YesIntent": function () { 
        // raise the `SomethingIntent` event, to pass control to the "SomethingIntent" handler below 
        this.emit('SomethingIntent');
      },
      "AMAZON.NoIntent": function () {
        // handle the case when user says No
        this.emit(':responseReady');
      }
      "SomethingIntent": function () {
        // handle the "Something" intent here
      }
    };
    

    Note, that in a more complex skill you might have to store some state to figure out that the user sent a 'Yes' intent in response to your question as to whether to "do something". You can save this state using the skill session attributes in the session object. For instance:

    var handlers = {
      'LaunchRequest': function () {
        let prompt = this.t("ASK_FOR_SOMETHING");
        let reprompt = this.t("LAUNCH_REPROMPT");
        this.response.speak(this.t("WELCOME_MSG") + ' ' + prompt).listen(reprompt);
        this.attributes.PromptForSomething = true;
        this.emit(':responseReady');
      },
      "AMAZON.YesIntent": function () { 
        if (this.attributes.PromptForSomething === true) {
          // raise the `SomethingIntent` event, to pass control to the "SomethingIntent" handler below 
          this.emit('SomethingIntent');
        } else {
          // user replied Yes in another context.. handle it some other way
          //  .. TODO ..
          this.emit(':responseReady');
        }
      },
      "AMAZON.NoIntent": function () {
        // handle the case when user says No
        this.emit(':responseReady');
      }
      "SomethingIntent": function () {
        // handle the "Something" intent here
        //  .. TODO ..
      }
    };
    

    Finally, you could also look into using the Dialog Interface as you alluded to in your question but if all you're trying to do is get a simple Yes/No confirmation as a prompt from the launch request than I think my example above would be pretty straight forward to implement.