简体   繁体   English

如何使Alexa技能读出JSON数据(node.js)?

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

I'm trying to make an Alexa skill in which the Alexa device reads out the titles from the google news api. 我正在尝试使Alexa技能能够使Alexa设备从Google新闻api读取标题。 I have a JSON url and I'd like to make a function so that the title can be parsed out and read by the Alexa device. 我有一个JSON网址 ,我想创建一个函数,以便标题可以被Alexa设备解析和读取。 Here's my code so far: (it's missing the main function w/ JSON and stuff) 到目前为止,这是我的代码:(它缺少带有JSON和其他内容的主要功能)

/**
 * 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);
};

You need to make a request to the api and use that result in Alexa's tell function. 您需要向api发出请求,并在Alexa的tell函数中使用该结果。

I would try using the Request module to get the API data. 我会尝试使用Request模块来获取API数据。 The first example on the npm website should show you how to do that. npm网站上的第一个示例应向您展示如何执行此操作。

request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
  }
});

Because you'll need to wait for the response to come back, you will need to edit the function to also implement Bluebird's Promisfication . 因为您需要等待响应返回,所以您需要编辑该函数以实现Bluebird的Promisfication This will allow you to wait for the response before passing it to the tell function because it could take a few seconds to get a result from the API. 这样一来,您就可以等待响应,然后再将其传递给tell函数,因为从API获取结果可能需要花费几秒钟的时间。

var Promise = require("bluebird");
var Request = require("request");
Promise.promisifyAll(Request);

Request.getAsync(url)
.then(function(error, response, body){
    console.log(body);
});

Lastly, you will also need to use JSON.parse to get the value of the body that you need and use it on the tell function. 最后,您还需要使用JSON.parse来获取所需主体的值,并将其用于tell函数。

var obj = JSON.parse(body);
response.tell(obj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM