Help Api Facebook for bot php


a big greetings to all, I'm a little ask, since I like to dig into google first. but this time I can not find anything that can solve or direct me.

I am developing a bot for facebook in very simple php, what I need is for the person that sent a message, something similar to this: "Attention SRES, I need help with my TDC, my ID is ....." , the same read it, take that message and create a ticket, and respond to the user something like: "Thanks for writing so-and-so ..."

Already create my app on generate my access_token and my verify_token, configure the Webhooks and tick the corresponding boxes, and also subscribe the page that will receive the messages and the bot will respond. also what it says of revision for app of messenger, that is to say everything what it gives me to configure in google as tutorial I have seen.

I've even taken several github codes and watched several videos from youtube and none of them send me the message, and the webhooks accepts the response url, let's say everything as it should be.

One of the last codes used was this:

if (isset($_GET['hub_verify_token'])) { 
    if ($_GET['hub_verify_token'] === 'xxxxx') {
        echo $_GET['hub_challenge'];
    } else {
        echo 'Invalid Verify Token';

/* receive and send messages */
$input = json_decode(file_get_contents('php://input'), true);
if (isset($input['entry'][0]['messaging'][0]['sender']['id'])) {

    $sender = $input['entry'][0]['messaging'][0]['sender']['id']; //sender facebook id
    $message = $input['entry'][0]['messaging'][0]['message']['text']; //text that user sent

    $url = '';

    /*initialize curl*/
    $ch = curl_init($url);
    /*prepare response*/
    $jsonData = '{
        "id":"' . $sender . '"
            "text":"You said, ' . $message . '"
    /* curl setting to send a json post data */
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    if (!empty($message)) {
        $result = curl_exec($ch); // user will get the message

I would like to advise you if there is another option of the facebok api, if this type of code no longer works, since it is the most seen on the web.

I've used other apis such as twitter (I can even read normal DM teet answer them and interact with the user as if a human answered him.), Instagram has managed to send msj of promotions, but with the one of facebook took two days and does not answer any message.

Please, I would greatly appreciate the help, I have not asked many questions in this forum, if something is wrong, please let me know ...

Thank you very much, regards.

asked by leoncenteno 11.12.2018 в 19:17

1 answer


Hello, make sure that in the webhooks part of the Bot you have enabled these = > messages, messaging_postbacks, messaging_optins, message_deliveries. Although I have never tried it with PHP + FB Messenger, I would do something like this = >

$sender = "<PSI>";
// Por si en el string se nos paso algo mal, es mas facil pasearlo desde un array y generar el json de envío
$jsonData = json_encode(["recipient" => ["id" => $sender], "message" => ["text" => "You said $message"]]);

$ch = curl_init('<PAGE_ACCESS_TOKEN>');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       

$result = curl_exec($ch);
// Deberia de funcionar 

In our case we use something called Bot Framework ( link ) it is easier to configure and also offers the Bot Emulator that allows you to do the tests in local before sending the bot to production :)


answered by 11.12.2018 в 23:42