alexa-skills-kit - 使用 Alexa Skills 拦截和重定向
问题描述
拦截器只能分配给一个意图吗?
我可以将拦截器重定向到另一个意图吗?
我创建了一个 respose 拦截器来检查用户是否赢得了游戏并将拦截器重定向到另一个意图。显示拦截器之前的 APL 意图模板,但 alexa 说话的音频来自我重定向的意图。并且不显示重定向意图的 APL 模板。
解决方案
拦截器只能分配给一个意图吗?
是的,您可以,您需要使用与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 的工作原理。
推荐阅读
- amazon-web-services - S3 存储桶中文件夹的总大小
- csv - 谷歌工作表脚本编辑器导入多张工作表
- vue.js - 如何将路径变量传递给 axios GET 请求
- angular - 离子模板解析错误、离子选择、npm 和离子版本
- sql-server - 如何在 SQL Server 空间数据类型列中加载 dwg 文件?
- python - 更新覆盖率报告而不覆盖它
- javascript - 使用 react-spring Parallax,如何在用户滚动时增加图像宽度?
- react-native-android - 错误语法错误:在 JSON 中使用 paramsquery 位置 0 的意外标记 <
- centos - 如何禁止 Clamav 从 Internet 下载病毒库
- open-liberty - OpenLiberty 中托管的 Web 应用程序无法通过系统类加载器找到类