I have to be missing something really simple here, but here goes.
I am just now starting to learn Alexa development, and found the alexa-app module which seems to make Alexa programming really straight-forward - except for this networking issue I'm running into.
I'm walking through the sample app provided by the team called AirportInfo. The code for the problem area is below:
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;
which in turns calls these functions:
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);
};
This looks good to me, but when the code runs, the main alexa-app "request" that controls when responses are sent back to the device is returning earlier than expected. Instead of a full response payload with the expected weather information for the selected airport, the response is sent back immediately after the return rp(options)
call is made. The code that executes in the .then()
block in the first code sample runs after the skill has already sent what amounts to an empty response back to Alexa. This actually crashes Alexa as she says some kind of cryptic message about an error with the skill.
Here's my server.js code:
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";
}
});
and here's the debug log showing this condition I'm describing:
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
Notice where preRequest Fired
and postRequest Fired
show up relative to success - received airport info for dfw
. Any ideas what I'm doing wrong? Something mis-configured in my Node environment, or perhaps a bad dependency version? I'm suspicious because it fails in my debugger (VS Code) from the Node command prompt, and from Lambda just the same.
LINK TO FULL SOURCE CODE: https://github.com/bignerdranch/alexa-airportinfo