Vidhya Sri
Vidhya Sri

Reputation: 1823

Facebook Messenger Postback Called thrice

I have successfully created a facebook messenger bot. I have implemented messaging postback also in my app and it seems to work fine in my android device (Messenger V 68.0.0.22.67).

But when I try with Samsung Galaxy S5 with messenger V 76.0.0.13.70, all the event postback are called thrice. I even tried with some other existing facebook bots to test the postback event in the same device and it is the same situation there also. I am getting response thrice for every postback.

Could it be some issue with the messenger version? I couldn't find any issue log related to this so far.

Any help would be highly appreciated.

Upvotes: 0

Views: 172

Answers (1)

Anirudh
Anirudh

Reputation: 15807

This happens when the signature sent back by Facebook in the headers does not match the signature you calculate for the payload. This returns a 500 to Facebook and Facebook retries this request twice (for a total of three requests).

PROBLEM

The problem is some of the characters have to UNICODE encoded.

For example:

@ character has to be replaced with "\u0040" before the hash is calculated.

SOLUTION

I have simply done this in the verifySignature in Facebook Messenger module

let hash = crypto.createHmac('sha1',this.APP_SECRET).update(JSON.stringify(req.body).replace(/@/g,"\\u0040").replace(/\//g, '\\/')).digest('hex');

This article delves deeper into the solutions.

https://hackernoon.com/regenerating-a-raw-request-payload-an-impossible-task-e4133fb2571

Upvotes: 1

Related Questions