【问题标题】:Interceptor and redirect with Alexa Skills使用 Alexa Skills 拦截和重定向
【发布时间】:2021-10-07 20:50:38
【问题描述】:
  1. 一个拦截器只能分配给一个意图吗?

  2. 我可以将拦截器重定向到另一个意图吗?

我创建了一个 respose 拦截器,用于检查用户是否赢得游戏并将拦截器重定向到另一个意图。显示拦截器之前的 APL 意图模板,但 alexa 说话的音频来自我重定向的意图。并且不显示重定向意图的APL模板。

【问题讨论】:

    标签: alexa-skills-kit alexa-skill


    【解决方案1】:

    一个拦截器只能分配给一个意图吗?

    可以,您需要在拦截器中使用与函数canHandle 相同的逻辑:

    canHandle(handlerInput: Alexa.HandlerInput) {
        // reuse this code
        return (
          Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
          Alexa.getIntentName(handlerInput.requestEnvelope) === "HelloWorldIntent"
        );
      },
    

    我可以将拦截器重定向到另一个意图吗?

    不完全是,但你可以用不同的方式来做。拦截器不回复请求。它只是intercept the request,所以你的 Intent 可以处理它。 intent 正在回复请求。

    因此您可以将会话逻辑放在拦截器中,意图将知道用户是否赢得了游戏。

    拦截器

    const MyAwesomeInterceptor = {
      process(handlerInput) {
        const { attributesManager, requestEnvelope } = handlerInput;
        const sessionAttributes = attributesManager.getSessionAttributes();
    
        // Code logic ...
    
        sessionAttributes.hasWon = true
      }
    }
    

    意图

    const HelloWorldIntentHandler = {
      canHandle(handlerInput) {
        return (
          Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
          Alexa.getIntentName(handlerInput.requestEnvelope) === "HelloWorldIntent"
        );
      },
    
      handle(handlerInput) {
        const { attributesManager } = handlerInput;
        const sessionAttributes = attributesManager.getSessionAttributes();
    
        if (sessionAttributes.hasWon) {
          return handlerInput.responseBuilder.speak("You won").getResponse();
        } else {
          return handlerInput.responseBuilder.speak("You lost").getResponse();
        }
      },
    };
    

    我建议您查看doc 以更好地了解 Alexa 的工作原理。

    【讨论】:

    • 谢谢你....应该是类似于 angular 的东西,你可以为某些请求分配拦截器。我会看文档.....
    猜你喜欢
    • 2017-11-28
    • 2017-04-20
    • 2018-02-03
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    相关资源
    最近更新 更多