0
votes

I am developing an Alexa skill using AWS Lambda, Node.js and the Alexa Skills Kit.I am using a forked from skill-sample-nodejs-fact project & successfully deployed & tested the sample fact project .Now I am trying to modify that code to read posts on some Facebook feeds.First I tried to develop some node application which can read posts & it was successful.Please find below code for your reference.I used fb module - 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);
});

Next, I tried to integrate above code block into the lambda function.Unfortunately, I was unable, to read Facebook posts using these codes.Please find those code blocks in the below panel .Also, I checked cloudwatch logs as well.I can see the "GetNewsIntent", but I didn't see "fb-init" , "fb-error" or "fb-exit"entries in logs.Surprisingly, no error in logs as well.I would much appreciate it if someone can help to solve that issue.

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

2 Answers

0
votes

Have you implemented account linking? You should be using event.session.user.accessToken for the parameter to setAccessToken().

0
votes

I have removed this.response.cardRenderer , this.response.speak & changed the code bit.It's working fine now.Please find the below code snippet which can be used to read posts on the BBC Facebook page.

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