【问题标题】:Alexa Skill doesn't respond after Ajax requestAlexa Skill 在 Ajax 请求后没有响应
【发布时间】:2018-10-13 04:36:52
【问题描述】:

这段代码完美运行

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 语句的示例,但我在网上某处阅读了一条回复,该回复建议在app.intent() 中为异步返回一个promise,但此更新没有效果:

return ajax('http://www.google.com')

我还认为它可能很慢并且超时,但我的 Alexa 技能超时设置为 5 分钟。我有其他技能可以毫无问题地执行 Ajax,并且代码都在 Lambda(一种云服务)上运行,所以我无法想象任何环境会导致问题。

任何帮助表示赞赏。

【问题讨论】:

标签: javascript ajax amazon-web-services aws-lambda alexa


【解决方案1】:

此代码有效

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 falseapp.intent() 意味着什么的任何解释。返回 undefined 或 Promise 对象会中断交互。

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 1970-01-01
    • 2015-11-16
    • 2019-07-15
    • 2020-10-15
    • 1970-01-01
    • 2020-05-04
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多