send-reply-to-contact-message.js•5.37 kB
/**
* Function to send a reply to a contact message via WhatsApp.
*
* @param {Object} args - Arguments for sending the message.
* @param {string} args.RecipientPhoneNumber - The phone number of the recipient.
* @param {string} args.MessageId - The message ID of the previous message.
* @param {Array} args.Contacts - An array of contact objects to send.
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ RecipientPhoneNumber, MessageId, Contacts }) => {
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`;
// Create the message payload
const payload = {
messaging_product: "whatsapp",
to: RecipientPhoneNumber,
context: {
message_id: MessageId
},
type: "contacts",
contacts: Contacts
};
// 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 message:', error);
return {
error: `An error occurred while sending the message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a reply to a contact message via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_reply_to_contact_message',
description: 'Send a reply to a contact message via WhatsApp.',
parameters: {
type: 'object',
properties: {
RecipientPhoneNumber: {
type: 'string',
description: 'The phone number of the recipient.'
},
MessageId: {
type: 'string',
description: 'The message ID of the previous message.'
},
Contacts: {
type: 'array',
items: {
type: 'object',
properties: {
addresses: {
type: 'array',
items: {
type: 'object',
properties: {
street: { type: 'string' },
city: { type: 'string' },
state: { type: 'string' },
zip: { type: 'string' },
country: { type: 'string' },
country_code: { type: 'string' },
type: { type: 'string', enum: ['HOME', 'WORK'] }
}
}
},
birthday: { type: 'string' },
emails: {
type: 'array',
items: {
type: 'object',
properties: {
email: { type: 'string' },
type: { type: 'string', enum: ['WORK', 'HOME'] }
}
}
},
name: {
type: 'object',
properties: {
formatted_name: { type: 'string' },
first_name: { type: 'string' },
last_name: { type: 'string' },
middle_name: { type: 'string' },
suffix: { type: 'string' },
prefix: { type: 'string' }
}
},
org: {
type: 'object',
properties: {
company: { type: 'string' },
department: { type: 'string' },
title: { type: 'string' }
}
},
phones: {
type: 'array',
items: {
type: 'object',
properties: {
phone: { type: 'string' },
wa_id: { type: 'string' },
type: { type: 'string', enum: ['HOME', 'WORK'] }
}
}
},
urls: {
type: 'array',
items: {
type: 'object',
properties: {
url: { type: 'string' },
type: { type: 'string', enum: ['HOME', 'WORK'] }
}
}
}
}
},
description: 'An array of contact objects to send.'
}
},
required: ['RecipientPhoneNumber', 'MessageId', 'Contacts']
}
}
}
};
export { apiTool };