Deploy: 2026-06-24 15:16:49

This commit is contained in:
Hamza-Ayed
2026-06-24 15:16:49 +03:00
parent 376bb0f61c
commit 2562652e68
2 changed files with 64 additions and 12 deletions

View File

@@ -62,6 +62,19 @@ function cleanChromeLocks(sessionKey) {
} }
} }
// Clear Session Folder recursively to prevent corruption on logouts
function clearSessionFolder(sessionKey) {
const sessionPath = path.join(SESSIONS_DIR, `session-${sessionKey}`);
try {
if (fs.existsSync(sessionPath)) {
console.log(`[CLEANUP] Recursively removing session folder for ${sessionKey}: ${sessionPath}`);
fs.rmSync(sessionPath, { recursive: true, force: true });
}
} catch (err) {
console.error(`[CLEANUP ERROR] Failed to delete session folder for ${sessionKey}:`, err.message);
}
}
async function sendWebhook(webhook_url, payload) { async function sendWebhook(webhook_url, payload) {
try { try {
console.log(`[Webhook] Sending to ${webhook_url} | state=${payload.state}`); console.log(`[Webhook] Sending to ${webhook_url} | state=${payload.state}`);
@@ -130,6 +143,8 @@ async function startSession(session_key, webhook_url) {
}); });
sessions.delete(session_key); sessions.delete(session_key);
try { client.destroy(); } catch (_) {} try { client.destroy(); } catch (_) {}
// Wipe session folder to prevent corrupted state loading on next boot
clearSessionFolder(session_key);
}); });
// Handle Incoming Messages to Webhook // Handle Incoming Messages to Webhook
@@ -191,10 +206,15 @@ async function startSession(session_key, webhook_url) {
async function disconnectSession(session_key) { async function disconnectSession(session_key) {
const client = sessions.get(session_key); const client = sessions.get(session_key);
if (client) { if (client) {
await client.logout(); try {
client.destroy(); await client.logout();
} catch (_) {}
try {
client.destroy();
} catch (_) {}
sessions.delete(session_key); sessions.delete(session_key);
} }
clearSessionFolder(session_key);
} }
async function checkContact(session_key, phone) { async function checkContact(session_key, phone) {

View File

@@ -98,9 +98,20 @@ app.post('/api/contacts/check', async (req, res) => {
} }
} }
if (activeSlots.length === 0) { 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 { try {
@@ -128,7 +139,7 @@ app.post('/api/messages/send', async (req, res) => {
try { try {
if (session_key === 'auto') { if (session_key === 'auto') {
const activeSlots = []; let activeSlots = [];
for (let i = 1; i <= 6; i++) { for (let i = 1; i <= 6; i++) {
if (isSessionReady(`slot-${i}`)) { if (isSessionReady(`slot-${i}`)) {
activeSlots.push(`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' }); return res.status(503).json({ error: 'No WhatsApp slots are currently ready to send messages' });
} }
// Round-robin selection let lastError = null;
session_key = activeSlots[currentSlotIndex % activeSlots.length]; let attempts = 0;
currentSlotIndex++; const maxAttempts = activeSlots.length;
console.log(`[RoundRobin] Selected ${session_key} out of ${activeSlots.length} active slots.`);
}
const result = await sendMessage(session_key, phone, message, media_url, audio, mimetype, image); while (attempts < maxAttempts) {
res.json({ status: 'success', data: result }); 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) { } catch (err) {
console.error(`Error sending message via ${session_key} to ${phone}:`, err); console.error(`Error sending message via ${session_key} to ${phone}:`, err);
res.status(500).json({ error: err.message || 'Failed to send message' }); res.status(500).json({ error: err.message || 'Failed to send message' });