![](/img/trans.png)
[英]How do I create an Alexa Skill that gets data from an HTTP/HTTPS API (using “Alexa Skills Kit” for Node.js on AWS Lambda)
[英]Alexa skill which can read Facebook posts using AWS Lambda, Node.js and the Alexa Skills Kit
我正在使用AWS Lambda,Node.js和Alexa Skills Kit开发Alexa技能,我正在使用来自Skill-sample-nodejs-fact项目的分支,并成功部署并测试了示例事实项目,现在我正在尝试修改它代码阅读一些Facebook的feeds.First帖子中,我试图开发一些节点应用可以读帖和它successful.Please找到下面为您reference.I代码中使用FB模块- https://www.npmjs.com/package / fb
const FB = require('fb');
FB.setAccessToken('abc');
const query='cnninternational/posts';
FB.api(query, function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(res);
});
接下来,我尝试将上述代码块集成到lambda函数中,不幸的是,我无法使用这些代码阅读Facebook帖子,请在下面的面板中找到这些代码块。此外,我还检查了cloudwatch日志。 “ GetNewsIntent”,但在日志中没有看到“ fb-init”,“ fb-error”或“ fb-exit”条目。令人惊讶的是,日志中也没有错误。如果有人可以提供帮助,我将不胜感激解决这个问题。
'use strict';
const Alexa = require('alexa-sdk');
const FB = require('fb');
const APP_ID = 'abc';
const SKILL_NAME = 'test';
const GET_FACT_MESSAGE = "Here's your news: ";
const STOP_MESSAGE = 'Goodbye!';
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'LaunchRequest': function () {
this.emit('GetNewsIntent');
},
'GetNewsIntent': function () {
console.log('GetNewsIntent');
const speechOutput = GET_FACT_MESSAGE;
const query='cnninternational/posts';
FB.setAccessToken('abc');
FB.api(query, function (res) {
console.log('fb-init');
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
console.log('fb-error');
return;
}
console.log(res);
speechOutput = speechOutput + res;
console.log('fb-exit');
});
this.response.cardRenderer(SKILL_NAME, speechOutput);
this.response.speak(speechOutput);
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
};
您是否实现了帐户关联 ? 您应该使用event.session.user.accessToken
作为setAccessToken()
的参数。
我已经删除了this.response.cardRenderer,this.response.speak并更改了代码位。它现在可以正常工作。请找到以下代码片段,可用于阅读BBC Facebook页面上的帖子。
var accessToken = ''; exports.handler = function(event, context, callback) { var alexa = Alexa.handler(event, context); alexa.appId = APP_ID; alexa.registerHandlers(handlers); alexa.execute(); }; const handlers = { 'NewSession': function() { var welcomeMessage = "Welcome to Athena"; welcomeMessage = welcomeMessage +"<break time=\\"1s\\"/>"+ "<audio src='https://s3.amazonaws.com/my-ssml-samples/Flourish.mp3' />"+"<break time=\\"1s\\"/>"; welcomeMessage += HELP_MESSAGE; accessToken = this.event.session.user.accessToken; if (accessToken) { FB.setAccessToken(accessToken); this.emit(':ask', welcomeMessage, HELP_REPROMPT); } else { // If we don't have an access token, we close down the skill. this.emit(':tellWithLinkAccountCard', "This skill requires you to link a Facebook account. Seems like you are not linked to a Facebook Account. Please link a valid Facebook account and try again."); } }, 'LaunchRequest': function () { this.emit('NewSession'); }, 'ReadBbcNewsFacebookPostsIntent': function () { var alexa = this; FB.api("bbcnews/posts", function (response) { if (response && !response.error) { if (response.data) { var output = "Here are recent posts" + "<break time=\\"1s\\"/>"; var max = 5; for (var i = 0; i < response.data.length; i++) { if (i < max) { output += "<break time=\\"1s\\"/>" + "Post " + (i + 1) + response.data[i].message.replace(/(?:https?|ftp):\\/\\/[\\n\\S]+/g, '') + ". "; } } alexa.emit(':ask', output+ ", What would you like to do next?",HELP_MESSAGE); } else { // REPORT PROBLEM WITH PARSING DATA } } else { // Handle errors here. console.log(response.error); this.emit(':tell', EMPTY_ACCESS_TOKEN_MESSAGE, TRY_AGAIN_MESSAGE); } }); } };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.