Skip to content

Instantly share code, notes, and snippets.

@Shaked
Created October 18, 2017 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shaked/9db67cea0ba5b7e9ade039c08c18780b to your computer and use it in GitHub Desktop.
Save Shaked/9db67cea0ba5b7e9ade039c08c18780b to your computer and use it in GitHub Desktop.
Facebook Duplicate Messages Possible Fix

Description

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

<?php
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: http://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response
set_time_limit(0);
ob_start();
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");
ob_end_flush();
ob_flush();
flush();
}
// run before everything (see explanation in README.md)
acknowledgeRequest();
//doStuff
//doMoreStuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment