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;
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
export async function sendMessage(phone: string, message: string | {text: string}, mentions: string[] = []) {
try {
// Normalize message input to always have text property
const messageText = typeof message === 'string' ? message : message.text;
export async function sendMessage(phone: string, message: string | { text: string }, mentions: string[] = []) {
try {
// Normalize message input to always have text property
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
const payload = {
number: phone,
text: messageText,
textMessage: {
text: messageText
},
options: {
delay: 1200,
presence: 'composing',
linkPreview: false,
...(mentions.length > 0 && {
mentions: {
everyOne: false,
mentioned: mentions.map(phone => phone.split('@')[0])
}
})
}
};
// Build payload according to API specification
const payload = {
number: phone,
textMessage: {
text: messageText
},
options: {
delay: 1200,
presence: 'composing',
linkPreview: false,
...(mentions.length > 0 && {
mentions: {
everyOne: false,
mentioned: mentions.map(phone => phone.split('@')[0])
}
})
}
};
await axios.post(`${API_URL}/message/sendText/${INSTANCE_NAME}`, payload, {
headers: {
'Content-Type': 'application/json',
apikey: API_KEY,
},
});
} catch (error) {
console.error('Error sending message:', error);
}
await axios.post(`${API_URL}/message/sendText/${INSTANCE_NAME}`, payload, {
headers: {
'Content-Type': 'application/json',
apikey: API_KEY,
},
});
} catch (error) {
console.error('Error sending message:', error);
}
}

Loading…
Cancel
Save