How to close a conversation in Alexa?

368 Views Asked by At

I am using ask-sdk version 2.3.0

const SessionEndedRequest = {
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        return request.type === 'SessionEndedRequest'
    }, 
    handle(handlerInput) {
        return handlerInput.responseBuilder.speak("Good bye").withShouldEndSession(true).getResponse();
    }
};


const skillBuilder = Alexa.SkillBuilders.custom();

exports.handler = skillBuilder
  .addRequestHandlers(
    LaunchRequest, 
    SessionEndedRequest
  )
  .lambda();

SessionEndedRequest is the function which gets executed when the user says "Stop" or "Cancel"

The outut when user says "cancel"

{
    "body": {
        "version": "1.0",
        "response": {
            "outputSpeech": {
                "type": "SSML",
                "ssml": "<speak>Good bye</speak>"
            },
            "shouldEndSession": true,
            "type": "_DEFAULT_RESPONSE"
        },
        "sessionAttributes": {},
        "userAgent": "ask-node/2.3.0 Node/v8.10.0"
    }
}

and the alexa response is this

There was a problem with the requested skill's response

1

There are 1 best solutions below

0
On BEST ANSWER

You can not return a response with any speech, card or directives after receiving a SessionEndedRequest, the SessionEndedRequestHandler is a good place to put your cleanup logic. It usually has this form (notice it sends an empty response):

const SessionEndedRequestHandler : RequestHandler = {
      canHandle(handlerInput : HandlerInput) : boolean {
        return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
      },
      handle(handlerInput : HandlerInput) : Response {
        console.log(`Session ended with reason: ${(handlerInput.requestEnvelope.request as SessionEndedRequest).reason}`);

        return handlerInput.responseBuilder.getResponse();
      },
    };

If you want to say something after a "cancel" or a "stop" you need to implement handlers for AMAZON.CancelIntent and/or AMAZON.StopIntent