How to download media files in WhatsApp API which send users?

Viewed 2005

I am currently writing a chatbot for WhatsApp. I use the 360dialog platform, which makes it possible to work with the WhatsApp Business API.

When the client sends a message, I see the following JSON object in the logs of my application:

{
    "messages": [
        {
            "from": "77773336633",
            "id": "ABGGd3c1cGY_Ago61ytHsZknvtLv",
            "image": {
                "id": "ffd23134-2dae-4fed-b5f8-0ce7867b6ddd",
                "mime_type": "image/jpeg",
                "sha256": "bd02100d961b5a1dbaae1dd645485ebbfeda77b44e82c015f1cf29b05654ccb9"
            },
            "timestamp": "1605703542",
            "type": "image"
        }
    ],
    "contacts": [
        {
            "profile": {
                "name": "Nurzhan Nogerbek"
            },
            "wa_id": "77773336633"
        }
    ]
}

I can't find any information in the documentation about how to download this file. In my case, I want to upload this image file that the client sends to my file storage. Please tell me which URL method from the WhatsApp API is responsible for this mechanism?

P.S. At the same time, I can send files to clients. This information is available on the official documentation.

2 Answers

The official documentation has a session for this in https://developers.facebook.com/docs/whatsapp/api/media.

Basically you must make a GET request to download the media.

curl --request GET \
  --url https://waba.360dialog.io/v1/media/{media id} \
  --header 'D360-API-KEY: {your api token}'

May be it will help, just try, take a look:-

const URL = `https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=1104480873777230&ext=1662705250&hash=ATuMx352sLrhKUegbQZSC8oLl3J5Vy3Z49lO4HwTUKWRYQ`;
const FROM = `video`;

const config = {
  method: 'get',
  url: URL, //PASS THE URL HERE, WHICH YOU RECEIVED WITH THE HELP OF MEDIA ID
  headers: {
    'Authorization': `Bearer ${token}`
  },
  responseType: 'arraybuffer'
};
axios(config)
  .then(function (response) {
    const ext = response.headers['content-type'].split("/")[1];
    fs.writeFileSync(`${FROM}.${ext}`, response.data);
  })
  .catch(function (error) {
    console.log(error);
  });
Related