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

Dee*_*tia 5 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();
  },
};
Run Code Online (Sandbox Code Playgroud)

Cic*_*mas 19

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

... 
"shouldEndSession": true
...
Run Code Online (Sandbox Code Playgroud)

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

 return handlerInput.responseBuilder
      .speak('bye!')
      .withShouldEndSession(true)
      .getResponse();
Run Code Online (Sandbox Code Playgroud)

此处列出响应构建器帮助程序功能


Ger*_*man 7

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

return handlerInput.responseBuilder
  .speak('bye!')
  .getResponse();
Run Code Online (Sandbox Code Playgroud)

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

return handlerInput.responseBuilder
      .speak('bye!')
      .withShouldEndSession(true)
      .getResponse();
Run Code Online (Sandbox Code Playgroud)

当您想要保持会话打开而不重新提示时,上面的代码通常用于相反的情况:

return handlerInput.responseBuilder
      .speak('bye!')
      .withShouldEndSession(false)
      .getResponse();
Run Code Online (Sandbox Code Playgroud)

  • 那么实际上 .withShouldEndSession(false) 在真实设备上有什么用呢? (2认同)
  • 这是保持会话打开的唯一方法,允许设备监听新的话语而不提供重新提示。基本上,当您不想坚持获得答案(通过重新提示)但允许用户说一次时,您可以使用它 (2认同)