send-reply-to-video-message-by-id.js•3.13 kB
/**
* Function to send a reply to a video message on WhatsApp.
*
* @param {Object} args - Arguments for sending the video message.
* @param {string} args.recipient - The phone number of the recipient.
* @param {string} args.messageId - The ID of the message you are replying to.
* @param {string} args.videoId - The ID of the video object you are sending.
* @param {string} [args.caption] - The caption for the video (optional).
* @returns {Promise<Object>} - The result of the message sending operation.
*/
const executeFunction = async ({ recipient, messageId, videoId, caption = '' }) => {
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 request
const payload = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipient,
context: {
message_id: messageId
},
type: "video",
video: {
caption: caption,
id: videoId
}
};
// 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 video message:', error);
return {
error: `An error occurred while sending the video message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a reply to a video message on WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_reply_video_message',
description: 'Send a reply to a video message on WhatsApp.',
parameters: {
type: 'object',
properties: {
recipient: {
type: 'string',
description: 'The phone number of the recipient.'
},
messageId: {
type: 'string',
description: 'The ID of the message you are replying to.'
},
videoId: {
type: 'string',
description: 'The ID of the video object you are sending.'
},
caption: {
type: 'string',
description: 'The caption for the video (optional).'
}
},
required: ['recipient', 'messageId', 'videoId']
}
}
}
};
export { apiTool };