user5009455
user5009455

Reputation:

Facebook Messenger Bot - PHP cURL messages

I've been fiddling around with Facebook Messenger Platform for the past couple days and have run into an issue. PHP has been the primary language.

Successfully, I've been able to implement a couple API's into the system, through plain text. (See image below)

enter image description here

This is what the system looks like:

$input = json_decode(file_get_contents('php://input'), true);
$senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$answer = "I don't understand that. Is that another language? Type 'hi' to get started.";

if($message == "hi") {
    $answer = "Yo!";
}

All of this comes from the Facebook Messenger Getting Started if you're not familiar.

What I'm attempting to do now is pass an image through cURL onto JSON. This is something I'm unfamiliar with, but have found two great sources to help me with this task. POSTing JSON Data With PHP cURL and Create nested list from Multidimensional Array.

Here is the result:

if($message == "test") {
$data = array("message" => array("attachement" => array('"type" => "image"'),"payload" => array('"url" => "http://example.com"')));                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=TOKEN_GOES_HERE');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                               
$answer = curl_exec($ch);
}

Here is the response I receive: enter image description here

I know for sure, that the parameters are not properly being picked up by cURL. Though, my limited knowledge on cuRL, suggests otherwise. My question is, how could I still achieve this? I want to be able to pass an image through JSON into messenger, using PHP.

Upvotes: 0

Views: 3651

Answers (1)

Hui-Yu Lee
Hui-Yu Lee

Reputation: 979

I think your post request works fine, but due to the error, you didn't pass the whole json data.

Below is how a image generic message looks like, where did you put the recipient in your data?

{
  "recipient":{
    "id":"USER_ID"
  },
  "message":{
    "attachment":{
      "type":"image",
      "payload":{
        "url":"https://petersapparel.com/img/shirt.png"
      }
    }
  }
}

reference: https://developers.facebook.com/docs/messenger-platform/send-api-reference#guidelines

Upvotes: 1

Related Questions