Messenger Bot in PHP: No Response Back
Asked Answered
O

5

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.

Overkill answered 15/4, 2016 at 21:46 Comment(2)
This answer helps me in your situation... https://mcmap.net/q/1781548/-facebook-chat-bot-php-webhook-sending-multiple-repliesHillinck
Maybe a silly question but the account your talking to the bot through...have you added on to the app as an Admin/tester?Bereft
B
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);
}
Bereft answered 25/4, 2016 at 13:54 Comment(1)
when I use your code, I got the following error. <b>Notice</b>: Undefined index: hub_challenge in <b>C:\xampp\htdocs\webhook.php</b> on line <b>2</b><br /> <br /> <b>Notice</b>: Undefined index: hub_verify_token in <b>C:\xampp\htdocs\webhook.php</b> on line <b>3</b><br /> Please help me how can I solve it.Roswell
C
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!');
Cassy answered 15/4, 2016 at 22:20 Comment(4)
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.Overkill
Anyone has any input on this. I've literally done everything. Changed pages, apps, servers. Still not happening.Overkill
Have subscribed to a page? Without subscribe you can't receive anything.Concede
@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. :(Overkill
A
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.

Asphodel answered 18/6, 2016 at 19:24 Comment(0)
V
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).

Vaccinate answered 11/9, 2016 at 11:14 Comment(0)
C
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"
Crossfertilization answered 18/2, 2017 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.