javascript - Alexa Skill 在 Ajax 请求后没有响应
问题描述
此代码完美运行
var app = new Alexa.app('appName');
// ...
app.intent('marcopolo', {
'slots': {},
'utterances': ['marco']
}, function(request, response){
console.log('marco worked');
response.say('polo').shouldEndSession(false).send();
});
// Alexa says: polo
// Log says: marco worked
此代码不起作用
var app = new Alexa.app('appName');
// ...
app.intent('marcopolo', {
'slots': {},
'utterances': ['marco']
}, function(request, response){
console.log('marco started');
return ajax('http://www.google.com')
.then(function(){
console.log('marco response');
response.say('polo').shouldEndSession(false).send();
})
.catch(function(){
console.log('marco error');
response.say('polo, I think').shouldEndSession(false).send();
});
});
// alexa says: (no response)
// Log says: marco started
我尝试使用request-promise和superagent作为 Ajax 库,结果相同。
以下是版本:
"alexa-app": "^2.4.0",
"request-promise": "^2.0.0",
"superagent": "^3.8.3"
这是我的 Alexa 技能意图:
"intents": [
{
"name": "marcopolo",
"slots": [],
"samples": [ "marco" ]
}
]
我从未见过使用声明的示例app.intent()
,return
但我在网上某处阅读了一个回复,该回复表明 async inside 需要返回一个承诺app.intent()
,但此更新没有效果:
return ajax('http://www.google.com')
我还认为它可能很慢并且超时,但我的 Alexa Skill Timeout 设置为 5 分钟。我有其他技能可以毫无问题地执行 Ajax,并且代码都在 Lambda(一种云服务)上运行,所以我无法想象任何环境都会导致问题。
任何帮助表示赞赏。
解决方案
此代码有效
var ajax = require('request-promise');
//...
app.intent('marcopolo', {
'slots': {},
'utterances': ['marco']
}, function(req, res){
ajax('http://google.com').then(function() {
console.log('success');
res.say('polo').send();
}).catch(function(err) {
console.log(err.statusCode);
res.say('not working').send();
});
return false;
});
事实证明,return
声明是必需的,而且必须是false
. 我找不到任何记录在案的地方,也找不到任何return false
关于app.intent()
. 返回undefined
或 Promise 对象会中断交互。
推荐阅读
- typescript - cypress.should 包含带有变量和子字符串的正则表达式
- flutter - 如何在颤动中制作分段色线?
- c++ - 从线性列表中删除元素
- mercurial - 找出给定两个提交哈希的提交数
- python - 左加入并仅获取熊猫中第一个字符串匹配的特定列
- random - 我们可以在带有种子参数的系统verilog中使用$random吗?
- c++ - 为什么程序只能在调试模式下工作?
- python - 在 Python 中使用 for 循环创建变量
- tensorflow - 将 tf.data.Dataset 拆分为两个不同的 Input 和 Target tf.data.Dataset
- html - SVG sprite 文件可以从 Nginx 正常加载,但不能用于 Tomcat,从另一个端口提供服务(模拟 CDN)