如何在Alexa中结束对话?

时间:2019-02-16 14:15:34

标签: alexa alexa-skills-kit alexa-sdk-nodejs

我正在使用Ask-SDK版本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是当用户说“停止”或“取消”时执行的功能

用户说“取消”时的提示

{
    "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"
    }
}

而alexa的回应是这样

  

所请求的技能响应出现问题

1 个答案:

答案 0 :(得分:1)

在收到SessionEndedRequest之后,您无法返回任何语音,卡片或指令的响应,SessionEndedRequestHandler是放置清除逻辑的好地方。它通常具有以下形式(请注意,它发送的响应为空):

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();
      },
    };

如果要在“取消”或“停止”之后说些什么,则需要实现AMAZON.CancelIntent和/或AMAZON.StopIntent的处理程序