send-document-message-by-url.js•2.63 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.documentUrl - The URL of the document to send.
* @param {string} args.documentCaption - The caption for the document.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ recipientPhoneNumber, documentUrl, documentCaption }) => {
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: {
link: documentUrl,
caption: documentCaption
}
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
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 document messages 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.'
},
documentUrl: {
type: 'string',
description: 'The URL of the document to send.'
},
documentCaption: {
type: 'string',
description: 'The caption for the document.'
}
},
required: ['recipientPhoneNumber', 'documentUrl']
}
}
}
};
export { apiTool };