2016-08-15 15 views
0

私はAlexaのデバイスがgoogleのニュースAPIからタイトルを読み上げるAlexaのスキルを作ろうとしています。私はJSON urlを持っていて、タイトルを解析してAlexaデバイスで読むことができるように関数を作りたいと思っています。ここでは、これまでの私のコードは次のとおりです。あなたは、APIへのリクエストを行い、アレクサさんのtell機能にその結果を使用する必要がAlexaのスキルをJSONデータ(node.js)を読み込むには?

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

実行している/質問している特定の問題はありますか? – httpNick

+0

@httpNick私は、ニュース記事のタイトルしか持たないように、URLからデータを解析しようとしています。それについてどうすればいいですか? –

答えて

0

(それは/ JSONとかwの主な機能が欠けています)。

Requestモジュールを使用してAPIデータを取得しようとします。 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. 
    } 
}); 

あなたが応答が戻ってくるのを待つ必要がありますので、あなたもBluebird's Promisficationを実装するための機能を編集する必要があります。これにより、APIから結果を得るのに数秒かかることがあるので、応答をtell関数に渡す前に応答を待つことができます。

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

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

最後に、あなたはまた、あなたが必要とする体の値を取得し、TELL関数にそれを使用するためにJSON.parseを使用する必要があります。

var obj = JSON.parse(body); 
response.tell(obj); 
関連する問題