Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2017 20:54
What would you like to do?
Facebook Duplicate Messages Possible Fix


I have been facing a problem where I got duplicate messages from Messenger's API while developing a chatbot.

I have been looking for a solution and tweeted for help. At the end I got to the following explanation:

Duplicate message postbacks is almost always because fb didn't hear a 200 response and the request ending soon enough. Depending on your language this could be tricky. In php for example not only do you have to respond with 200 ok early, you have to actively clear the buffer and end the connection for fb to receive the response. (Then you can do more processing if needed). Other languages may have something similar.

as mentioned here.

I have attached the solution above. Just make sure you put that directly when the PHP entry point starts running

function acknowledgeRequest() {
// In order to return a response and continue processing code, we do this trick.
// this essentially ends the request from the requester (FB)'s perspective.
// source:
echo 'success'; // FB does not care return message as long as it's 200 OK
$size = ob_get_length();
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Status: 200 OK");
header("Content-Encoding: none"); // disable compression
header("Content-Length: {$size}");
header("Connection: close");
// run before everything (see explanation in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment