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.
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);
}
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!');
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).
Can you check following things.
- You are the admin of that page and you are sending message from admin account only.
- Are you receiving messages send by you on the script log these messages in some file to check?
- 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.
- Make sure that token you created when creating webhook is placed is correct.
- Have you copied the generated token.
Also plz send your code.
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"
来源:https://stackoverflow.com/questions/36657366/messenger-bot-in-php-no-response-back