如何结束自定义Alexa技能的会话?

时间:2018-07-04 10:30:38

标签: alexa alexa-skill alexa-app

我正在为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();
  },
};

2 个答案:

答案 0 :(得分:15)

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

... 
"shouldEndSession": true
...

您可以在响应构建器中尝试使用辅助功能 withShouldEndSession(true)

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

响应构建器助手功能在here中列出

答案 1 :(得分:3)

在代码段中,您只需删除提示行即可结束会话:

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

因此,以下建议的解决方案有效,但它是多余的:

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

上面的代码通常在相反的情况下使用,当您想保持会话打开而无需再次提示时:

return handlerInput.responseBuilder
      .speak('bye!')
      .withShouldEndSession(false)
      .getResponse();
相关问题