send-reply-with-reaction-message.js•2.97 kB
/**
* Function to send a reply with a reaction message via WhatsApp API.
*
* @param {Object} args - Arguments for sending the reaction message.
* @param {string} args.recipientPhoneNumber - The WhatsApp ID or phone number of the recipient.
* @param {string} args.messageId - The ID of the message to react to.
* @param {string} args.emoji - The emoji to use for the reaction.
* @returns {Promise<Object>} - The response from the API after sending the reaction message.
*/
const executeFunction = async ({ recipientPhoneNumber, messageId, emoji }) => {
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 payload for the reaction message
const payload = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "reaction",
reaction: {
message_id: messageId,
emoji: emoji
}
};
// 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: 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 reaction message:', error);
return {
error: `An error occurred while sending the reaction message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a reply with a reaction message via WhatsApp API.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_reaction_message',
description: 'Send a reply with a reaction message via WhatsApp API.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The WhatsApp ID or phone number of the recipient.'
},
messageId: {
type: 'string',
description: 'The ID of the message to react to.'
},
emoji: {
type: 'string',
description: 'The emoji to use for the reaction.'
}
},
required: ['recipientPhoneNumber', 'messageId', 'emoji']
}
}
}
};
export { apiTool };