0

I'm trying to set up a fb messenger chatbot but don't seem to be able to get the webhook callback url verified. Every time I try to verify it I get this error message - The URL couldn't be validated. Response does not match challenge, expected value = '1596214014', received=''

Here's the screenshot:

Screenshot

Here's the php I'm using -

<?php

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'token_my_token') {
echo $challenge;
}

I've also tried

echo $_GET['hub_challenge'];

and just

echo file_get_contents('php://input');

All of these result in the same error message as above. Basically, as far as I can tell facebook isn't sending a GET request to my server or if it is it doesn't include any data. Can anyone tell if I am doing something wrong or if there is a setting I need to change to ensure facebook is sending the data correctly?

Edit - When checking the access logs this is what I find, which looks like facebook isn't sending any data in the get request.

2a03:2880:1010:dffb:face:b00c:0:8000 - - [19/Apr/2016:20:50:06 +0000] "GET /wp-content/plugins/applications/fbmessenger.php HTTP/1.0" 200 - "-" "facebookplatform/1.0 (+http://developers.facebook.com)

Thanks

PeterD
  • 19
  • 1
  • 1
  • 5
  • Have you checked the logs what Facebook do send you? – WizKid Apr 19 '16 at 16:34
  • Yes, edited the post above with that info. Thanks. – PeterD Apr 19 '16 at 21:07
  • **echo $_REQUEST['hub_challenge'];** is the key to get it work. Make sure you are sending it back and you are not sending anything more! (do not echo more code) AND, it takes sometimes 8-9 trials to get it work. Hit "save" one after another if you are getting errors. Strange but mine worked after 9 trials. (without changing my code) – Tarik Jun 17 '17 at 13:05

4 Answers4

4

just try my code and it's gonna work.

 $challenge = $_REQUEST['hub_challenge'];
  $verify_token = $_REQUEST['hub_verify_token'];

  if ($verify_token === 'Your's app token') {
  echo $challenge;
  }
  //Token of app
 $row = "Token";


 $input = json_decode(file_get_contents('php://input'), true);

//Receive user
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
 //User's message
 $message = $input['entry'][0]['messaging'][0]['message']['text'];



//Where the bot will send message
 $url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$row;


 $ch = curl_init($url);

//Answer to the message adds 1
if($message)
{
 $jsonData = '{
    "recipient":{
        "id":"'.$sender.'"
      }, 
    "message":{
        "text":"'.$message. ' 1' .'"
      }
 }';
};



 $json_enc = $jsonData;

 curl_setopt($ch, CURLOPT_POST, 1);

 curl_setopt($ch, CURLOPT_POSTFIELDS, $json_enc);

 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));  

 if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
 }
Grynets
  • 2,477
  • 1
  • 17
  • 41
2

you have to return Challenges so Facebook can verify its correct Url and Token Match

 <?php

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'token_my_token') {
echo $challenge;
}

Facebook Docs Link ( In Node.js ) You can see challenge return after verifying the token

https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup

MUHAMMAD USMAN
  • 149
  • 2
  • 6
0

Could you try my API? https://github.com/Fritak/messenger-platform

If you set it like in example, it should work:

// This is just an example, this method of getting request is not safe!
$stream  = file_get_contents("php://input");
$request = empty($stream)? $_REQUEST : $stream;

$bot = new \fritak\MessengerPlatform(
        ['accessToken'      => 'token_for_app',
         'webhookToken'     => 'my_secret_token',
         'facebookApiUrl'   => 'https://graph.facebook.com/v2.6/me/' //2.6 is minimum
        ], $request);

    if($bot->checkSubscribe())
    {
        print $bot->request->getChallenge();
        exit;
    }

If not, problem is somewhere between Facebook and script, not in PHP itself. Go check apache settings etc.

Well issue might be on facebook side, they had some issues over past few days...

fritak
  • 191
  • 3
0

Have only this code in your php file: (fbmessenger.php)

<?php 
// header('HTTP/1.1 200 OK');

/* GET ALL VARIABLES GET & POST */
foreach ($_REQUEST AS $key => $value){
    $message .= "$key => $value ($_SERVER[REQUEST_METHOD])\n";
}
$input = file_get_contents("php://input");
$array = print_r(json_decode($input, true), true);
file_put_contents('fbmessenger.txt', $message.$array."\nREQUEST_METHOD: $_SERVER[REQUEST_METHOD]\n----- Request Date: ".date("d.m.Y H:i:s")." IP: $_SERVER[REMOTE_ADDR] -----\n\n", FILE_APPEND);
echo $_REQUEST['hub_challenge'];

You will have requests saved in a file called "fbmessenger.txt" in the same directory.

Note that for some strange reason you may need to submit few times to get it approved & saved! (I had to hit "save" 8-9 times before fb approved link)

Make sure you use https (SSL) connection and once your connection is done, verify your token with "hub_verify_token" to make sure request is coming from fb.

Tarik
  • 4,270
  • 38
  • 35