send-reply-to-location-message.js•3.34 kB
/**
* Function to send a location message via WhatsApp.
*
* @param {Object} args - Arguments for sending the location message.
* @param {string} args.recipientPhoneNumber - The recipient's phone number.
* @param {string} args.messageId - The message ID of the previous message.
* @param {number} args.latitude - The latitude of the location.
* @param {number} args.longitude - The longitude of the location.
* @param {string} args.locationName - The name of the location.
* @param {string} args.locationAddress - The address of the location.
* @returns {Promise<Object>} - The response from the WhatsApp API.
*/
const executeFunction = async ({ recipientPhoneNumber, messageId, latitude, longitude, locationName, locationAddress }) => {
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
const url = `${baseUrl}/${version}/${businessPhoneNumberId}/messages`;
const body = {
messaging_product: "whatsapp",
recipient_type: "individual",
to: recipientPhoneNumber,
context: {
message_id: messageId
},
type: "location",
location: {
latitude: latitude,
longitude: longitude,
name: locationName,
address: locationAddress
}
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(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 location message:', error);
return {
error: `An error occurred while sending the location message: ${error instanceof Error ? error.message : JSON.stringify(error)}`
};
}
};
/**
* Tool configuration for sending a location message via WhatsApp.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'send_location_message',
description: 'Send a location message via WhatsApp.',
parameters: {
type: 'object',
properties: {
recipientPhoneNumber: {
type: 'string',
description: 'The recipient\'s phone number.'
},
messageId: {
type: 'string',
description: 'The message ID of the previous message.'
},
latitude: {
type: 'number',
description: 'The latitude of the location.'
},
longitude: {
type: 'number',
description: 'The longitude of the location.'
},
locationName: {
type: 'string',
description: 'The name of the location.'
},
locationAddress: {
type: 'string',
description: 'The address of the location.'
}
},
required: ['recipientPhoneNumber', 'messageId', 'latitude', 'longitude', 'locationName', 'locationAddress']
}
}
}
};
export { apiTool };