可以使用 AWS Lambda、Node.js 和 Alexa Skills Kit 阅读 Facebook 帖子的 Alexa 技能

Alexa skill which can read Facebook posts using AWS Lambda, Node.js and the Alexa Skills Kit

我正在使用 AWS Lambda 开发 Alexa skill,Node.js Alexa Skills Kit.I 正在使用 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 function.Unfortunately 中,但我无法使用这些 codes.Please 阅读 Facebook 帖子,在下面的面板中找到这些代码块。此外,我检查了 cloudwatch 日志,因为 well.I 可以看到 "GetNewsIntent",但我没有在 logs.Surprisingly 中看到 "fb-init"、"fb-error" 或 "fb-exit" 条目,没有日志中的错误 well.I 如果有人可以帮助解决该问题,我们将不胜感激。

'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');
    },
};

你实现了account linking了吗?您应该使用 event.session.user.accessToken 作为 setAccessToken().

的参数

我已经删除了 this.response.cardRenderer 、 this.response.speak 并更改了代码 bit.It 工作正常 now.Please 找到下面的代码片段,它可以用来阅读帖子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);
            }
        });
    }
};