简体   繁体   English

发送文档附件时如何在 WhatsApp Business API 中设置文件名参数?

[英]How to set filename parameter in WhatsApp Business API while sending document attachment?

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.我正在将 PDF 文档发送到 WhatsApp 号码,该号码已成功发送给用户,但文档的标题显示为无标题。

WhatsApp Business API version we are using is v2.21.6.我们使用的 WhatsApp Business API 版本是 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.我参考了 WhatsApp Business API 的文档,其中解释了如何发送带有 ID 或链接的文档。 Documentation link https://developers.facebook.com/docs/whatsapp/api/messages/media文档链接https://developers.facebook.com/docs/whatsapp/api/messages/media

This is my file object to send the file to WhatsApp Number,这是我的文件 object 发送文件到 WhatsApp 号码,

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,

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": "https://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.将二进制文件上传到 WhatsApp Business API 后,我们使用 ID 将文件发送给客户,该文件已成功上传,没有任何错误消息,但文件名显示为“无标题”,并且没有从文件 object 中获取文件名。

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. Facebook 文档说,标题是可选的,需要与图像一起发送,您可以将其与任何附件一起发送,但与图像一起作为图像的附加行。 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.就我而言,我所做的是,我发送的是文档而不是图像,因此我删除了标题字段并创建了文件 object,其中包含强制参数和一个可选参数,即文件名。

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所以最终文件 object 如下所示

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 将如下所示,

    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
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们通过 WhatsApp 发送图像时,如何在 WhatsApp Business Cloud API 中获取图像的 URL - How to get the URL of an image in WhatsApp Business Cloud API, while we send an image through WhatsApp 从NodeJS发送附件到Skype for Business - Sending attachment to Skype for Business from NodeJS 通过代码将媒体参数发送到kaleyra api时发送媒体参数不起作用 - Sending media parameter is not working while sending it to kaleyra api through code Apple Business Chat REST API,使用NodeJS下载和解密附件 - Apple Business Chat REST API, Downloading and Decrypting Attachment with NodeJS 如何从 Facebook 业务经理激活自动 Whatsapp 模板? - How to activate automatic Whatsapp template from Facebook business manager? 使用 Filereader API 时如何设置下载的文件名? - How can I set the downloaded filename when using Filereader API? 发送消息时出错(whatsapp-web.js) - Error while sending message (whatsapp-web.js) 如何在循环中“等待”来自异步 function 的承诺/发送带有邮戳 API 附件的电子邮件 - How to “await” promises from a asynchronous function in a loop / Sending E-Mails with attachment over the Postmark API 流中的mailgun附件。 如何申请文件名 - mailgun attachment from stream. How to apply filename 如何使用 Twilio Whatsapp API 保存状态 - how to preserve state using Twilio Whatsapp API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM