send-message-template-interactive.js•3.73 kB
/**
* Function to send a message template via WhatsApp.
*
* @param {Object} args - Arguments for sending the message.
* @param {string} args.recipientPhoneNumber - The recipient's phone number.
* @param {string} args.templateName - The name of the template to be used.
* @param {string} args.languageCode - The language and locale code for the template.
* @param {Array} args.headerParameters - Parameters for the header component.
* @param {Array} args.bodyParameters - Parameters for the body component.
* @param {Array} args.buttonParameters - Parameters for the button component.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ recipientPhoneNumber, templateName, languageCode, headerParameters, bodyParameters, buttonParameters }) => {
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
const url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
const payload = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "template",
template: {
name: templateName,
language: {
code: languageCode
},
components: [
{
type: "header",
parameters: headerParameters
},
{
type: "body",
parameters: bodyParameters
},
...buttonParameters.map((param, index) => ({
type: "button",
sub_type: "quick_reply",
index: index.toString(),
parameters: [param]
}))
]
}
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
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 template:', error);
return {
error: `An error occurred while sending the message template: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a message template via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_message_template',
description: 'Send a message template via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The recipient\'s phone number.'
},
templateName: {
type: 'string',
description: 'The name of the template to be used.'
},
languageCode: {
type: 'string',
description: 'The language and locale code for the template.'
},
headerParameters: {
type: 'array',
description: 'Parameters for the header component.'
},
bodyParameters: {
type: 'array',
description: 'Parameters for the body component.'
},
buttonParameters: {
type: 'array',
description: 'Parameters for the button component.'
}
},
required: ['recipientPhoneNumber', 'templateName', 'languageCode']
}
}
}
};
export { apiTool };