send-catalog-template-message.js•3.03 kB
/**
* Function to send a catalog template message via WhatsApp.
*
* @param {Object} args - Arguments for sending the message.
* @param {string} args.recipientPhoneNumber - The phone number of the recipient.
* @param {string} args.version - The API version to use.
* @param {string} args.businessPhoneNumberId - The business phone number ID.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ recipientPhoneNumber, version, businessPhoneNumberId }) => {
const baseUrl = '<API_URL>'; // will be provided by the user
const token = process.env.BOTPE_MCP_API_KEY;
const url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
const payload = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "template",
template: {
name: "intro_catalog_offer",
language: {
code: "en_US"
},
components: [
{
type: "body",
parameters: [
{ type: "text", text: "100" },
{ type: "text", text: "400" },
{ type: "text", text: "3" }
]
},
{
type: "button",
sub_type: "CATALOG",
index: 0,
parameters: [
{
type: "action",
action: {
thumbnail_product_retailer_id: "2lc20305pt"
}
}
]
}
]
}
};
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 catalog template message:', error);
return {
error: `An error occurred while sending the message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending catalog template messages via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_catalog_template_message',
description: 'Send a catalog template message via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The phone number of the recipient.'
},
version: {
type: 'string',
description: 'The API version to use.'
},
businessPhoneNumberId: {
type: 'string',
description: 'The business phone number ID.'
}
},
required: ['recipientPhoneNumber', 'version', 'businessPhoneNumberId']
}
}
}
};
export { apiTool };