send-multi-product-message.js•4.63 kB
/**
* Function to send a multi-product message via WhatsApp.
*
* @param {Object} args - Arguments for sending the message.
* @param {string} args.recipientPhoneNumber - The WhatsApp ID or phone number of the recipient.
* @param {string} args.headerType - The type of the header for the message.
* @param {string} args.headerText - The text content of the header.
* @param {string} args.bodyText - The text content of the body.
* @param {string} args.footerText - The text content of the footer.
* @param {Array<Object>} args.sections - An array of sections containing product items.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ recipientPhoneNumber, headerType, headerText, bodyText, footerText, sections }) => {
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 {
// Construct the URL for the API request
const url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
// Prepare the message payload
const payload = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "interactive",
interactive: {
type: "product_list",
header: {
type: headerType,
text: headerText
},
body: {
text: bodyText
},
footer: {
text: footerText
},
action: {
catalog_id: "146265584024623",
sections: sections
}
}
};
// Set up headers for the request
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
// Perform the fetch request
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(payload)
});
// 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 multi-product message:', error);
return {
error: `An error occurred while sending the message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending multi-product messages via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_multi_product_message',
description: 'Send a multi-product message via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The WhatsApp ID or phone number of the recipient.'
},
headerType: {
type: 'string',
description: 'The type of the header for the message.'
},
headerText: {
type: 'string',
description: 'The text content of the header.'
},
bodyText: {
type: 'string',
description: 'The text content of the body.'
},
footerText: {
type: 'string',
description: 'The text content of the footer.'
},
sections: {
type: 'array',
items: {
type: 'object',
properties: {
title: {
type: 'string',
description: 'The title of the section.'
},
product_items: {
type: 'array',
items: {
type: 'object',
properties: {
product_retailer_id: {
type: 'string',
description: 'The SKU of the product in the catalog.'
}
},
required: ['product_retailer_id']
}
}
},
required: ['title', 'product_items']
},
description: 'An array of sections containing product items.'
}
},
required: ['recipientPhoneNumber', 'headerType', 'headerText', 'bodyText', 'footerText', 'sections']
}
}
}
};
export { apiTool };