【问题标题】:Alexa skill which can read Facebook posts using AWS Lambda, Node.js and the Alexa Skills KitAlexa 技能,可以使用 AWS Lambda、Node.js 和 Alexa Skills Kit 阅读 Facebook 帖子
【发布时间】:2017-11-27 20:48:49
【问题描述】:

我正在使用 AWS Lambda、Node.js 和 Alexa Skills Kit 开发 Alexa 技能。我正在使用来自 Skill-sample-nodejs-fact 项目的分支并成功部署并测试了示例事实项目。现在我正在尝试修改该代码以阅读某些 Facebook 提要上的帖子。首先我尝试开发一些可以阅读帖子的节点应用程序并且它是成功的。请找到以下代码供您参考。我使用了 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');
    },
};

【问题讨论】:

    标签: node.js facebook-graph-api aws-lambda amazon-cloudwatch alexa


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复 Ben。是的,我做到了。我硬编码了 this.event.session.user.accessToken.Account 的值。链接已成功。
    【解决方案2】:

    我已经删除了 this.response.cardRenderer , this.response.speak 并更改了代码位。它现在工作正常。请找到下面的代码 sn-p 可用于阅读 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);
                }
            });
        }
    };

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 2018-01-05
      • 2016-04-01
      • 2016-12-25
      • 2017-08-03
      • 2023-01-21
      • 2017-02-15
      • 1970-01-01
      相关资源
      最近更新 更多