Tobias Gassmann
Tobias Gassmann

Reputation: 11829

How do I get Dialog-Data in Alexa's HelpIntent

I have implemented a multi-stage dialog with Alexa. The skill iteratively asks the user for information to calculate the desired result. One after the other the individual slots are filled. So far so good.

Now I want to implement the HelpIntent: According to the current state of the Dialog, I want Alexa to speak different Help-Texts. The problem is that I do not know how to access the data in the slots of my MainIntent.

I need to access the Data in the slots of the MainIntent, because that's how I determine the current position in the dialog on which the Help-Text depends.

So the question in short: How do I acccess slot-data of the MainIntent in the HelpIntent?

Thanks :-)

Upvotes: 2

Views: 87

Answers (2)

terryk2
terryk2

Reputation: 151

Seems like you'll probably get the behavior you're looking for using Skill State Management from the alexa-sdk.

In essence you need to define a series of states your skill can be in and define a intent handler for each one.

You might have a series of stages as your "states as folows:

const states = {
    STAGE_ONE: "_STAGEONE",
    STAGE_TWO: "_STAGETWO",
    STAGE_N: "_STAGE_N"
}

You then define different IntentHandlers for each stage using the Alexa.CreateStateHandler function as below

const StageOneHandler = Alexa.CreateStateHandler(states.STAGE_ONE, {
    'MainIntent': function(){ ... },
    'HelpIntent': function(){ ... },
    ...
}

Using this approach you'll need to define the behaviour for each intent that is valid at each stage within that hander...that is if you have 4 stages you'll likely end up with 5 Amazon.HelpIntent functions (one for each state as well as one when the state hasn't been set). Each helpintent will be able to return the response unique to the stage of the interaction

Finally it's a matter of registering all the state handlers with your alexa skill. From the documentaion we have this example:

exports.handler = function (event, context, callback) {
  const alexa = Alexa.handler(event, context, callback);
  alexa.appId = appId;

  alexa.registerHandlers(StageOneHandler, StageTwoHandler,...);
  alexa.execute();
};

Then in each MainIntent function within the various handlers you need to explicitly set what the next state should be... For example after receiving the STAGE_ONE answer you might set the new state to STAGE_TWO using this.handler.state = states.STAGE_TWO

Upvotes: 2

Amod Gokhale
Amod Gokhale

Reputation: 2448

You can have data slots stored in attributes in mainIntent reference https://developer.amazon.com/blogs/post/Tx213D2XQIYH864/announcing-the-alexa-skills-kit-for-node-js

yourfunction:function(){
     this.attributes['CurrentStage'] = 5;

},

In your HelpIntent read the StageAttribute

AMAZON.HelpIntent': function() {
        const speechOutput = this.t('HELP_REPROMPT');
        const reprompt = this.t('HELP_REPROMPT');
        console.log("Inside HelpIntent:" + this.t('HELP_REPROMPT'));
        console.log("Current Game Stage is " +this.attributes['CurrentStage']);
        var iCurrStage = this.attributes['CurrentStage'];
        switch(expression) {
            case 1:
                  this.emit(':ask', "You are at Stage 1", reprompt);
                  break;
            case 2:
                  this.emit(':ask', "You are at Stage 2", reprompt);
                  break;
            default:
                  this.emit(':ask', "hmmm.. errror", reprompt);
                  break;
         } 
        this.emit(':ask', speechOutput, reprompt);
    },

Upvotes: 2

Related Questions