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);
}