2

I am trying to build a test messenger bot in PHP. My web hook gets setup up perfectly and even the page subscription is done correctly. However, my bot does not respond to any text in messenger. I have tried to change app IDs, page IDs, just to make sure if there are issues with any of that. I have also tried various methods including basic curl as outlined here: Facebook Chat bot (PHP webhook) sending multiple replies

and tried 2 different php libraries: https://github.com/Fritak/messenger-platform https://github.com/pimax/fb-messenger-php

I get no PHP errors, the challenge is still successful at Facebook's end. My SSL certificate is fine, yet I am unable to get the bot respond.

Any help on this will be greatly appreciated.

Community
  • 1
  • 1
Gautamm Mehra
  • 23
  • 1
  • 5
  • This answer helps me in your situation... http://stackoverflow.com/a/36616229/2990234 – Anfuca Apr 15 '16 at 23:17
  • Maybe a silly question but the account your talking to the bot through...have you added on to the app as an Admin/tester? – Lee Woodman Apr 28 '16 at 13:29

5 Answers5

1

Check that CURL is installed correctly. Try this simple Gist, https://gist.github.com/visitdigital/58c71acb123870d1ac2ec714d7536587

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

// Set this Verify Token Value on your Facebook App 
if ($verify_token === 'YOURVERIFYTOKEN') {
  echo $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'];

//API Url and Access Token, generate this token value on your Facebook App Page

$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=ACCESSTOKEN';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
    "recipient":{
        "id":"' . $sender . '"
    }, 
    "message":{
        "text":"The message you want to return"
    }
}';

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request but first check if the message is not empty.
if(!empty($input['entry'][0]['messaging'][0]['message'])){
  $result = curl_exec($ch);
}
Lee Woodman
  • 1,319
  • 2
  • 16
  • 30
  • when I use your code, I got the following error. `Notice: Undefined index: hub_challenge in C:\xampp\htdocs\webhook.php on line 2

    Notice: Undefined index: hub_verify_token in C:\xampp\htdocs\webhook.php on line 3
    ` Please help me how can I solve it.
    – Cloud Oct 27 '16 at 03:57
0

You need to send response by yourself when you are reciving messages (see documentation).

I don't how you do that for pimax API, sorry, but for my API you can do it this way:

// Messenger is calling your URL, someone is sending a message...
$messages = $bot->getMessagesReceived();

// Now you need an ID
$userToSendMessage = $messages[0]->messaging[0]->sender->id;

// Send answer
$bot->sendMessage($userToSendMessage, 'Hi!');
  • Hi Fritak, Thank you for your answer. I am using the index.php as is with only changing the tokens. Your script already has the sendMessage code, yet its not trigging for some reason. – Gautamm Mehra Apr 16 '16 at 14:31
  • Anyone has any input on this. I've literally done everything. Changed pages, apps, servers. Still not happening. – Gautamm Mehra Apr 17 '16 at 16:07
  • Have subscribed to a page? Without subscribe you can't receive anything. – fritak Apr 20 '16 at 06:17
  • @fritak, yes. Have done that. the subscription goes off perfectly. But just after that there is no ping. I tried building a bot on api.ai and that works fine, but i want to make dynamic responses and want the PHP SDK to work. :( – Gautamm Mehra Apr 20 '16 at 22:38
0

Can you check following things.

  1. You are the admin of that page and you are sending message from admin account only.
  2. Are you receiving messages send by you on the script log these messages in some file to check?
  3. On your page account does fb give you some warning like your page is not receiving msg. If not, then msg is sent successfully to you problem lies in your reply.
  4. Make sure that token you created when creating webhook is placed is correct.
  5. Have you copied the generated token.

Also plz send your code.

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48
lego king
  • 558
  • 5
  • 11
0

I had the same problem, the answer was that my webserver was redirecting the request (was adding a slash to the end of the url).

0

1-verify that cURL is properly installed in your computer
2-try sending it manually using this code below in your terminal , make sure to put your access token and the recipient's id. i hade the same problem as you .although i had cURL installed in my computer(windows) it wouldn't send the request .when i changed to linux it worked just fine .
Give it a try.

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient": {
    "id": "USER_ID"
  },
  "message": {
    "text": "hello, world!"
  }
}' "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
Canser Yanbakan
  • 3,780
  • 3
  • 39
  • 65
shaghabo
  • 1
  • 1