1

I'm sending the PDF document to WhatsApp number, which is been successfully sent to the user but the title of the document is getting displayed as Untitled.

WhatsApp Business API version we are using is v2.21.6. I've referred to the documentation of WhatsApp Business API where it explains how one should send the document with ID or link. Documentation link http://developers.facebook.com/docs/whatsapp/api/messages/media

This is my file object to send the file to WhatsApp Number,

const fileDetails = {
        mime_type: 'application/pdf',
        id: uploadFile.media[0].id(media id),
        filename: fileName[3]( file name which we have to show)
      };

and below is helper function to send the message,

function getMessageByContentType(
  contentType,
  link,
  id = '',
  filename = '',
  caption = ''
) {
  const contentTypeMessageMapper = {
    audio: {
      type: 'audio',
      audio: {
        id,
      },
    },
    document: {
      type: 'document',
      document: {
        id,
        filename,
        caption,
      },
    },
    video: {
      type: 'video',
      video: {
        link,
      },
    },
    image: {
      type: 'image',
      image: {
        id,
        link,
      },
    },
  };

 //Method to send document to user WhatsApp mobile number
 yield whatsAppMessage.sendWhatsappMediaMessageToUser(
    bot,
    userId, //Mobile number
    fileDetails //File object.
  );
    //POST request
    {
  "method": "POST",
  "json": true,
  "headers": {
    "content-type": "application/json",
    "Authorization": "Auth Token"
  },
  "body": {
    "type": "document",
    "document": {
      "id": "a0706671-4fe7-49b0-8d1b-bcfb2fc5f7e8",
      "filename": "fileName.pdf",
      "caption": ""
    },
    "recipient_type": "individual",
    "to": "Mobile Number"
  },
  "uri": "http://WhatsApp-Business-API-URL/v1/messages",
  "rejectUnauthorized": false
}

After uploading binary file to WhatsApp Business API, we are using the ID to send the file to the customer which is getting uploaded successfully without any error message but the filename is coming as "Untitled" and not taking filename from the file object.

0

So finally I found out the answer to how to set the filename. Facebook documentation says, Caption is optional and needed to send it with image, you can send it with any attachment but with image it comes as an additional line with image. This is good as it can be an explanation for the image.

The file name is also an optional parameter which you can send it with the attachment. In my case, what I did is, I was sending the document and not an image so I removed the caption field and created the file object with the mandatory parameter and one optional parameter, the filename.

There is no clear indication that you have to use caption and filename together to set the filename for document type attachment.

So the final file object looks like following

const fileDetails = {
    mime_type: 'application/pdf',
    id: uploadFile.media[0].id(media id),
    filename: fileName[3]( file name which we have to show),
    caption: fileName[3]( file name caption),
  };

And your send message function will be like below,

    function sendWhatsappMediaMessageToUser(bot, mobileNumber, fileDetails) {
  const message = getMessageByContentType(
    fileDetails.mime_type,
    fileDetails.link,
    fileDetails.id,
    fileDetails.filename,
    filename.caption
  );
  message.recipient_type = 'individual';
  message.to = mobileNumber;
  // eslint-disable-next-line no-use-before-define
  return sendMediaMessageTOUserUsingBusinessApi(
    bot.config.api_host,
    bot.config.auth_token,
    message
  );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.