send-text-message-with-preview-url.js•2.29 kB
/**
* Function to send a text message with a preview URL via WhatsApp.
*
* @param {Object} args - Arguments for sending the message.
* @param {string} args.RecipientPhoneNumber - The recipient's phone number.
* @param {string} args.body - The message body to send.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ RecipientPhoneNumber, body }) => {
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 headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const payload = {
messaging_product: "whatsapp",
to: RecipientPhoneNumber,
text: {
preview_url: true,
body: body
}
};
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(payload)
});
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 message:', error);
return {
error: `An error occurred while sending the message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a text message via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_text_message',
description: 'Send a text message with a preview URL via WhatsApp.',
parameters: {
type: 'object',
properties: {
RecipientPhoneNumber: {
type: 'string',
description: 'The recipient\'s phone number.'
},
body: {
type: 'string',
description: 'The message body to send.'
}
},
required: ['RecipientPhoneNumber', 'body']
}
}
}
};
export { apiTool };