question

samayshamdasani avatar image
samayshamdasani asked

How to make an Alexa skill read out JSON data (node.js)?

Hey guys, I'm a new developer of Alexa skills and ran into a problem. I'm trying to make an Alexa skill in which the Alexa device reads out the titles from the google news api. I have a JSON url and I'd like to make a function so that the titles can be parsed out and read by the Alexa device. Here's my code so far: (it's missing the main function w/ JSON and stuff - that's where I need some help)

/**
 * App ID for the skill
 */
var APP_ID = undefined;
/**
 * The AlexaSkill prototype and helper functions
 */
var AlexaSkill = require('./AlexaSkill');

var News = function () {
    AlexaSkill.call(this, APP_ID);
};

// Extend AlexaSkill
News.prototype = Object.create(AlexaSkill.prototype);
News.prototype.constructor = News;

News.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {

};

News.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
    handleNewsRequest(response);
};


News.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {

};

News.prototype.intentHandlers = {
    "NewsIntent": function (intent, session, response) {
        handleNewsRequest(response);
    },

    "AMAZON.HelpIntent": function (intent, session, response) {
        response.ask("You can ask me for the latest news headlines in the world right now. Simply ask Top News for the latest news.");
    },

    "AMAZON.StopIntent": function (intent, session, response) {
        var speechOutput = "Goodbye";
        response.tell(speechOutput);
    },

    "AMAZON.CancelIntent": function (intent, session, response) {
        var speechOutput = "Goodbye";
        response.tell(speechOutput);
    }
};

/**
 * News API
 */
function handleNewsRequest(response) {
  /**
   * This is where I need help!!!!!!!!
   */







    // Create speech output
    var speechOutput =     ;
    var cardTitle = "Top News";
    response.tellWithCard(speechOutput, cardTitle, speechOutput);
}

// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
    // Create an instance of the Top News skill.
    var news = new News();
    news.execute(event, context);
};
alexa skills kitskillhow-to
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

KirkC@Amazon avatar image
KirkC@Amazon answered

Hello samayshamdasani. We provide a few sample skills on GitHub, each of which cover particular programming concepts. It just so happens that the History Buff sample deals with exactly this, importing and parsing .json from an external source. You can find it here:

https://github.com/amzn/alexa-skills-kit-js/tree/master/samples/historyBuff

You can download the source code and find our other Javascript samples here as well:

https://github.com/amzn/alexa-skills-kit-js

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

newuser-a228c23d-aef3-464a-aa34-ba1fcc540fba avatar image
newuser-a228c23d-aef3-464a-aa34-ba1fcc540fba answered

The History Buff example is in java, no? How would we do it in node.js?

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.