{"id":2313,"date":"2022-08-28T10:54:38","date_gmt":"2022-08-28T10:54:38","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?page_id=2313"},"modified":"2023-04-14T08:02:58","modified_gmt":"2023-04-14T08:02:58","slug":"whatsapp-business-api","status":"publish","type":"page","link":"https:\/\/codeinsightacademy.com\/blog\/programming\/whatsapp-business-api\/","title":{"rendered":"WhatsApp Business API"},"content":{"rendered":"\n<p>Message template to send media file<\/p>\n\n\n\n<p>NOTE: DO NOT FORGET TO Add sample WHILE CREATING MEDIA TEMPLATE<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2022\/08\/image-8.png\"><img loading=\"lazy\" width=\"1345\" height=\"766\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2022\/08\/image-8.png\" alt=\"\" class=\"wp-image-2314\"\/><\/a><\/figure>\n\n\n\n<p>Upload media<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n$file=\"\/tmp\/test.jpg\";\n$mime = mime_content_type($file);\n$info = pathinfo($file);\n$name = $info&#91;'basename'];\n$file = new CURLFile($file, $mime, $name);\n\n $file->setMimeType($mime);\n $curl = curl_init();\n curl_setopt_array($curl, array(\n    CURLOPT_URL => \"https:\/\/graph.facebook.com\/{{Version}}\/{{Phone-Number-ID}}\/media\",\n    CURLOPT_RETURNTRANSFER => true,\n    CURLOPT_ENCODING => \"\",\n    CURLOPT_MAXREDIRS => 10,\n    CURLOPT_TIMEOUT => 0,\n    CURLOPT_FOLLOWLOCATION => true,\n    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,\n    CURLOPT_CUSTOMREQUEST => \"POST\",\n    CURLOPT_POSTFIELDS => array(\"messaging_product\" => \"whatsapp\", \"type\"=>$mime, \"file\"=> $file),\n    CURLOPT_HTTPHEADER => array(\n         \"Authorization: Bearer &lt;YOUR TOKEN GOES HERE>\"\n     ),\n));\n$resultWhatsAppMedia = json_decode(curl_exec($curl), true);\n$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);\n\n$MEDIA_OBJECT_ID = $resultWhatsAppMedia&#91;'id']; \/\/MEDIA OBJECT ID\n\necho $MEDIA_OBJECT_ID;\n<\/code><\/pre>\n\n\n\n<p>Payload to send media file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"messaging_product\": \"whatsapp\",\n    \"to\": \"91XXXXXXXXXX\",\n    \"type\": \"template\",\n    \"template\": {\n        \"name\": \"course_update\",\n        \"language\": {\n            \"code\": \"en_US\"\n        },\n         \"components\": &#91;{\n                \"type\": \"header\",\n                \"parameters\": &#91;\n                    {\n                        \"type\": \"image\",\n                        \"image\": {\n                          \"id\": \"XXXXXXXXXXXXXXXXXXX\"\n                        }\n                    }\n                ]\n            },{\n            \"type\": \"body\",\n            \"parameters\": &#91;\n                {\"type\": \"text\", \"text\": \"Shailesh\"}\n            ]\n      }]\n    }\n}\n\nYour message will look like this<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2022\/08\/image-9.png\"><img loading=\"lazy\" width=\"383\" height=\"321\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2022\/08\/image-9.png\" alt=\"\" class=\"wp-image-2326\"\/><\/a><\/figure>\n\n\n\n<p>cURL call while sending message<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -i -X POST https:\/\/graph.facebook.com\/{{Version}}\/{{Phone-Number-ID}}\/messages -H 'Authorization: Bearer <strong>&lt;YOUR TOKEN COMES HERE><\/strong>' -H 'Content-Type: application\/json' -d '{ \"messaging_product\": \"whatsapp\", \"to\": \"91XXXXXXXXXX\", \"type\": \"template\", \"template\": { \"name\": \"hello_world\", \"language\": { \"code\": \"en_US\" } } }'<\/code><\/pre>\n\n\n\n<p>Payload to send message with parameters<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"messaging_product\": \"whatsapp\",\n    \"to\": \"91XXXXXXXXX\",\n    \"type\": \"template\",\n    \"template\": {\n        \"name\": \"full_payment\",\n        \"language\": {\n            \"code\": \"en_US\"\n        },\n        \"components\": &#91;{\n            \"type\": \"body\",\n            \"parameters\": &#91;\n                {\"type\": \"text\", \"text\": \"Shailesh\"},\n                {\"type\": \"text\", \"text\": \"5000\"},\n                {\"type\": \"text\", \"text\": \"https:\/\/urlgoeshere?id=12345\"}\n            ]\n      }]\n    }<\/code><\/pre>\n\n\n\n<p>cURL call with parameters<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -i -X POST https:\/\/graph.facebook.com\/{{Version}}\/{{Phone-Number-ID}}\/messages -H 'Authorization: Bearer <strong>&lt;YOUR TOKEN COMES HERE><\/strong>' -H 'Content-Type: application\/json' -d '{ \"messaging_product\": \"whatsapp\", \"to\": \"91XXXXXXXXXX\", \"type\": \"template\", \"template\": { \"name\": \"<strong>template_name<\/strong>\", \"language\": { \"code\": \"en_US\" }, \"components\": &#91;{ \"type\": \"body\", \"parameters\": &#91;{\"type\": \"text\", \"text\": \"<strong>&lt;STRING VALUE><\/strong>\"},{\"type\": \"text\", \"text\": \"<strong>\u20b9 7000<\/strong>\"}]}] } }'\n\n<\/code><\/pre>\n\n\n\n<p>Manage Phone Numbers<br><a href=\"https:\/\/business.facebook.com\/wa\/manage\/phone-numbers\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/business.facebook.com\/wa\/manage\/phone-numbers\/<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Reference Links:<br><a href=\"https:\/\/developers.facebook.com\/docs\/whatsapp\/cloud-api\/get-started\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/developers.facebook.com\/docs\/whatsapp\/cloud-api\/get-started\/<\/a><br><br><a rel=\"noreferrer noopener\" href=\"https:\/\/developers.facebook.com\/docs\/whatsapp\/api\/messages\/message-templates\/media-message-templates\/\" target=\"_blank\">https:\/\/developers.facebook.com\/docs\/whatsapp\/api\/messages\/message-templates\/media-message-templates\/<\/a><br><br><a rel=\"noreferrer noopener\" href=\"https:\/\/developers.facebook.com\/docs\/whatsapp\/message-templates\/guidelines\/\" target=\"_blank\">https:\/\/developers.facebook.com\/docs\/whatsapp\/message-templates\/guidelines\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Message template to send media file NOTE: DO NOT FORGET TO Add sample WHILE CREATING MEDIA TEMPLATE Upload media Payload to send media file cURL call while sending message Payload to send message with parameters cURL call with parameters Manage Phone Numbershttps:\/\/business.facebook.com\/wa\/manage\/phone-numbers\/ Reference Links:https:\/\/developers.facebook.com\/docs\/whatsapp\/cloud-api\/get-started\/ https:\/\/developers.facebook.com\/docs\/whatsapp\/api\/messages\/message-templates\/media-message-templates\/ https:\/\/developers.facebook.com\/docs\/whatsapp\/message-templates\/guidelines\/<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":871,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/2313"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2313"}],"version-history":[{"count":9,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/2313\/revisions"}],"predecessor-version":[{"id":2330,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/2313\/revisions\/2330"}],"up":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/871"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}