send-document-message-by-id.js•2.83 kB
/**
* Function to send a document message via WhatsApp API.
*
* @param {Object} args - Arguments for sending the document message.
* @param {string} args.recipientPhoneNumber - The phone number of the recipient.
* @param {string} args.documentId - The ID of the document to send.
* @param {string} args.caption - The caption for the document.
* @param {string} args.filename - The filename of the document.
* @returns {Promise<Object>} - The result of the document message send operation.
*/
const executeFunction = async ({ recipientPhoneNumber, documentId, caption, filename }) => {
const baseUrl = '<API_URL>'; // will be provided by the user
const token = process.env.BOTPE_MCP_API_KEY;
const version = '<Version>'; // will be provided by the user
const businessPhoneNumberId = '<PHONE_NUMBER_ID>'; // will be provided by the user
try {
const url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
const body = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "document",
document: {
id: documentId,
caption: caption,
filename: filename
}
};
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(body)
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(JSON.stringify(errorData));
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error sending document message:', error);
return {
error: `An error occurred while sending the document message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a document message via WhatsApp API.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_document_message',
description: 'Send a document message via WhatsApp API.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The phone number of the recipient.'
},
documentId: {
type: 'string',
description: 'The ID of the document to send.'
},
caption: {
type: 'string',
description: 'The caption for the document.'
},
filename: {
type: 'string',
description: 'The filename of the document.'
}
},
required: ['recipientPhoneNumber', 'documentId']
}
}
}
};
export { apiTool };