I am working with on a Lambda function that an Alexa skill can use. All i want is something simple that can read the event and send the info back to the user. For this purpose I'm using the npm library ical.js https://www.npmjs.com/package/ical with the function ical.fromURL(url, options, function(err, data) {} ) but the problem is that the function never executes. I have the following code:
var Alexa = require("alexa-sdk");
var ical = require("ical");
var test = "This is a simple test 1";
exports.handler = function(event, context) {
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
'LaunchRequest':function() {
console.log(test);
ical.fromURL('http://lanyrd.com/topics/nodejs/nodejs.ics', {}, function(err, data) {
test = "Nothing changes";
});
console.log(test);
test.emit(':tell', 'I am done');
}
};
This is the output I get from the could watch when I do "ask simulate -l en-US -t 'start calendar read'" in ASK CLI output on cloudwatch as you can see the test text doesn't change, and would work if it was outside of the function(err, data){}. I don't believe there are any problems with reading in the calendar as the link http://lanyrd.com/topics/nodejs/nodejs.ics downloads a working ics file. The function activates if I try it in the https://npm.runkit.com/ical tool. So I am not sure what I am doing wrong. Also the skill works gives the response when tested in the alexa skill kit development.
You mistype the test.emit(':tell', 'I am done');
instead of this.emit(':tell', 'I am done');
.
Also your code will not return the data from the url, because the this.emit
will be return first rather than your callback function. In order to return the data, you need to put the this.emit
inside the callback function of ical.fromURL
.
var handlers = {
'LaunchRequest':function() {
console.log(test);
ical.fromURL('http://lanyrd.com/topics/nodejs/nodejs.ics', {}, (err, data) => {
test = "Nothing changes";
console.log(test);
// you can edit the response here base on the data you receive.
this.emit(':tell', `I am done: ${test}`);
});
}
};