如何在发送文档附件时在WhatsApp Business API中设置文件名参数?
我正在将PDF文档发送到WhatsApp号,该号码已成功发送给用户,但是文档标题显示为“无标题”。
我们正在使用的WhatsApp Business API版本是v2.21.6。我参考了WhatsApp Business API的文档,其中解释了如何发送带有ID或链接的文档。文档链接
这是我的文件对象,用于将文件发送到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)
};
和下面是发送消息的助手功能,
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
}
将二进制文件上传到WhatsApp Business API之后,我们使用ID将文件发送给客户,该客户已成功上传,没有任何错误消息,但文件名以“ Untitled”的形式出现,并且没有从文件对象中获取文件名。
回答如下:所以最后我找到了如何设置文件名的答案。 Facebook文档说,标题是可选的,需要随图像一起发送,您可以随附件一起发送,但随图像一起发送,附带图像。这很好,因为它可以作为图像的解释。
文件名也是可选参数,您可以将其与附件一起发送。就我而言,我要做的是发送文档而不是图像,因此我删除了标题字段,并创建了带有强制参数和一个可选参数文件名的文件对象。
没有明确的指示,您必须同时使用标题和文件名来设置文档类型附件的文件名。
因此最终文件对象如下所示
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),
};
如何在发送文档附件时在WhatsApp Business API中设置文件名参数?
我正在将PDF文档发送到WhatsApp号,该号码已成功发送给用户,但是文档标题显示为“无标题”。
我们正在使用的WhatsApp Business API版本是v2.21.6。我参考了WhatsApp Business API的文档,其中解释了如何发送带有ID或链接的文档。文档链接
这是我的文件对象,用于将文件发送到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)
};
和下面是发送消息的助手功能,
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
}
将二进制文件上传到WhatsApp Business API之后,我们使用ID将文件发送给客户,该客户已成功上传,没有任何错误消息,但文件名以“ Untitled”的形式出现,并且没有从文件对象中获取文件名。
回答如下:所以最后我找到了如何设置文件名的答案。 Facebook文档说,标题是可选的,需要随图像一起发送,您可以随附件一起发送,但随图像一起发送,附带图像。这很好,因为它可以作为图像的解释。
文件名也是可选参数,您可以将其与附件一起发送。就我而言,我要做的是发送文档而不是图像,因此我删除了标题字段,并创建了带有强制参数和一个可选参数文件名的文件对象。
没有明确的指示,您必须同时使用标题和文件名来设置文档类型附件的文件名。
因此最终文件对象如下所示
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),
};