首页 > 解决方案 > 如何结束自定义 Alexa 技能的会话?

问题描述

我正在为 Alexa 创建自定义技能。我想关闭会话AMAZON.StopIntent。我怎样才能用下面的代码实现这一点?

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

标签: alexaalexa-skillalexa-app

解决方案


当响应 JSON 中的shouldEndSession标志设置为 true时,Alexa 结束会话。

... 
"shouldEndSession": true
...

在您的响应构建器中,您可以尝试使用辅助函数withShouldEndSession(true)

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

此处列出了响应构建器辅助函数


推荐阅读