首页 > 解决方案 > 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-promisesuperagent作为 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(一种云服务)上运行,所以我无法想象任何环境都会导致问题。

任何帮助表示赞赏。

标签: javascriptajaxamazon-web-servicesaws-lambdaalexa

解决方案


此代码有效

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 对象会中断交互。


推荐阅读