首页 > 解决方案 > 使用 Alexa Skills 拦截和重定向

问题描述

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

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

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

标签: alexa-skills-kitalexa-skill

解决方案


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

是的,您可以,您需要使用与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();
    }
  },
};

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


推荐阅读