1
votes

I have an alexa skill that plays a sound file on request and then plays a message and stops playing it when the user says stop. I use an end session statement in the stop intent. However, after saying stop once, if you say Alexa stop again it plays the message again, telling me that the skill is still active. How do you give a command to completely exit from the skill?

Here's my current stop intent:

 'AMAZON.StopIntent': function() {

//output to available screen
makeTemplate.call(this, 'stop');

this.response.speak('Ok. I sent a practice tip to your Alexa app.').audioPlayerStop();
this.emit(':responseReady');
this.response.shouldEndSession(true);

},

1

1 Answers

0
votes

You could take advantage of the state machine within Alexa, I would suggest to have different state, besides the default regular one, to have the StopIntent there. In this case you could switch to this state when you are playing the sound and only then your particular Stop behavior will work, there you can return to the regular state which wouldn't have a default behavior on your skill so it will run the default one from the Alexa itself closing your skill.

In this code you can have a basic idea on how this could work, though for sure there is stuff missing, but the important things are the this.handler.state which controls in what state is the session at the moment, and Alexa.CreateStateHandler(state, intents) which gets as parameter the name of a particular state and the particular behavior for the intents on that state.

const Alexa = require('alexa-sdk');

const defaultHandlers = {
    PlayIntent: function() {
        // move to state 'PLAY'
        this.handler.state = 'PLAY'
        // Code to play
    }
}

const playingHanders = Alexa.CreateStateHandler('PLAY', {
    PlayIntent: function() {
        // Code to play 
    },

    'AMAZON.StopIntent': function() {
        //output to available screen
        makeTemplate.call(this, 'stop');
        // move to default state
        this.handler.state = ''
        this.response.speak('Ok. I sent a practice tip to your Alexa app.').audioPlayerStop();
        this.emit(':responseReady');
        this.response.shouldEndSession(true);
    }
})

module.exports.skill = (event, context, callback) => {
    const alexa = Alexa.handler(event, context, callback);
    alexa.appId = APP_ID
    alexa.registerHandlers(defaultHandlers, playingHanders)
    alexa.execute();
}

There are many tutorials on this on internet so you can find better ideas on how to take advantage of this.