pimax/fb-messenger-php

The answers are not sent to Facebook

Closed this issue · 16 comments

I have this issue in a few apps using pimax/fb-messenger-php and it's was reproduce in pimax/fb-messenger-php-example without modifications.

Details.
The issue happend when someone send a message, Facebook recieve the message and sent to the server that proccess the content to send an answer, this answer is never sent to Facebook.

It's this issue can be related to some Facebook API changes?

  • What kind of message do you sent?
  • Does the message really arrive at the server?
  • Do you receive any errors?

Maybe it's a cURL error, did you check #136 ?

Hi @wittfabian sorry for the late answer,

  • tested sending simple text messages,
  • I checked the messages really arrives the server and it does.
  • No errors was ocurring in server

@Yiidiir I checked the curl php extension and it was installed correctly

@sage393 i mean did you try to set CURLOPT_SSL_VERIFYPEER to false ?
maybe it's something to do with SSL

@Yiidiir where is supposed to set this option?

@sage393 either set

        curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);

inside FbBotApp.php in call() after curl_init() , or use #138 and do $bot->getCurlError() just after you send the message.

@Yiidiir I used $bot->getCurlError() after the message send function but see nothing, I will try to put an output of the send message function

@Yiidiir I try to log the output of the function but not work, It seems like the function never execute, I do not know why, I used the same code in three different installations and it works fine

@sage393 do you understand that you should get the output of the function when the payload is sent from a facebook request? if you're not using a tool like ngrok that allows you to replay the requests you might want to try to store the value of getCurlError() in a file or your db?!

@Yiidiir I understand how to test the code, I made my tests in a VPS with ssl certificate.

I proved that the message was sent from Facebook is catched in my VPS, I wrote the message in a log file, The only think apperaly not work is the response.

In the other two installations of my code I can write successfully in the log file the response of the send() function of FbBotApp.

@sage393 There's still a high chance it's something to do with cURL, add this in the call() function just after curl_setopt($process, CURLOPT_TIMEOUT, 30);

curl_setopt($process, CURLOPT_VERBOSE, 1);
curl_setopt($process, CURLOPT_STDERR, fopen(dirname(__FILE__).'/errorlog.txt', 'w'));

Make sure you give chmod permission (eg. 777) to errorlog.txt which you create in the same folder as FbBotApp.php

If it doesn't work, this is another work around to get what cURL is sending:
add this before curl_exec() :

curl_setopt($process, CURLINFO_HEADER_OUT, true);

after curl_exec() get what cURL sent as headers :

$headers = curl_getinfo($process, CURLINFO_HEADER_OUT);
file_put_contents('errorlog.txt', $headers); // set chmod 777 for errorlog.txt

@Yiidiir nothing seen to work, this is the content of the call() function in FbBotApp class:

public function call($url, $data, $type = self::TYPE_POST)
    {
        $data['access_token'] = $this->token;

        $headers = [
            'Content-Type: application/json',
        ];

        if ($type == self::TYPE_GET) {
            $url .= '?'.http_build_query($data);
        }

        $process = curl_init($this->apiUrl.$url);
        curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($process, CURLOPT_HEADER, false);
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
        curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($process, CURLOPT_VERBOSE, 1);
        curl_setopt($process, CURLOPT_STDERR, fopen(dirname(__FILE__).'/errorlog.log', 'w'));
    

        if($type == self::TYPE_POST || $type == self::TYPE_DELETE) {
            curl_setopt($process, CURLOPT_POST, 1);
            curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($data));
        }

        if ($type == self::TYPE_DELETE) {
            curl_setopt($process, CURLOPT_CUSTOMREQUEST, "DELETE");
        }

        curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        $return = curl_exec($process);

        $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
        file_put_contents('errorlog.log', $headers);

        /**
         * Check for cURL Errors and, if found display the error code
         *
         * @see http://php.net/manual/en/function.curl-error.php
         */
        $curl_errors = curl_error($process);
        if ($curl_errors) {
            $this->curl_error = $curl_error;
        }

        curl_close($process);

        return json_decode($return, true);
    }

The permissions in the package directory:

-rwxr-xr-x 1 user user 13746 Mar  4 15:58 FbBotApp.php
-rwxr-xr-x 1 user user 18046 Mar  4 15:58 LICENSE.txt
drwxrwxr-x 2 user user  4096 Mar  4 15:58 Menu
drwxrwxr-x 2 user user  4096 Mar  4 15:58 Messages
-rwxr-xr-x 1 user user   460 Mar  4 15:58 README.md
-rwxr-xr-x 1 user user  1047 Mar  4 15:58 UserProfile.php

@Yiidiir I updated the code and tested in other server and works fine, I cant figure out why still have the issue in my main server

public function call($url, $data, $type = self::TYPE_POST)
    {
        $data['access_token'] = $this->token;

        $headers = [
            'Content-Type: application/json',
        ];

        if ($type == self::TYPE_GET) {
            $url .= '?'.http_build_query($data);
        }

        $process = curl_init($this->apiUrl.$url);
        curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($process, CURLOPT_HEADER, false);
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
        curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($process, CURLOPT_VERBOSE, 1);
        curl_setopt($process, CURLOPT_STDERR, fopen(dirname(__FILE__).'/errorlog.log', 'w'));
    

        if($type == self::TYPE_POST || $type == self::TYPE_DELETE) {
            curl_setopt($process, CURLOPT_POST, 1);
            curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($data));
        }

        if ($type == self::TYPE_DELETE) {
            curl_setopt($process, CURLOPT_CUSTOMREQUEST, "DELETE");
        }

        curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($process, CURLINFO_HEADER_OUT, true);
        $return = curl_exec($process);

        $headers = curl_getinfo($process, CURLINFO_HEADER_OUT);
        file_put_contents(dirname(__FILE__).'errorlog.log', $headers);

        /**
         * Check for cURL Errors and, if found display the error code
         *
         * @see http://php.net/manual/en/function.curl-error.php
         */
        $curl_errors = curl_error($process);
        if ($curl_errors) {
            $this->curl_error = $curl_error;
        }

        curl_close($process);

        return json_decode($return, true);
    }

@Yiidiir Sorry for the late answer, finally I solved my problem, the code works the problem was in my Database, my Facebook user ID it was disabled to the bot, the problem has nothing with this package