send-contact-message.js•3.97 kB
/**
* Function to send a contact message via WhatsApp.
*
* @param {Object} args - Arguments for sending the contact message.
* @param {string} args.RecipientPhoneNumber - The phone number of the recipient.
* @param {Object} args.contact - The contact details to be sent.
* @param {Array} args.contact.addresses - The addresses associated with the contact.
* @param {string} args.contact.birthday - The birthday of the contact.
* @param {Array} args.contact.emails - The emails associated with the contact.
* @param {Object} args.contact.name - The name details of the contact.
* @param {Object} args.contact.org - The organization details of the contact.
* @param {Array} args.contact.phones - The phone numbers associated with the contact.
* @param {Array} args.contact.urls - The URLs associated with the contact.
* @returns {Promise<Object>} - The result of the contact message sending operation.
*/
const executeFunction = async ({ RecipientPhoneNumber, contact }) => {
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 {
const url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const body = JSON.stringify({
messaging_product: "whatsapp",
to: RecipientPhoneNumber,
type: "contacts",
contacts: [contact]
});
const response = await fetch(url, {
method: 'POST',
headers,
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 contact message:', error);
return {
error: `An error occurred while sending the contact message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending contact messages via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_contact_message',
description: 'Send a contact message via WhatsApp.',
parameters: {
type: 'object',
properties: {
RecipientPhoneNumber: {
type: 'string',
description: 'The phone number of the recipient.'
},
contact: {
type: 'object',
description: 'The contact details to be sent.',
properties: {
addresses: {
type: 'array',
description: 'The addresses associated with the contact.'
},
birthday: {
type: 'string',
description: 'The birthday of the contact.'
},
emails: {
type: 'array',
description: 'The emails associated with the contact.'
},
name: {
type: 'object',
description: 'The name details of the contact.'
},
org: {
type: 'object',
description: 'The organization details of the contact.'
},
phones: {
type: 'array',
description: 'The phone numbers associated with the contact.'
},
urls: {
type: 'array',
description: 'The URLs associated with the contact.'
}
},
required: ['addresses', 'birthday', 'emails', 'name', 'org', 'phones', 'urls']
}
},
required: ['RecipientPhoneNumber', 'contact']
}
}
}
};
export { apiTool };