send-reply-to-text-message.js•3.01 kB
/**
* Function to send a reply to a text message via WhatsApp API.
*
* @param {Object} args - Arguments for sending the reply.
* @param {string} args.recipientPhoneNumber - The phone number of the recipient.
* @param {string} args.messageId - The message ID of the previous message to reply to.
* @param {string} args.textMessageContent - The content of the text message to send.
* @returns {Promise<Object>} - The result of the send message operation.
*/
const executeFunction = async ({ recipientPhoneNumber, messageId, textMessageContent }) => {
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`;
// Prepare the request body
const body = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
context: {
message_id: messageId
},
type: "text",
text: {
preview_url: false,
body: textMessageContent
}
};
// 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(body)
});
// 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 reply to text message:', error);
return {
error: `An error occurred while sending the reply: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a reply to a text message via WhatsApp API.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_reply_to_text_message',
description: 'Send a reply to a text message via WhatsApp API.',
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 to reply to.'
},
textMessageContent: {
type: 'string',
description: 'The content of the text message to send.'
}
},
required: ['recipientPhoneNumber', 'messageId', 'textMessageContent']
}
}
}
};
export { apiTool };