send-message-template-text.js•5.65 kB
/**
* Function to send a message template via WhatsApp.
*
* @param {Object} args - Arguments for sending the message.
* @param {string} args.recipientPhoneNumber - The phone number of the recipient.
* @param {string} args.templateName - The name of the template to use.
* @param {string} args.languageCode - The language and locale code for the template.
* @param {string} args.text - The text string to include in the message.
* @param {Object} args.currency - The currency details for the message.
* @param {Object} args.dateTime - The date and time details for the message.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ recipientPhoneNumber, templateName, languageCode, text, currency, dateTime }) => {
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 url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
const body = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "template",
template: {
name: templateName,
language: {
code: languageCode
},
components: [
{
type: "body",
parameters: [
{
type: "text",
text: text
},
{
type: "currency",
currency: {
fallback_value: currency.fallbackValue,
code: currency.code,
amount_1000: currency.amount1000
}
},
{
type: "date_time",
date_time: {
fallback_value: dateTime.fallbackValue,
day_of_week: dateTime.dayOfWeek,
year: dateTime.year,
month: dateTime.month,
day_of_month: dateTime.dayOfMonth,
hour: dateTime.hour,
minute: dateTime.minute,
calendar: "GREGORIAN"
}
}
]
}
]
}
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(body)
});
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 message template:', error);
return {
error: `An error occurred while sending the message template: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a message template via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_message_template',
description: 'Send a message template via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The phone number of the recipient.'
},
templateName: {
type: 'string',
description: 'The name of the template to use.'
},
languageCode: {
type: 'string',
description: 'The language and locale code for the template.'
},
text: {
type: 'string',
description: 'The text string to include in the message.'
},
currency: {
type: 'object',
properties: {
fallbackValue: {
type: 'string',
description: 'The fallback value for the currency.'
},
code: {
type: 'string',
description: 'The currency code.'
},
amount1000: {
type: 'integer',
description: 'The amount in the smallest unit (e.g., cents).'
}
},
required: ['fallbackValue', 'code', 'amount1000']
},
dateTime: {
type: 'object',
properties: {
fallbackValue: {
type: 'string',
description: 'The fallback value for the date and time.'
},
dayOfWeek: {
type: 'integer',
description: 'The day of the week (0-6).'
},
year: {
type: 'integer',
description: 'The year.'
},
month: {
type: 'integer',
description: 'The month (1-12).'
},
dayOfMonth: {
type: 'integer',
description: 'The day of the month (1-31).'
},
hour: {
type: 'integer',
description: 'The hour (0-23).'
},
minute: {
type: 'integer',
description: 'The minute (0-59).'
}
},
required: ['fallbackValue', 'dayOfWeek', 'year', 'month', 'dayOfMonth', 'hour', 'minute']
}
},
required: ['recipientPhoneNumber', 'templateName', 'languageCode', 'text', 'currency', 'dateTime']
}
}
}
};
export { apiTool };