Deploy: 2026-06-24 15:16:49
This commit is contained in:
@@ -98,9 +98,20 @@ app.post('/api/contacts/check', async (req, res) => {
|
||||
}
|
||||
}
|
||||
if (activeSlots.length === 0) {
|
||||
return res.status(503).json({ error: 'No WhatsApp slots are ready' });
|
||||
return res.status(503).json({ error: 'No WhatsApp slots are ready to check contacts' });
|
||||
}
|
||||
session_key = activeSlots[0]; // Just use the first available slot for checking
|
||||
|
||||
let checkError = null;
|
||||
for (const slotKey of activeSlots) {
|
||||
try {
|
||||
const result = await checkContact(slotKey, phone);
|
||||
return res.json({ status: 'success', session_key: slotKey, data: result });
|
||||
} catch (err) {
|
||||
console.error(`[ContactCheck] Failed check via ${slotKey}:`, err.message);
|
||||
checkError = err;
|
||||
}
|
||||
}
|
||||
return res.status(500).json({ error: `Failed to check contact via any active slot. Last error: ${checkError.message}` });
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -128,7 +139,7 @@ app.post('/api/messages/send', async (req, res) => {
|
||||
|
||||
try {
|
||||
if (session_key === 'auto') {
|
||||
const activeSlots = [];
|
||||
let activeSlots = [];
|
||||
for (let i = 1; i <= 6; i++) {
|
||||
if (isSessionReady(`slot-${i}`)) {
|
||||
activeSlots.push(`slot-${i}`);
|
||||
@@ -139,14 +150,35 @@ app.post('/api/messages/send', async (req, res) => {
|
||||
return res.status(503).json({ error: 'No WhatsApp slots are currently ready to send messages' });
|
||||
}
|
||||
|
||||
// Round-robin selection
|
||||
session_key = activeSlots[currentSlotIndex % activeSlots.length];
|
||||
currentSlotIndex++;
|
||||
console.log(`[RoundRobin] Selected ${session_key} out of ${activeSlots.length} active slots.`);
|
||||
}
|
||||
let lastError = null;
|
||||
let attempts = 0;
|
||||
const maxAttempts = activeSlots.length;
|
||||
|
||||
const result = await sendMessage(session_key, phone, message, media_url, audio, mimetype, image);
|
||||
res.json({ status: 'success', data: result });
|
||||
while (attempts < maxAttempts) {
|
||||
const selectedKey = activeSlots[currentSlotIndex % activeSlots.length];
|
||||
currentSlotIndex++;
|
||||
attempts++;
|
||||
|
||||
try {
|
||||
console.log(`[RoundRobin] Attempt ${attempts}/${maxAttempts}: Trying to send via ${selectedKey}`);
|
||||
const result = await sendMessage(selectedKey, phone, message, media_url, audio, mimetype, image);
|
||||
return res.json({ status: 'success', session_key: selectedKey, data: result });
|
||||
} catch (err) {
|
||||
console.error(`[RoundRobin] Attempt ${attempts} failed via ${selectedKey}:`, err.message);
|
||||
lastError = err;
|
||||
|
||||
activeSlots = activeSlots.filter(s => s !== selectedKey);
|
||||
if (activeSlots.length === 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(500).json({
|
||||
error: `Failed to send message after trying all active slots. Last error: ${lastError ? lastError.message : 'Unknown'}`
|
||||
});
|
||||
} else {
|
||||
const result = await sendMessage(session_key, phone, message, media_url, audio, mimetype, image);
|
||||
res.json({ status: 'success', data: result });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error sending message via ${session_key} to ${phone}:`, err);
|
||||
res.status(500).json({ error: err.message || 'Failed to send message' });
|
||||
|
||||
Reference in New Issue
Block a user