send-list-message.js•4.52 kB
/**
* Function to send a list message via WhatsApp API.
*
* @param {Object} args - Arguments for sending the message.
* @param {string} args.recipientPhoneNumber - The recipient's phone number.
* @param {string} args.headerText - The header text for the list message.
* @param {string} args.bodyText - The body text for the list message.
* @param {string} args.footerText - The footer text for the list message.
* @param {string} args.buttonText - The button text for the list message.
* @param {Array} args.sections - The sections of the list message, each containing title and rows.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ recipientPhoneNumber, headerText, bodyText, footerText, buttonText, 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
const messageData = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "interactive",
interactive: {
type: "list",
header: {
type: "text",
text: headerText
},
body: {
text: bodyText
},
footer: {
text: footerText
},
action: {
button: buttonText,
sections: sections
}
}
};
try {
const response = await fetch(`${baseUrl}/${version}/${businessPhoneNumberId}/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
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 list message:', error);
return {
error: `An error occurred while sending the list message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a list message via WhatsApp API.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_list_message',
description: 'Send a list message via WhatsApp API.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The recipient\'s phone number.'
},
headerText: {
type: 'string',
description: 'The header text for the list message.'
},
bodyText: {
type: 'string',
description: 'The body text for the list message.'
},
footerText: {
type: 'string',
description: 'The footer text for the list message.'
},
buttonText: {
type: 'string',
description: 'The button text for the list message.'
},
sections: {
type: 'array',
items: {
type: 'object',
properties: {
title: {
type: 'string',
description: 'The title of the section.'
},
rows: {
type: 'array',
items: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'The ID of the row.'
},
title: {
type: 'string',
description: 'The title of the row.'
},
description: {
type: 'string',
description: 'The description of the row.'
}
},
required: ['id', 'title']
}
}
},
required: ['title', 'rows']
},
description: 'The sections of the list message.'
}
},
required: ['recipientPhoneNumber', 'headerText', 'bodyText', 'footerText', 'buttonText', 'sections']
}
}
}
};
export { apiTool };