[英]Send Images using Whatsapp Cloud Api
我正在嘗試使用Whatsapp Cloud API發送圖像。 使用 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"
}
}'
謝謝
您需要將媒體上傳到https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/media
響應將為您提供“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"
}
}'
首先,您需要將媒體文件上傳到 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)
我建議您從 Developer Dashboard 下載 WhatsApp Cloud API Postman 集合,以備將來疑惑:
順便說一句,我建議您為 WhatsApp Cloud API 安裝PHP SDK :
compose require netflie/whatsapp-cloud-api
媒體消息要發送媒體消息,請對 /PHONE_NUMBER_ID/messages 進行 POST 調用,並附加一個帶有type=image、文檔、音頻、圖像、視頻或貼紙的消息對象。 然后,添加相應的媒體對象。
使用帶有鏈接的圖像的示例請求:
POST通過 URL 發送圖像消息要發送媒體消息,請對 /{{Phone-Number-ID}}/messages 進行 POST 調用,並附加一個 type = image 的消息對象。 然后,請務必包含指向圖像的鏈接。
使用指向圖像文件的鏈接向您的客戶發送音頻消息。
授權:承載令牌
BODY {“messaging_product”:“whatsapp”,“recipient_type”:“個人”,“to”:“{{Recipient-Phone-Number}}”,“type”:“image”,“image”:{“link” : "http(s)://image-url" } } 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;
回復:
{
"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
@apositivo,將上述代碼放在郵遞員身上時,我得到了 wamid.#######。 但我沒有收到媒體消息? 有什么特別的原因嗎? 還是我應該使用媒體對象 ID?
{“messaging_product”:“whatsapp”,“recipient_type”:“個人”,“to”:“{{Recipient-Phone-Number}}”,“type”:“image”,“image”:{“link”: “https://iprovider.pk/whatsappapi/1640191015925.jpg”}}
有兩種方法可以通過link
或id
上傳媒體。
要首先獲取id
或link
,您需要使用 API 上傳媒體,該 API 將返回帶有ID
和link
的媒體對象。
我使用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
使用郵遞員的示例
網址:
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"
}
}
別忘了...
一些網絡服務器不允許訪問文件...😉
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.