I'm working on my first Alexa skill and, as a starting point, would like Alexa to state data retrieved from a simple GET request (see lambda function below). For some reason, however, the request does not actually seem to be executing - nothing from inside request.get() is printing to the console and speechOutput is 'Outside Request' after the handler executes. I'm also new to looking through CloudWatch logs and have not been able to find any information about the network requests to even know if this is being attempted. Any help here would be welcome!
'use strict';
//Required node packages
const alexa = require('./node_modules/alexa-sdk');
const request = require('request');
// var https = require('https')
//this is the handler, when the lambda is invoked, this is whats called
exports.handler = function (event, context, callback) {
const skill = alexa.handler(event, context);
skill.appId = '<app_id>';
skill.registerHandlers(handlers);
skill.execute();
};
//Alexa handlers
const handlers = {
'LaunchRequest': function () {
console.log("inside of LaunchRequest");
const speechOutput = "Hello from NASA!";
this.response.speak(speechOutput).listen(speechOutput);
this.emit(':responseReady');
},
//Entering our main, part finding function
'GetAPOD': function () {
const intent_context= this
const speechOutput = getData()
intent_context.response.speak(speechOutput).listen(speechOutput);
intent_context.emit(':responseReady');
},
'Unhandled': function (){
console.log("inside of unhandled");
const speechOutput = "I didn't understand that. Please try again";
this.response.speak(speechOutput).listen(speechOutput);
this.emit(':responseReady');
}
};
const getData = function() {
const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
console.log("inside get data")
request.get(url, function (error, response, body) {
console.log("inside request")
console.log('error', error) //Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
return "complete request"
return body
});
return "outside request"
}
I have found in the past that such API requests will get clobbered because they are not synchronous, like David stated. To resolve this, I have had to tuck the request in a promise to get it to resolve, something similar to this in your case:
Change your function to contain the promise:
function getData = function() {
const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
console.log("inside get data")
return new Promise(function(resolve, reject) {
request.get(url, function (error, response, body) {
if (err) {
reject(err);
}
if (body) {
resolve(JSON.parse(body));
}
});
});
}
Then change your intent handler to use the promise:
//Entering our main, part finding function
'GetAPOD': function () {
getData()
.then(function(body) {
let speechOutput = body;
intent_context.response.speak(speechOutput).listen(speechOutput);
intent_context.emit(':responseReady');
}
Something along these lines. You would need to play with it a bit to make sure the results are produced as you intend. Hope this helps.
D
Related
I am learning async map . I want to download a bunch of URLs but i want to send a header along with the get request.
If i didn't have a header, I could have just done
var request = require('request');
var async = require('async');
var urls = ['http://myurl1.com', 'http://myurl2.com', 'http://myurl3.com'];
async.map(urls, request, function(err, results) {
if (err) throw(err); // handle error
console.log(results.length); // == urls.length
});
Now I do need to send a header {"x-url-key":"myurlkey"} along with every get request.
How do I modify the code above to do so?
That should be straightforward enough to do, we can create a wrapper function requestWithHeader to pass to async.map, this will specify whichever headers (or other options) you wish.
I'm also specifying json: true here, you may not want to do that in your actual code.
In this example I'm using https://httpbin.org/get as the url, this will send back all the request parameters which is useful for test purposes as we can see which headers we populated.
var request = require('request');
var async = require('async');
var urls = ["https://httpbin.org/get?foo=bar", "https://httpbin.org/get?foo=baz"];
function requestWithHeader(uri, callback) {
request(uri, { headers: {"x-url-key":"myurlkey"}, json:true }, callback)
}
async.map(urls, requestWithHeader, function(err, results) {
if (err) throw(err); // handle error
console.log("Results:", results.map(result => result.body));
});
To wait for async.map to finish you can create an asynchronous function to call it, e.g.
async function testMapWithPromise() {
try {
let results = await async.map(urls, requestWithHeader);
console.log("testMapWithPromise: Results:", results.map(result => result.body));
// Do whatever with results here...
} catch (error) {
console.error("testMapWithPromise: An error occurred:", error);
}
}
testMapWithPromise();
Trying to send several messages (from AWS SQS lambda, if that matters) but it's never waiting for the promises.
function getEndpoint(settings){
return new Promise(function(resolve, reject) {
// [...] more stuff here
}
Which is then called in a loop:
exports.handler = async (event) => {
var messages = [];
event.Records.forEach(function(messageId, body) {
//options object created from some stuff
messages.push(getEndpoint(options).then(function(response){
console.log("anything at all"); //NEVER LOGGED
}));
});
await Promise.all(messages);
};
But the await seems to be flat out skipped. I'm not sure how I'm getting Process exited before completing request with an explicit await. I have similar async await/promise setups in other scripts that work, but cannot spot what I've done wrong with this one.
You forgot to return something to lambda:
exports.handler = async (event) => {
var messages = [];
event.Records.forEach(function(messageId, body) {
//options object created from some stuff
messages.push(getEndpoint(options));
});
await Promise.all(messages);
return 'OK'
};
this should also work:
exports.handler = (event) => { // async is not mandatory here
var messages = [];
event.Records.forEach(function(messageId, body) {
//options object created from some stuff
messages.push(getEndpoint(options));
});
return Promise.all(messages); // returning a promise
};
and you could use map:
exports.handler = (event) => { // async is not mandatory here
const messages = event.Records.map(function(messageId, body) {
//options object created from some stuff
return getEndpoint(options)
});
return Promise.all(messages); // returning a promise
};
To understand why this happens, you must dive a bit into lambda's implementation: it will essentially wait for the function stack to be cleared and since you did NOT return anything at all in there, the function stack got empty right after it queued all the stuff - adding a simple return after the await call makes the fn stack to NOT be empty which means lambda will wait for it to be finished.
If you run this on standard node, your function would also return before the promises were finished BUT your node process would NOT exit until the stack was cleared. This is where lambda diverges from stock node.
I want to create an AWS Lambda function to check whether the site is available. I tried this function.
'use strict';
var url = require('url');
var target = 'https://www.google.com'; // Change this one
exports.handler = function(event, context, callback) {
var urlObject = url.parse(target);
var mod = require(
urlObject.protocol.substring(0, urlObject.protocol.length - 1)
);
console.log('[INFO] - Checking ' + target);
var req = mod.request(urlObject, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('[INFO] - Read body chunk');
});
res.on('end', function() {
console.log('[INFO] - Response end');
callback();
});
});
req.on('error', function(e) {
console.log('[ERROR] - ' + e.message);
callback(e);
});
req.end();
};
I tried this in "How it works" section before create the actual function. But when I run this, I get an error "Process exited before completing request"
My objective is to send an alert if the site is down (using AWS cloud-watch).
Your code terminates because you're invoking req.end before any the events are invoked. Under the hood, these APIs use the EventEmitter API in NodeJS so it publishes events to the channels that are listening to them, but since all of this happens asynchronously, req.end is being invoked before any of these events are fired.
You can greatly simplify your code by using the request module. Just pack it with your dependencies.
I have refactored your code a little bit to use async/await as well, so I needed to promifisy the callback. It's a good practice to do so. If you want to send a notification when something goes wrong, just put the code inside the catch block.
'use strict';
const target = 'https://www.google.com'; // Change this one
const request = require('request')
const handler = async (event) => {
try {
const data = await new Promise((res, rej) => {
request.get(target, (err, data) => {
if (err) {
return rej(err)
}
return res(data)
})
})
console.log(data)
} catch (e) {
console.log(e)
//send notification
}
};
This is a nodeJS code nugget from my application, written for publishing in AWS Lambda. The callProcess function basically returns some processed information about the city I am passing - hard coded here for "New York"
function speech2(intent, session, callback) {
let country;
const repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
let speechOutput = 'old text';
callProcess('New York', function (error, data) {
if (!error) {
speechOutput = data;
console.log(speechOutput);
}
else {
console.log(error.message);
}
});
// Setting repromptText to null signifies that we do not want to reprompt the user.
// If the user does not respond or says something that is not understood, the session
// will end.
callback(sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText,
shouldEndSession));
}
The console.log(speechOutput) correctly displays the processed information about the city - i.e. callProcess has worked. However the callback at the end of this function that has speechOutput is still referring to 'old text' i.e. I am unable to over-write the variable using the processed information that sits within the function? How do I do this within callbacks?
Any help here is greatly appreciated. Thanks in advance.
your callProcess function is an async function which correctly shows the speechOutput data. The callback you've wriiten is outside this callProcess function which is called before callProcess is executed.
you can get the correct value of speechOutput by calling the callback inside your callProcess function.
like this-`
callProcess('New York', function (error, data) {
if (!error) {
speechOutput = data;
console.log(speechOutput);
callback(sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText,
shouldEndSession));
}
else {
console.log(error.message);
}
});
for further info how async method behave have a look at this async and sync functions
I'm very new to async languages like nodejs, I'm trying to write a web scraper that will visit a link, download a code, extract with regex, THEN visit another link using that code. I'm aware I could use callbacks, but I expect to have to go 8-9 levels deep, I think promises is the way to go (is there a better way?)
var promise = require("promise");
var request = require("request");
login();
function get_login_code()
{
request.get("someurl.com", function (error, response, body)
{
// just for example
body = 'TOKEN" value="hello world"';
var login_code = body.match(/.TOKEN" value="([^"]+)../i);
return login_code
});
}
function login()
{
var login_promise = promise.resolve(get_login_code());
console.log(login_promise);
}
I've tried a bunch of combinations of messing around with promises, but I either always get undefined or a promise which doesn't have a value. I don't want to nest promise functions inside promises because that's exactly the same thing as callback hell. Can someone tell me what I'm doing wrong, I really want this code to be procedural and not 8 callbacks. In the ideal world promise.resolve just waits until get_login_code() returns the actual code, not undefined.
Output:
Promise { _45: 0, _81: 1, _65: undefined, _54: null }
Desired Output:
hello world
What your code do:
calls get_login_code that returns nothing (i.e. undefined)
inside of login function you create a new promise that is immediately resolved to the result of get_login_code, i.e. undefined.
Thus, you do not use login_code at all.
To make it work, you should make get_login_code to return a promise that will be resolved to login_code. Consider you use promise npm module, the code may look like:
// uppercased, it's a constructor
var Promise = require("promise");
var request = require("request");
login();
function get_login_code()
{
return new Promise(function (resolve, reject) {
request.get("someurl.com", function (error, response, body) {
if (err) {
reject(err);
return;
}
// just for example
body = 'TOKEN" value="hello world"';
var login_code = body.match(/.TOKEN" value="([^"]+)../i);
resolve(login_code);
});
});
}
function login()
{
// return a new promise to use in subsequent operations
return get_login_code()
.then(function(login_code) {
console.log(login_code);
});
}
You should create new promise in the function to handle reject and resolve not by handling resolve to the function itself. Use then to get the response value from promise. I guess this should work.
var promise = require("promise");
var request = require("request");
function get_login_code()
{
var promise = new Promise(function(resolve, reject) {
request.get("someurl.com", function (error, response, body)
{
if (error) {
reject(error);
} else {
// just for example
body = 'TOKEN" value="hello world"';
var login_code = body.match(/.TOKEN" value="([^"]+)../i);
resolve(login_code);
}
});
});
}
get_login_code()
.then(function (code) {
console.log(code);
});