Messenger Bot in PHP: No Response Back

半世苍凉 提交于 2019-12-04 15:28:44

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).

lego king

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.

shaghabo

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!