【问题标题】:How to end session for custom Alexa skill?如何结束自定义 Alexa 技能的会话?
【发布时间】:2018-12-12 18:27:06
【问题描述】:

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

【问题讨论】:

    标签: alexa alexa-skill alexa-app


    【解决方案1】:

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

    ... 
    "shouldEndSession": true
    ...
    

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

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

    列出了响应构建器帮助函数here

    【讨论】:

    • 你不知道我必须做多少谷歌搜索才能找出 .withShouldEndSession(true/false) 存在。谢谢
    【解决方案2】:

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

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

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

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

    上面的代码经常用在相反的场景中,当你想保持会话打开而不重新提示时:

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

    【讨论】:

    • 使用 .withShouldEndSession(false) 标志时会话属性维护多长时间?
    • 直到会话超时。在真实设备中几秒钟,如果我没记错的话是 8 秒。在模拟器中(developer.amazon.com/alexa 上的测试选项卡)很长时间(基本上不会超时,除非您退出页面)
    • 那么 .withShouldEndSession(false) 在真实设备上的实际用途是什么?
    • 这是保持会话打开的唯一方法,允许设备在不提供重新提示的情况下侦听新的话语。基本上,当您不想坚持得到答案(通过重新提示)但允许用户说一次时,您会使用它
    猜你喜欢
    • 2018-11-04
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多