Running an Alexa skill in a specific state

I used to run into an issue where Alexa didn't change the state back to empty, but found that there is a bug in that . To avoid this problem, I decided that I want my skill to always start with START_MODE

.

I used this one as my reference where they set the skill state by executing alexa.state = constants.states.START

before alexa.execute()

on line 55 However, when I do the same in my code, it doesn't work.

Below is what my skill looks like:

exports.newSessionHandler = {

    LaunchRequest () {
        this.hander.state = states.START;
        // Do something
    }

};

exports.stateHandler = Alexa.CreateStateHandler(states.START, {

    LaunchRequest () {
        this.emit("LaunchRequest");
    },

    IntentA () {
        // Do something
    },

    Unhandled () {
        // Do something
    }

});

      

I use Bespoken-tools

to test this skill with Mocha

, and when I directly pass IntentA

like this:

alexa.intended("IntentA", {}, function (err, p)  { /*...*/ })

      

The test is complaining Error: No 'Unhandled' function defined for event: Unhandled

. From what I'm collecting, this can only mean that the skill is in an empty state at startup (because I haven't defined any Unhandled

for that state), which should mean it alexa.state

isn't actually a thing. But then it makes me wonder how they worked in the above code example.

My guess is that a workaround for this would be to create an alias for each intent that I expect to have in START_MODE

by doing:

IntentA () {
    this.handler.state = states.START;
    this.emitWithState("IntentA");
}

      

But I want to know if there is a way to get my skill to start in a certain state, because it looks much better and better in my eyes.

+3


source to share


1 answer


The problem is that when you receive the LaunchRequest, there is no state as you discovered. If you look at the official Alexa examples, you can see that they solve this by doing what you said, making an "alias" intent for all of their intents and just using them to change state, and then calling themselves using "emitWithState".

This is probably the best way to handle this, as it gives you the most control over what's called state and intent.

Another option, if you want every new session to start from the same state, is to use the NewSession event. this event is fired before the start request and all new sessions go through it. your code will look something like this:

NewSession () {
    if(this.event.request.type === Events.LAUNCH_REQUEST) {
        this.emit('LaunchRequest');
    } else if (this.event.request.type === "IntentRequest") {
        this.handler.state = states.START;
        this.emitWithState(this.event.request.intent.name);
    }
};

      



A complete example of this can be seen here (look at the Handlers.js file): https://github.com/alexa/skill-sample-node-device-address-api/tree/master/src

I would also recommend reading this section on the Alexa GitHub: https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs#making-skill-state-management-simpler

EDIT: I took a look at the link you provided and it looks like they are setting state outside of the alexa handler. So, assuming you wanted to mimic what they do, you would not set the state in your Intent handler, but the Lambda handler itself (where you create the alexa object).

exports.handler = function (event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.appId = appId;
    alexa.registerHandlers(
        handlers,
        stateHandlers,
    );
    alexa.state = START_MODE;
    alexa.execute();
};

      

+1


source







All Articles