question

newuser-e65160ef-2a48-4001-b174-67a24621009f avatar image
newuser-e65160ef-2a48-4001-b174-67a24621009f asked ·

ASK: Asking for user response in the middle of an intent? (NodeJs)

I know the title might be confusing, so I'll give an example.

'GetDataIntent': function(){

    var body = "";
    var url = "someAPI.com";

    https.get(url, (response) =>{
        response.on('data', (chunk) => {
            body+=chunk;
        });
        response.on('end', (chunk) => {
            var data = JSON.parse(body);
            /* Do Some Stuff */


            this.emit(":ask", "Would you like more info?");

            //The Concept I am trying to describe
            Alexa.registerHandlers(innerHandlers);
            var innerHandlers = {
                'AMAZON.YesIntent': function(){
                     this.emit(":tell", "More info");
                     /* then destroy the temp handlers */
                 },
                'AMAZON.NoIntent': function(){
                     this.emit(":tell", "Goodbye!");
                     /* Destroy the temp handlers */
                 }
            };
        }); 
});
}

From the documentation I have read about ASK, it seems that you would have to register handlers at the beginning of the program, but for obvious reasons I cant do this. I want to be able to make temporary handlers, similar to the way I have shown, so it does not jump to any random intent, but stays within the scope of the current intent, based on user response.

alexa skills kitresponsenode_js
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Alexander Martin avatar image
Alexander Martin answered ·
var states = {
  'DEFAULT': '',
  'DATA_INTENT_CONFIRMATION': '__DATA_INTENT_CONFIRMATION'
};

var defaultHandlers = Alexa.CreateStateHandler(states.DEFAULT, {
  'GetDataIntent': function(){
    var body = "";
    var url = "someAPI.com";

    https.get(url, (response) =>{
      response.on('data', (chunk) => {
        body+=chunk;
      });
 
     response.on('end', (chunk) => {
        var data = JSON.parse(body);
        /* Do Some Stuff */

        this.handler.state = states.DATA_INTENT_CONFIRMATION;

        this.emit(":ask", "Would you like more info?");
      }); 
    });
  }
});


var dataIntentConfirmationHandlers = Alexa.CreateStateHandler(states.DATA_INTENT_CONFIRMATION, {
  'AMAZON.YesIntent': function YesIntent() {
    // reset to default state
    this.handler.state = states.DEFAULT;
    
    // code 
  },


  'AMAZON.NoIntent': function NoIntent() {
    // reset to default state
    this.handler.state = states.DEFAULT;

    // code
  },


  'Unhandled': function Unhandled() {
    // user said neither yes nor no
    this.emit(':ask', 'Please say yes or no', '...');
  }
});


alexa.registerHandlers(defaultHandlers, dataIntentConfirmationHandlers);

you could use states to get "temporary handlers", see: https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs (chapter Making Skill State Management Simpler)

10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.