Search code examples
phpfacebookfacebook-messengerfacebook-messenger-bot

Empty message from facebook messenger bot


I followed the guide to build a facebook messenger bot from here

but in my software I receive alway an empty response.

here my bot code:



    define('PAGE_TOKEN',"xxx");
    define('VERIFY_TOKEN',"xxx");

    if(isset($_GET['hub_mode']) && isset($_GET['hub_challenge']) && isset($_GET['hub_verify_token'])) {
        if($_GET['hub_verify_token']==VERIFY_TOKEN && $_GET['hub_mode']=='subscribe') {
            echo $_GET['hub_challenge'];
        }
    }

    $input = json_decode(file_get_contents('php://input'), true);
    // Get the Senders Graph ID
    $sender = $input['entry'][0]['messaging'][0]['sender']['id'];
    // Get the returned message
    $message = $input['entry'][0]['messaging'][0]['message']['text'];

    error_log($input);
    error_log($sender);
    error_log($message);

the verify phase is fine, but when i send a messagge to my bot in my error_log file I found only empty values:



    [Sun Aug 13 17:35:49.617919 2017] [:error] [pid 22501] [client 173.252.124.11:18834]
    [Sun Aug 13 17:35:49.617951 2017] [:error] [pid 22501] [client 173.252.124.11:18834]
    [Sun Aug 13 17:35:49.617972 2017] [:error] [pid 22501] [client 173.252.124.11:18834]

Here there is also my access_log:



    173.252.124.30 - - [13/Aug/2017:17:35:48 +0200] "POST /webhook HTTP/1.1" 301 3621 "-" "-"
    173.252.124.11 - - [13/Aug/2017:17:35:49 +0200] "GET /webhook/ HTTP/1.1" 200 328 "-" "-"

so why the $input variable is alway empty?


Solution

  • 173.252.124.30 - - [13/Aug/2017:17:35:48 +0200] "POST /webhook HTTP/1.1" 301 3621 "-" "-"
    173.252.124.11 - - [13/Aug/2017:17:35:49 +0200] "GET /webhook/ HTTP/1.1" 200 328 "-" "-"
    

    Your server answers the POST request with a 301 redirect, so that the client (Facebook) has to make a GET request next, and therefor you see no more POST data.

    Seeing that the first URL is /webhook, and gets redirected to /webhook/, this is likely just an automatic trailing slash redirect, and should be fixed once you specify the correct URL with the trailing slash in your app settings to begin with.