0

The Facebook Messenger API is pretty sweet overall. However, whenever an user sends a sticker (The balloon thumb up sticker for example) to my bot, it always freezes for around 20 seconds. If the user sends a lot of stickers, the bot will simply crash and Facebook will send me "Webhook failing" developer alert.

I suspect there is something wrong with my code but I couldn't find any error. Something interesting I found is that when the bot crashed and Facebook sends me the "Webhook failing" alert, the bot will be revived and back to responding to messages if I resubscribe my app to the page by entering the following in the command line.

curl -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=<PAGE_ACCESS_TOKEN>"

Here is some information that may be helpful to you.

Repo: https://github.com/lorix-lpan/r-score-god

Server.js: https://github.com/lorix-lpan/r-score-god/blob/master/src/server.js

Facebook Page: https://www.facebook.com/rscoregod/

Note: The bot is still not available to the public at the moment, message it so I can add you as a test user.

Thank you very much!

lpan
  • 448
  • 5
  • 10

1 Answers1

2

Eventually, I figured out the problem myself. There is indeed something wrong with my code (or facebook's getting started code ;) ).

From Messenger platform's getting started page

The post handler is defined as the following

app.post('/webhook/', function (req, res) {
  messaging_events = req.body.entry[0].messaging;
  for (i = 0; i < messaging_events.length; i++) {
    event = req.body.entry[0].messaging[i];
    sender = event.sender.id;
    if (event.message && event.message.text) {
      text = event.message.text;
      sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200));
    }
  }
  res.sendStatus(200);
});

However, when a sticker or GIF is sent to the server, the "message" field of the "event" variable will be empty. Thus, the code inside the following block will not be evaluated and no HTTP request will be made since the sendTextMessage function POST to the Facebook server.

if (event.message && event.message.text) {
  text = event.message.text;
  sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200));
}

I solved the issue by simply adding another simple if statement for stickers and GIF (or other attachments). When an attachment is sent by the user (either a GIF or a sticker), the "event" variable will have a "attachments" property. In other words, event.attachments will be defined. My solution is the following:

app.post('/webhook/', function (req, res) {
  messaging_events = req.body.entry[0].messaging;
  for (i = 0; i < messaging_events.length; i++) {
    event = req.body.entry[0].messaging[i];
    sender = event.sender.id;
    if (event.attachments) {
      sendTextMessage(sender, 'I am an attachment');
    }
    if (event.message && event.message.text) {
      text = event.message.text;
      sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200));
    }
  }
  res.sendStatus(200);
});

Check out this commit for more detail

lpan
  • 448
  • 5
  • 10
  • Hi Stumbled upon this question as I was looking for a solution on a similar issue in my bot. Do you mind taking a look at my issue posted here and let me know what I am missing? https://stackoverflow.com/questions/48255932/res-send200-issue-on-facebook-messenger-bot/48255982#48255982 – SamT Jan 18 '18 at 22:21