alexa-app 和请求承诺的网络问题

Networking issue with alexa-app and request-promise

我必须在这里遗漏一些非常简单的东西,但是这里是。

我刚刚开始学习 Alexa 开发,发现了 alexa-app 模块,它似乎让 Alexa 编程变得非常简单 - 除了这个网络问题我 运行 感兴趣。

我正在浏览名为 AirportInfo 的团队提供的示例应用程序。问题区域代码如下:

var faaHelper = new FAADataHelper();
faaHelper.requestAirportStatus(airportCode).then(function(airportStatus) {
    var goodMessage = faaHelper.formatAirportStatus(airportStatus);
    console.log(goodMessage);
    res.say(goodMessage).send();
}).catch(function(err) {
    console.log(err.statusCode);
    var prompt = 'I didn\'t have data for an airport code of ' + airportCode;
    console.log(prompt);
    res.say(prompt).reprompt(reprompt).shouldEndSession(false).send();
});
return false;

依次调用这些函数:

FAADataHelper.prototype.requestAirportStatus = function(airportCode) {
    return this.getAirportStatus(airportCode).then(
        function(response) {
            console.log('success - received airport info for ' + airportCode);
            return response.body;
        }
    );
};
FAADataHelper.prototype.getAirportStatus = function(airportCode) {
    var options = {
        method: 'GET',
        uri: ENDPOINT + airportCode,
        resolveWithFullResponse: true,
        json: true
    };
    return rp(options);
};

这对我来说看起来不错,但是当代码运行时,控制何时将响应发送回设备的主要 alexa-app "request" 返回时间比预期的要早。响应不是包含所选机场的预期天气信息的完整响应负载,而是在 return rp(options) 调用后立即发回。在第一个代码示例的 .then() 块中执行的代码在 之后运行 技能已经向 Alexa 发送了相当于空响应的内容。这实际上使 Alexa 崩溃,因为她说了一些关于技能错误的神秘信息。

这是我的 server.js 代码:

var AlexaAppServer = require("../index.js");
AlexaAppServer.start({
    server_root: './',
    port: 8080,
    debug: true,
    // Use preRequest to load user data on each request and add it to the request json.
    // In reality, this data would come from a db or files, etc.
    preRequest: function(json, req, res) {
        console.log("preRequest fired");
        json.userDetails = { "name": "Bob Smith" };
    },
    // Add a dummy attribute to the response
    postRequest: function(json, req, res) {
        // look for this output in the log below
        console.log("postRequest fired");
        json.dummy = "text";
    }
});

这是显示我正在描述的这种情况的调试日志:

preRequest fired
REQUEST { method: 'GET',
  uri: 'http://services.faa.gov/airport/status/dfw',
  resolveWithFullResponse: true,
  json: true,
  simple: false,
  callback: [Function: RP$callback],
  transform: undefined,
  transform2xxOnly: false }
postRequest fired
REQUEST make request http://services.faa.gov/airport/status/dfw
REQUEST onRequestResponse http://services.faa.gov/airport/status/dfw 200 { date: 'Fri, 24 Mar 2017 05:09:41 GMT',
  server: 'Apache',

  ...

  'access-control-allow-origin': '*',
  'access-control-allow-methods': 'GET, HEAD, OPTIONS',
  'version-requested': 'Any',
  connection: 'close',
  'transfer-encoding': 'chunked',
  'content-type': 'application/json;charset=UTF-8' }
REQUEST reading response's body

...

REQUEST end event http://services.faa.gov/airport/status/dfw
REQUEST has body http://services.faa.gov/airport/status/dfw 517
REQUEST emitting complete http://services.faa.gov/airport/status/dfw
success - received airport info for dfw

注意 preRequest FiredpostRequest Fired 相对于 success - received airport info for dfw 的位置。任何想法我做错了什么?我的 Node 环境配置有误,或者依赖版本有问题?我很怀疑,因为它在我的调试器(VS 代码)中从 Node 命令提示符和 Lambda 中都失败了。

LINK 完整源代码:https://github.com/bignerdranch/alexa-airportinfo

关于 Alexa 的 Promises,我不是很了解,但我确实弄明白了。 从您提出问题之日起,有更新版本的 alexa-app-server (3.0.1) 和 alexa-app (4.0.0) 可用。我最近使用这些版本开发了一项技能。 alexa-app 2,alexa-airportinfo 中提到的版本,不适用于最新的 alexa-app-server。

因此,首先要做的是在您的代码中使用最新的 alexa-app。然后,意识到通过在 FAADataHelper 中使用请求承诺,代码是 returning Promise 回到 index.js。为此,您需要 return 该 Promise,而不是函数末尾的 false。

综上所述,我获得了最新的 alexa-app-server,将 airportinfo 中的 alexa-app 版本更改为最新版本,然后 运行 它得到了与您相同的结果 - 响应来了在 rp 请求完成之前返回。当我将代码更改为以下代码时,我得到了想要的结果——请求完成,然后响应以语音结束。

app.intent('airportinfo', {
  'slots': {
    'AIRPORTCODE': 'FAACODES'
  },
  'utterances': ['{|flight|airport} {|delay|status} {|info} {|for} {-|AIRPORTCODE}']
},
  function(req, res) {
    //get the slot
    var airportCode = req.slot('AIRPORTCODE');
    var reprompt = 'Tell me an airport code to get delay information.';
    if (_.isEmpty(airportCode)) {
      var prompt = 'I didn\'t hear an airport code. Tell me an airport code.';
      res.say(prompt).reprompt(reprompt).shouldEndSession(false);
      return true;
    } else {
      var faaHelper = new FAADataHelper();
      return faaHelper.requestAirportStatus(airportCode).then(function(airportStatus) {
        console.log(airportStatus);
        res.say(faaHelper.formatAirportStatus(airportStatus)).send();
      }).catch(function(err) {
        console.log(err.statusCode);
        var prompt = 'I didn\'t have data for an airport code of ' + airportCode;
         //https://github.com/matt-kruse/alexa-app/blob/master/index.js#L171
        res.say(prompt).reprompt(reprompt).shouldEndSession(false).send();
      });
      // return false;
    }
  }
);