使用WhatsApp Cloud API发送图像

发布于 2025-02-03 00:44:43 字数 618 浏览 4 评论 0原文

我正在尝试使用 whatsapp cloud cloud api api < /a>。使用PHP,我能够成功发送普通短信。

通过文档,' Media_object_id '是什么意思?一个例子很棒。

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"
  }
}'

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

软糯酥胸 2025-02-10 00:44:43

首先,您需要将媒体文件上传到WhatsApp服务器,然后WhatsApp现在将响应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

,您将获得媒体对象ID。您需要从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);

是不同类型的媒体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)

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)
一城柳絮吹成雪 2025-02-10 00:44:43

您需要将媒体上传到 https://graph.face.face.com/v13.03.03.03.03.03.0 /from_phone_number_id/媒体

响应将为您提供“ Media_object_id”

使用映像链接

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"
  }
}'

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"
  }
}'
自由如风 2025-02-10 00:44:43

媒体消息
要发送媒体消息,请向/phone_number_id/sagess发送帖子,然后使用 type = image,文档,音频,图像,视频或贴纸附加消息对象。然后,添加一个相应的媒体对象。

使用图像与链接的示例请求:

发布
url 发送图像消息
要发送媒体消息,请向 /{{{phone-number-id}} /消息进行帖子调用,然后将消息对象连接到type = image。然后,请确保包括图像的链接。

使用指向图像文件的链接向客户发送音频消息。

授权:承载令牌的

身体
{
“ Messaging_product”:“ WhatsApp”,
“ conferient_type”:“个人”,
“ to”:“ {{{cocceent-phone-number}}”,
“类型”:“图像”,
“图像”: {
“链接”:“ http(s):// image-url”
}
}
php卷曲的示例
&lt;?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;

响应:

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

”在此处输入图像描述”

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

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

别想她 2025-02-10 00:44:43

使用Postman

URL:

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

对象:

{
    "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"
    }
}

不要忘记...
一些Web服务器不允许访问文件...

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

仙气飘飘 2025-02-10 00:44:43

我建议您从您的开发人员dashboard下载WhatsApp Cloud API Postman Collection以后的疑问:

  1. 转到 https:// https:// https:// developers.facebook.com/apps/
  2. 选择您的WhatsApp应用程序
    在左侧栏中,转到WhatsApp -&gt;入门
    单击按钮“在Postman中运行”以打开完整的API示例集合

,以便您建议您安装 php sdk for whatsapp cloud api:

compose require netflie/whatsapp-cloud-api

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
揽月 2025-02-10 00:44:43

@apositivo,将上述代码放在邮递员上时,我得到了wamid。#######。但是我没有收到媒体消息?有什么特别的原因吗?还是我应该使用媒体对象ID?

{
“ Messaging_product”:“ WhatsApp”,
“ conferient_type”:“个人”,
“ to”:“ {{{cocceent-phone-number}}”,
“类型”:“图像”,
“图像”: {
“链接”:“ https://iprovider.pk/whatsappapi/1640191015925.jpg”
}
}

@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"
}
}

时光是把杀猪刀 2025-02-10 00:44:43

有两种方法可以通过链接ID上传媒体。

要获取ID链接首先,您需要使用API​​上传媒体,该API将返回媒体对象,并使用ID 链接。

我使用了 whatsapp ruby​​ sdk 与API连接,看起来像这样:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文