Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to end session for custom Alexa skill?

I am creating a custom skill for Alexa. I want to close the session on AMAZON.StopIntent. How can I achieve this with below code?

const ExitHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && (request.intent.name === 'AMAZON.StopIntent');
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak('bye!')
      .reprompt('bye!')
      .getResponse();
  },
};
like image 226
Deepak Mankotia Avatar asked Jul 04 '18 10:07

Deepak Mankotia


People also ask

How do you end an Alexa skill?

Ask Alexa to quit the skill.Say "Alexa, cancel," or "Alexa, quit," to exit the current Alexa skill.

How do I publish my Alexa custom skill?

To publish a skill that is in a Certified status, navigate to Certification > Submission. When publishing your skill, you have the following options: Publish now. Start the process now of publishing the skill to the Alexa Skills Store.

How do I remove custom Alexa skills?

To disable your skill, go to the Alexa app and select Skills then choose Your Skills. Select the skill and choose Disable skill. If you wish to delete your skill, go to the Skills You've Made page, select the skill and choose Delete.

What is session in Alexa?

A skill session begins when a user invokes your skill and Alexa sends your skill a request. The request contains a session object that uses a Boolean value called new to indicate that this is a new session. Your skill receives the request and returns a response for Alexa to speak to the user.


2 Answers

Alexa ends the session when shouldEndSession flag is set to true in the response JSON.

... 
"shouldEndSession": true
...

In your response builder can you try with the helper function withShouldEndSession(true)

 return handlerInput.responseBuilder
      .speak('bye!')
      .withShouldEndSession(true)
      .getResponse();

Response builder helper functions are listed here

like image 79
johndoe Avatar answered Sep 22 '22 14:09

johndoe


In your code snippet you would end the session just by removing the reprompt line:

return handlerInput.responseBuilder
  .speak('bye!')
  .getResponse();

so the suggested solution below works but it's redundant:

return handlerInput.responseBuilder
      .speak('bye!')
      .withShouldEndSession(true)
      .getResponse();

The code above is often used in the opposite scenario, when you want to keep the session open without a reprompt:

return handlerInput.responseBuilder
      .speak('bye!')
      .withShouldEndSession(false)
      .getResponse();
like image 37
German Avatar answered Sep 20 '22 14:09

German