alexa / alexa-skills-kit-sdk-for-nodejs

The Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
Apache License 2.0
3.12k stars 736 forks source link

Session is alive but any utterance will trigger my skill. #269

Closed glardd closed 6 years ago

glardd commented 6 years ago

Hello, I followed the facts skill tutorial by amazon https://developer.amazon.com/alexa-skills-kit/tutorials/fact-skill-1

once I finished it i was able to get my skill running but wanted to customize it even more. I wanted to keep the skill running so i can use the other utterances to get more facts instead of constantly using the invocation name. I was able to use :ask and reprompt to keep the skill session open but any thing i say will bring up another fact. i was wondering if it was possible for it to only respond to the utterances i gave it in the skill builder. Here is the example code they gave me. I didn't include the const variables because i figure i don't need to change that.

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

const handlers = { 'LaunchRequest': function () {

   this.emit('GetNewFactIntent');

}, 'GetNewFactIntent': function () {

    const factArr = data;
    const factIndex = Math.floor(Math.random() * factArr.length);
    const randomFact = factArr[factIndex];
    const speechOutput = GET_FACT_MESSAGE + randomFact;
    var repromptSpeech = 'I can tell you more facts';

    this.response.cardRenderer(SKILL_NAME, randomFact);
    this.response.speak(speechOutput);
    this.response.listen(repromptSpeech);
    this.emit(':responseReady');

},

'AMAZON.HelpIntent': function () {
    const speechOutput = HELP_MESSAGE;
    const reprompt = HELP_REPROMPT;

    this.response.speak(speechOutput).listen(reprompt);
    this.emit(':responseReady');
},
'AMAZON.CancelIntent': function () {
    this.response.speak(STOP_MESSAGE);
    this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
    this.response.speak(STOP_MESSAGE);
    this.emit(':responseReady');
},

};`

TianTXie commented 6 years ago

@glardd , I think one way is to define another 'YesIntent' and use the reprompt in last response to trigger it. For example,

...
'GetNewFactIntent': function () {
...
this.response.cardRenderer(SKILL_NAME, randomFact);
    this.response.speak(speechOutput);
    this.response.listen('Do yo want to know more fact?');
    this.emit(':responseReady');
}
'YesIntent': function(){
...
this.response.cardRenderer(SKILL_NAME, randomFact);
    this.response.speak(speechOutput);
    this.response.listen('Do yo want to know more fact?');
    this.emit(':responseReady');
}
...
}

Please refer this link. It is a very similar example that shows this case.

mohit7331 commented 6 years ago

After the skill completes a task, the session remains open with no prompt to the user. The skill must close the session after fulfilling requests if it does not prompt the user for any input.

How to handle this? Using this this.response.listen(repromptSpeech); this.emit(':responseReady');

Help me

jitendra-vadlamani commented 6 years ago

You can use

this.response.shouldEndSession(true);

This would end the session.

mohit7331 commented 6 years ago

can you please give a example how to use this as im new to use javascript thank you

On Sat, Mar 31, 2018 at 5:00 AM, jitendraavinash notifications@github.com wrote:

You can use

this.response.shouldEndSession(true);

This would end the session.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/269#issuecomment-377645630, or mute the thread https://github.com/notifications/unsubscribe-auth/Abjq79qmne--HwOfAV22Y7b59Tf87GZkks5tjsAmgaJpZM4RcQZR .

-- Regards, Mohit Kumar Vice President Computer Science Society Department Of Computer Science University Of Delhi

TianTXie commented 6 years ago

@mohit7331, you can not send the reprompt message without output speech, if you want to send a reprompt object in your response, you should do:

this.response.speak('speech output').listen('reprompt');
this.emit(':responseReady');

If Alexa doesn't receive the response from user for a certain time, it will close the session automatically. For more information, check here.