8

I am trying to send images with Whatsapp Cloud API. Using PHP, I am able to send normal text messages successfully.

When going through the docs, what does 'MEDIA_OBJECT_ID' mean ? An example would be great.

curl -X  POST \
 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
 -H 'Authorization: Bearer ACCESS_TOKEN' \
 -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "PHONE_NUMBER",
  "type": "image",
  "image": {
    "id" : "MEDIA_OBJECT_ID"
  }
}'

thanks

Yoosu
  • 101
  • 2
  • 4
  • https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#get-media-id – tobifasc May 30 '22 at 08:51
  • 2
    I'm guessing you first need to upload the media file, get the media object id from the response and use that id when posting your message? – M. Eriksson May 30 '22 at 08:55

7 Answers7

10

First, you need to upload the media file to the WhatsApp Server then WhatsApp will respond back the MEDIA_OBJECT_ID

$target="/home/rishabh/uploads/myImage.png";
$mime=mime_content_type('myImage.png')

 $file = new CURLFILE($target);
 $file->setMimeType($mime);
 $curl = curl_init();
 curl_setopt_array($curl, array(
    CURLOPT_URL => "https://graph.facebook.com/v13.0/$phoneSid/media",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => array("messaging_product" => "whatsapp", "type"=>$mime, "file"=> $file),
    CURLOPT_HTTPHEADER => array(
         "Authorization: Bearer ".$whatsappToken
     ),
));
$resultWhatsAppMedia = json_decode(curl_exec($curl), true);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

$MEDIA_OBJECT_ID = $resultWhatsAppMedia['id']; //MEDIA OBJECT ID

Now, you will get the media object id. You need to send the media from API

$FileName="Caption Name or Image Name";
$messageBody = [
                "messaging_product"=>"whatsapp",
                "recipient_type" => "individual",
                "to" => "$to_number",
                "type" => "image",
                "image" => [
                    "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
                    "caption" => $FileName,
                ]
            ];

$curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => json_encode($messageBody),
        CURLOPT_HTTPHEADER => array(
            "Authorization:Bearer $YOUR_WHATSAPP_ACCESS_TOKEN",
            'Content-Type: application/json'
        ),
    ));
    $response = json_decode(curl_exec($curl), true);
    $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

Here is the different type of media JSON

 // for mp3
     $messageBody = [
                    "messaging_product"=>"whatsapp", 
                    "recipient_type" => "individual",
                    "to" => "$to_number",
                    "type" => "audio",
                    "audio" => [
                        "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
                    ]
                ];
    
    // for pdf, doc, apk etc
      $messageBody = [
                    "messaging_product"=>"whatsapp",
                    "recipient_type" => "individual",
                    "to" => "$to_number",
                    "type" => "document",
                    "document" => [
                        "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
                        "caption" => $fileName,
                        "filename" => $fileName,
                    ]
                ];
    
     // for mp4
     $messageBody = [
                    "messaging_product"=>"whatsapp",
                    "recipient_type" => "individual",
                    "to" => "$to_number",
                    "type" => "video",
                    "video" => [
                       "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
                        "caption" => $media['fileName'],
                    ]
                ];
    
    $RequestJSON = json_encode($messageBody)
Rishabh Rawat
  • 1,083
  • 9
  • 15
  • should I replace `FROM_PHONE_NUMBER_ID` with `+[country_code][mobile_number]`? e.g. +628123456789 – dapidmini Aug 26 '22 at 04:35
  • @dapidmini no FROM_PHONE_NUMBER_ID is your Facebook phone number ID. Check this image https://miro.medium.com/max/1400/1*ZoH-bVjqupC0nf9kBfD4AQ.png Check the black marker sign in image – Rishabh Rawat Aug 26 '22 at 14:17
  • @dapidmini Check this for more detail https://medium.com/@rishicentury/how-to-use-whatsapp-cloud-api-6c4b4a22fc34 – Rishabh Rawat Aug 26 '22 at 14:19
2

You need to upload the media to https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/media

The response will give you the "MEDIA_OBJECT_ID"

OR

use image link instead

curl -X  POST \
 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
 -H 'Authorization: Bearer ACCESS_TOKEN' \
 -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "PHONE_NUMBER",
  "type": "image",
  "image": {
    "link" : "Image URL"
  }
}'
Whebcraft
  • 168
  • 7
2

Example using Postman

URL:

https://graph.facebook.com/{{Version}}/{{Phone-Number-ID}}/messages

OBJECT:

{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "5500900050006",
    "type": "image",
    "image": {
        "link": "https://images.ecycle.com.br/wp-content/uploads/2021/05/20195924/o-que-e-paisagem.jpg"
    }
}

Don't forget...
Some web server does not permit access to files...

EuSousa
  • 113
  • 1
  • 1
  • 6
  • this was exactly my problem , i copied a link from wikipedia i was receiving the message, it worked when i used your link. – Vishal Poddar Nov 12 '22 at 14:03
  • https://graph.facebook.com/v17.0/111800652021143/messages I received success message.. but I don't receive any thing by whats app – lady Sep 02 '23 at 14:35
1

Media Messages To send a media message, make a POST call to /PHONE_NUMBER_ID/messages and attach a message object with type=image, document, audio, image, video, or sticker. Then, add a corresponding media object.

Sample request using image with link:

POST Send Image Message by URL To send a media message, make a POST call to /{{Phone-Number-ID}}/messages and attach a message object with type = image. Then, be sure to include the link to the image.

Send an audio message to your customers using a link to an image file.

Authorization : Bearer Token

BODY { "messaging_product": "whatsapp", "recipient_type": "individual", "to": "{{Recipient-Phone-Number}}", "type": "image", "image": { "link": "http(s)://image-url" } } example in PHP CURL <?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://graph.facebook.com/%7B%7BVersion%7D%7D/%7B%7BPhone-Number-ID%7D%7D/messages',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "{{Recipient-Phone-Number}}",
    "type": "image",
    "image": {
        "link": "http(s)://image-url"
    }
}',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer {{User-Access-Token}}',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

RESPONSE:

{
    "messaging_product": "whatsapp",
    "contacts": [
        {
            "input": "48XXXXXXXXX",
            "wa_id": "48XXXXXXXXX "
        }
    ],
    "messages": [
        {
            "id": "wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww"
        }
    ]
}

enter image description here

https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages#media-messages

apositivo
  • 343
  • 1
  • 9
0

I recommend you to download the WhatsApp Cloud API Postman collection from you Developer Dashboard for future doubts:

  1. Go to https://developers.facebook.com/apps/
  2. Select your WhatsApp application In left sidebar, go to WhatsApp -> Getting Started Click on the button "Run in Postman" to open a full API collection of samples

By the way I recommend you install the PHP SDK for WhatsApp Cloud API:

compose require netflie/whatsapp-cloud-api
jockos
  • 3
  • 4
0

@apositivo, when putting the above code on postmen, i got the wamid.#######. but I didn't receive the media message? any particular reason? or should i use the media object id?

{ "messaging_product": "whatsapp", "recipient_type": "individual", "to": "{{Recipient-Phone-Number}}", "type": "image", "image": { "link": "https://iprovider.pk/whatsappapi/1640191015925.jpg" } }

0

there are two ways to upload a media via link or an id.

To get an id or a link first you need to upload the media using the API which would return a media object with ID and a link.

I used the Whatsapp Ruby SDK for connecting with the API which looks like this:

uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
media = medias_api.media(media_id: uploaded_media.data&.id).data
Ignacio Chiazzo
  • 947
  • 11
  • 25