Ad

What Is The Correct Way To Upload File As Binary Data In Http Post Request Of Whatspp Business Api?

- 1 answer

I am trying to create a capability to send invoices to our customers over whatsapp as pdf. For this I already have a invoice pdf which needs to be uploaded over whatsapp server to be sent out to our customers as per the document : https://developers.facebook.com/docs/whatsapp/api/media/

Since it expects a binary-media data, I am creating a http post request body as below :

    let requestOptions = {
      method: method,
      url: "https://whatsapp-url" + '/v1/upload',
      body: Buffer.from(fs.readFileSync("path/test.pdf")),
      json: true,
      headers: {
        authorization: bearerToken,
        'content-type' : 'application/pdf'
      }
    };

As a response to this I do receive media-id from Whatsapp in the response body which I use to send message to the customer as per the document: https://developers.facebook.com/docs/whatsapp/api/messages

But alas !! the pdf file received is corrupted and does not open, Moreover I also notice that pdf sent by me is of much lessor size as compared to one I receive on the customer end over Whatsapp message.

Can anyone help out in exactly what I might be doing wrong in creating request body for upload as I suspect there lies all my problem.

Ad

Answer

There are two ways to send the file to the user via a link or by an ID. Link can be your own WhatsApp server link or can be an external link with provider name as a param.

If you have the file with you then you can upload the file to your WhatsApp business API server and get media ID in return. Use that media id to create a link or simply use that media ID to send the file to the user.

Following is code sample to create the binary file

 const dataStream = fs.readFileSync(
    `Your Path to file/File Name`,
    null
  );

  let uploadFile = yield whatsAppMessage.uploadWhatsappMediaToUser(
    bot, // We are using Bot kit to process message
    'application/pdf', // mime type
    dataStream, // Binary Data Stream to upload the file
    file name //File name
  );

    function uploadWhatsappMediaToUser(bot, contentType, binaryData, filename) {
  const options = {
    method: 'POST',
    uri: `Upload URL`,
    headers: {
      Authorization: `Bearer  {auth_token}`,
      'Content-Type': contentType,
      'Content-Disposition': `attachment; filename=${filename}`,
    },
    rejectUnauthorized: false,
    body: binaryData,
  };

  return rp(options);
}

In uploadFile variable, you'll receive the json object which you need to parse to extract media ID. Use that media ID to send the file.

Following is the code for sending the file as an attachment in the message

const fileDetails = {
    mime_type: 'application/pdf', // MIME type
    id: uploadFile.media[0].id, // Media ID
    filename: fileName,
  };
  yield whatsAppMessage.sendWhatsappMediaMessageToUser(
    bot, // Bot kit object
    userId, //User mobile number
    fileDetails // Above File object
  );

This way you'll be able to send the file to the user.

Ad
source: stackoverflow.com
Ad