send-catalog-message.js•2.88 kB
/**
* Function to send a catalog message via WhatsApp.
*
* @param {Object} args - Arguments for sending the catalog message.
* @param {string} args.recipientPhoneNumber - The recipient's WhatsApp phone number.
* @param {string} args.version - The API version to use.
* @param {string} args.businessPhoneNumberId - The business phone number ID.
* @returns {Promise<Object>} - The response from the API after sending the message.
*/
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 messageData = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "interactive",
interactive: {
type: "catalog_message",
body: {
text: "Hello! Thanks for your interest. Ordering is easy. Just visit our catalog and add items to purchase."
},
action: {
name: "catalog_message",
parameters: {
thumbnail_product_retailer_id: "2lc20305pt"
}
},
footer: {
text: "Best grocery deals on WhatsApp!"
}
}
};
try {
const response = await fetch(`${baseUrl}/${version}/${businessPhoneNumberId}/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(messageData)
});
// Check if the response was successful
if (!response.ok) {
const errorData = await response.json();
throw new Error(JSON.stringify(errorData));
}
// Parse and return the response data
const data = await response.json();
return data;
} catch (error) {
console.error('Error sending catalog message:', error);
return {
error: `An error occurred while sending the catalog message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a catalog message via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_catalog_message',
description: 'Send a catalog message via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The recipient\'s WhatsApp phone number.'
},
version: {
type: 'string',
description: 'The API version to use.'
},
businessPhoneNumberId: {
type: 'string',
description: 'The business phone number ID.'
}
},
required: ['recipientPhoneNumber', 'version', 'businessPhoneNumberId']
}
}
}
};
export { apiTool };