My First Amazon Alexa Skill: Reading RSS using Node.js

Alexa

I recently had my first Amazon Alexa skill published and am eagerly awaiting my Alexa Dev hoodie! Developing and publishing an Alexa skill is a lot easier than you might think especially with all the tutorials and resources Amazon provides. I decided to develop a skill that’ll read the WOD (Workout of the day) from the crossfit gym I go to. The only problem is that my gym’s website didn’t have a public API even though it was running on WordPress. Fortunately it did have an unpublished RSS feed that I was able to find the URL to.
I chose to create my skill using Node.js without having to use any node modules. This was a challenge since I couldn’t read in the RSS feed directly without running into any cross-domain issues. To overcome this I used Yahoo’s YQL to read the RSS feed and return the results via JSON. YQL can read any RSS feed using it’s SQL-like syntax.

select * from rss where url = 'RSS URL'

It even provides you with a REST url. Once I had that, the rest was easy.

var https = require('https');

var optionsget = {
    host : 'query.yahooapis.com', 
    port : 443,
    path : '/v1/public/yql?q=rssurl&format=json&diagnostics=true&callback=', // the rest of the url with parameters if needed
    method : 'GET' 
};

function getWod(eventCallback) {
    var reqGet = https.request(optionsget, function(res) {
        var finalData = "";

        res.on('data', function(d) {
            finalData += d.toString();
        });

        res.on("end", function() {     
            var obj = JSON.parse(finalData);
            eventCallback(obj);
        });
    });

    reqGet.end();
    reqGet.on('error', function(e) {
        console.log("Got error: ", e);
    });
}



8 Comments

Looks like a great way to save time (YQL), however I’m running into issues with my node.js syntax – is the code above _exactly_ as it would appear in the Lambda function (aside from the YQL API call)? Here’s one error I got, for instance:

{
“errorMessage”: “Handler ‘handler’ missing on module ‘index'”
}

The code is an exact snippet of the method I use to retrieve the json. The error you’re getting is related to the export function in your code.

I was able to get beyond that error but am getting null values returned from the method. Could you post the whole Lambda function or something similar – having a difficult time wrapping my head around node.js syntax. Thanks.

Here’s a snippet in additional to what is in the post:

exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID; //this should be your App ID
alexa.registerHandlers(handlers);
alexa.execute();
};

var handlers = {
'LaunchRequest': function () {
var reprompt = "What can I help you with?";
this.emit(':ask', reprompt, reprompt);
},
'GetNewFactIntent': function () {
this.emit('GetFact');
},
'GetFact': function () {
var t = this;
getWod(function (events) {
if (events.length == 0) {
var speechOutput = "There was a problem retrieving the json. Please try again later.";
t.emit(':tell', speechOutput);
} else {
// Create speech output
var speechOutput = events;
var cardTitle = "Your card title";
t.emit(':tellWithCard', speechOutput, cardTitle, speechOutput);
}
});
},
'AMAZON.CancelIntent': function () {
var speechOutput = "Goodbye";
this.emit(':tell', speechOutput);
},
'AMAZON.StopIntent': function () {
var speechOutput = "Goodbye";
this.emit(':tell', speechOutput);
}
};

Hope that helps you.

Hi, I think this is what I want to do, but I don’t understand the code, do you have the full lambda function code please?

Leave a Reply

Your email address will not be published. Required fields are marked *

Please Do the Math      
 

%d bloggers like this: