send-audio-message-by-id.js•2.36 kB
/**
* Function to send an audio message via WhatsApp.
*
* @param {Object} args - Arguments for sending the audio message.
* @param {string} args.recipientPhoneNumber - The recipient's phone number.
* @param {string} args.audioObjectId - The ID of the audio object to send.
* @returns {Promise<Object>} - The result of the audio message sending operation.
*/
const executeFunction = async ({ recipientPhoneNumber, audioObjectId }) => {
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 body = JSON.stringify({
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
type: "audio",
audio: {
id: audioObjectId
}
});
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
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 audio message:', error);
return {
error: `An error occurred while sending the audio message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending audio messages via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_audio_message',
description: 'Send an audio message via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The recipient\'s phone number.'
},
audioObjectId: {
type: 'string',
description: 'The ID of the audio object to send.'
}
},
required: ['recipientPhoneNumber', 'audioObjectId']
}
}
}
};
export { apiTool };