send-single-product-message.js•3.5 kB
/**
* Function to send a single product message via WhatsApp.
*
* @param {Object} args - Arguments for sending the product message.
* @param {string} args.recipientPhoneNumber - The WhatsApp ID or phone number of the recipient.
* @param {string} [args.optionalBodyText] - Optional body text for the message.
* @param {string} [args.optionalFooterText] - Optional footer text for the message.
* @param {string} args.catalogId - The catalog ID associated with the product.
* @param {string} args.productRetailerId - The retailer ID of the product to send.
* @returns {Promise<Object>} - The response from the API after sending the message.
*/
const executeFunction = async ({ recipientPhoneNumber, optionalBodyText, optionalFooterText, catalogId, productRetailerId }) => {
const baseUrl = '<API_URL>'; // will be provided by the user
const version = '<Version>'; // will be provided by the user
const token = process.env.BOTPE_MCP_API_KEY;
const businessPhoneNumberId = '<PHONE_NUMBER_ID>'; // will be provided by the user
try {
const url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
const messageData = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "interactive",
interactive: {
type: "product",
body: {
text: optionalBodyText || "<OPTIONAL_BODY_TEXT>"
},
footer: {
text: optionalFooterText || "<OPTIONAL_FOOTER_TEXT>"
},
action: {
catalog_id: catalogId,
product_retailer_id: productRetailerId
}
}
};
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(messageData)
});
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 product message:', error);
return {
error: `An error occurred while sending the product message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a single product message via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_single_product_message',
description: 'Send a single product message via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The WhatsApp ID or phone number of the recipient.'
},
optionalBodyText: {
type: 'string',
description: 'Optional body text for the message.'
},
optionalFooterText: {
type: 'string',
description: 'Optional footer text for the message.'
},
catalogId: {
type: 'string',
description: 'The catalog ID associated with the product.'
},
productRetailerId: {
type: 'string',
description: 'The retailer ID of the product to send.'
}
},
required: ['recipientPhoneNumber', 'catalogId', 'productRetailerId']
}
}
}
};
export { apiTool };