簡體   English   中英

可以使用AWS Lambda,Node.js和Alexa技能套件閱讀Facebook帖子的Alexa技能

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM