main
borja 3 months ago
parent 233f27aa59
commit 4daca04352

Binary file not shown.

@ -8,41 +8,42 @@ const INSTANCE_NAME = process.env.INSTANCE_NAME;
const API_KEY = process.env.API_KEY; const API_KEY = process.env.API_KEY;
if (!API_URL || !INSTANCE_NAME || !API_KEY) { if (!API_URL || !INSTANCE_NAME || !API_KEY) {
throw new Error('Required environment variables are not defined.'); throw new Error('Required environment variables are not defined.');
} }
// Send a message with optional mentions // Send a message with optional mentions
export async function sendMessage(phone: string, message: string | {text: string}, mentions: string[] = []) { export async function sendMessage(phone: string, message: string | { text: string }, mentions: string[] = []) {
try { try {
// Normalize message input to always have text property // Normalize message input to always have text property
const messageText = typeof message === 'string' ? message : message.text; const messageText = typeof message === 'string' ? message : message.text;
console.log(`el phone es ${phone} y el mensaje es ${message}`)
// Build payload according to API specification // Build payload according to API specification
const payload = { const payload = {
number: phone, number: phone,
textMessage: { text: messageText,
text: messageText textMessage: {
}, text: messageText
options: { },
delay: 1200, options: {
presence: 'composing', delay: 1200,
linkPreview: false, presence: 'composing',
...(mentions.length > 0 && { linkPreview: false,
mentions: { ...(mentions.length > 0 && {
everyOne: false, mentions: {
mentioned: mentions.map(phone => phone.split('@')[0]) everyOne: false,
} mentioned: mentions.map(phone => phone.split('@')[0])
}) }
} })
}; }
};
await axios.post(`${API_URL}/message/sendText/${INSTANCE_NAME}`, payload, { await axios.post(`${API_URL}/message/sendText/${INSTANCE_NAME}`, payload, {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
apikey: API_KEY, apikey: API_KEY,
}, },
}); });
} catch (error) { } catch (error) {
console.error('Error sending message:', error); console.error('Error sending message:', error);
} }
} }

Loading…
Cancel
Save