Search code examples
node.jsalexa-skills-kit

How to call an intent using response.speak() in node.js?


this.response.speak('Okay, I will ask you some questions about ' +language + 
'. Here is your first question.' + this.AskQuestion);

If I give in Python as the language, on the Alexa simulator I get the output as

"Okay, I will ask you some questions about python. Here is your first question.undefined"

AskQuestion looks like this:

'AskQuestion': function() {
 var language = this.attributes['language'];

var currentQuestion=flashcardsDictionary
[this.attributes['currentFlashcardIndex']].question;

return 'In ' + language +', ' + currentQuestion;
}

Why would the AskQuestion return an undefined value?


Solution

  • I am gonna assume that AskQuestion is another handler. In the sdk example at the end of the item, it gives an example how to call another handler in the same state.

    const handlers = {
        'LaunchRequest': function () {
            this.emit('HelloWorldIntent');
        },
    
        'HelloWorldIntent': function () {
            this.emit(':tell', 'Hello World!');
        }
    };
    

    so you cant just this.AskQuestion cause its not there, alexa sdk hides the functions in another hidden set of keys inside the object. What you could do is forward the intent to AskQuestion and handle everthing there

    this.response.speak('AskQuestion');
    

    and then in AskQuestion

    'AskQuestion': function() {
        var language = this.attributes['language'];
    
        var currentQuestion=flashcardsDictionary
        [this.attributes['currentFlashcardIndex']].question;
    
        this.response.speak("Yes everything is alright");
        this.emit(':responseReady');
    }