1
votes

I'm in the process of creating a facebook messenger bot using AWS Lambda and the Serverless framework. For now I just want it to repeat whatever is sent right back to the user. Here is the code:

'use strict';
var https = require('https');
const axios = require('axios');


var VERIFY_TOKEN = "VERIFY";
var PAGE_ACCESS_TOKEN = "TOKEN";

module.exports.hello = (event, context, callback) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify({
            message: 'Go Serverless v1.0! Your function executed successfully!',
            input: event,
        }),
    };

    callback(null, response);

    // Use this code if you don't use the http event with the LAMBDA-PROXY integration
    // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

// Receive user messages
module.exports.botReply = (event, context, callback) => {

    var data = JSON.parse(event.body);
    console.log("BOT REPLY")

    // Make sure this is a page subscription
    if (data.object === 'page') {

        // Iterate over each entry - there may be multiple if batched
        data.entry.forEach(function(entry) {
            var pageID = entry.id;
            var timeOfEvent = entry.time;
            // Iterate over each messaging event
            entry.messaging.forEach(function(msg) {
                if (msg.message) {
                    console.log("received message");
                    const payload = {
                    recipient: {
                      id: msg.sender.id
                    },
                    message: {
                      text: "test"
                    }
                  };
                    const url = "https://graph.facebook.com/v2.6/me/messages?access_token=" + PAGE_ACCESS_TOKEN;
                    axios.post(url, payload).then((response) => callback(null, response));

                } else {
                    console.log("Webhook received unknown event: ", event);
                    var response = {
                        'body': "ok",
                        'statusCode': 200
                    };

                    callback(null, response);
                }
            });
        });
    }

}

So the bot does successfully echo back the messages, but in my logs I can see it getting executed multiple times. Sometimes the message has no "message" key in the JSON for some reason, so the multiple executions have different outcomes. I believe it has something to do with me sending the message back to the user because when I comment out the axios.post, the problem stops. Any idea why this is happening?

1
In general if Lambda function throws exception, Lambda will retry the function with-in certain interval, are you sure you function not failing when re-try happens? - kosa
Well I don't see in errors in the logs... - Brandon
When you subscribe to facebook messenger webhooks there are several events you can subscribe for. In case you have subscribed for message delivered event, your lambda can be triggered once your successfully delivered. You can get more info on this from developers.facebook.com/docs/messenger-platform/… - Asanka
@Asanka got it! Thanks so much! - Brandon

1 Answers

3
votes

@Asanka figured it out in the comments. Basically, facebook was sending multiple events over that I had not accounted for in my code. Stuff like "message delivered" and "message read" were also events I didn't know about. I just had to unsubscribe to them in the developer console.