Search code examples
alexa-skills-kitalexa-slot

Alexa skill 2 intents and open slot


Is it possible that Alexa reacts right when I have in a sample utterances something like this, opened slots?

MyNameIsIntent {firstname}
ProjektIntent {projekt}

Right now she always goes back to MyNameIsIntent even if she ask a question for a ProjektIntent

The bad conversation:

Alexa: Welcome, what is your name?
Me: Frank
Alexa Hi Frank, Tell me what project is interesting for you?
Me: Teleshoping
Alexa: Hi Teleshoping, Tell me what project is interesting for you?

Im totally new to Alexa, could you give me please a tip if it is possible that Alexa answers the right question? I tried to do it with a session atributes but it doesn't work


Solution

  • You can use States do react differently on the same intents depending on the state of your application.

    See https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs#making-skill-state-management-simpler for the official documentation.

    You create a dictionary of StateHandlers like this (in Node.js 4.3):

    var askNameStateHandlers = Alexa.CreateStateHandler("askNameState", 
        {
             "MyIntent": function () { /* ... */ }
            // more handlers ...
        });
    

    Then you register your handlers like this, assuming you have a dictionary of handlers without states defaultHandlers and two with handlers for a specific state askNameStateHandlers and askProjectStateHandlers:

    exports.handler = function(event, context, callback) {
        var alexa = Alexa.handler(event, context);
        alexa.registerHandlers(defaultHandlers, askNameStateHandlers, askProjectStateHandlers);
        alexa.execute();
    };
    

    To change the state of your application, simply assign it like this inside a handler function:

    this.handler.state = "askNameState";
    

    The alexa-sdk will then take care of calling the right handler depending on your application state.

    There is also a full example implementation at https://github.com/alexa/skill-sample-nodejs-highlowgame


    Note that this way, you will only have one intent MyIntent who accepts the answers to both questions, deciding which of your functions should handle the result based on the application state only.