Fix session deletion bug on reconnect limit

This commit is contained in:
Hamza-Ayed
2026-05-22 19:16:29 +03:00
parent 5478fbbae6
commit de6e90d873

View File

@@ -104,36 +104,36 @@ async function startSession(session_key, webhook_url) {
} }
if (m.type !== 'notify') return; if (m.type !== 'notify') return;
for (const msg of m.messages) { for (const msg of m.messages) {
// Ignore messages sent by ourselves // Ignore messages sent by ourselves
if (msg.key.fromMe) continue; if (msg.key.fromMe) continue;
const remoteJid = msg.key.remoteJid; const remoteJid = msg.key.remoteJid;
if (!remoteJid) continue; if (!remoteJid) continue;
// Only process individual chats (ignore groups and broadcasts) // Only process individual chats (ignore groups and broadcasts)
const isGroup = remoteJid.endsWith('@g.us'); const isGroup = remoteJid.endsWith('@g.us');
const isBroadcast = remoteJid.endsWith('@broadcast'); const isBroadcast = remoteJid.endsWith('@broadcast');
if (isGroup || isBroadcast) continue; if (isGroup || isBroadcast) continue;
// Extract text body // Extract text body
const body = msg.message?.conversation || const body = msg.message?.conversation ||
msg.message?.extendedTextMessage?.text || msg.message?.extendedTextMessage?.text ||
msg.message?.imageMessage?.caption || msg.message?.imageMessage?.caption ||
msg.message?.videoMessage?.caption || ''; msg.message?.videoMessage?.caption || '';
const isAudio = !!msg.message?.audioMessage; const isAudio = !!msg.message?.audioMessage;
const isImage = !!msg.message?.imageMessage; const isImage = !!msg.message?.imageMessage;
// Only process messages that have text content OR are audio/image messages // Only process messages that have text content OR are audio/image messages
if (!body && !isAudio && !isImage) continue; if (!body && !isAudio && !isImage) continue;
let audioBase64 = null; let audioBase64 = null;
let audioMimeType = null; let audioMimeType = null;
let imageBase64 = null; let imageBase64 = null;
let imageMimeType = null; let imageMimeType = null;
if (isAudio) { if (isAudio) {
try { try {
console.log(`[Baileys] Downloading audio message for ${remoteJid}`); console.log(`[Baileys] Downloading audio message for ${remoteJid}`);
@@ -141,7 +141,7 @@ async function startSession(session_key, webhook_url) {
msg, msg,
'buffer', 'buffer',
{}, {},
{ {
logger: pino({ level: 'silent' }), logger: pino({ level: 'silent' }),
rekey: true rekey: true
} }
@@ -159,7 +159,7 @@ async function startSession(session_key, webhook_url) {
msg, msg,
'buffer', 'buffer',
{}, {},
{ {
logger: pino({ level: 'silent' }), logger: pino({ level: 'silent' }),
rekey: true rekey: true
} }
@@ -171,7 +171,7 @@ async function startSession(session_key, webhook_url) {
continue; // Skip if image download fails continue; // Skip if image download fails
} }
} }
// Extract sender phone number (handle LID privacy scheme) // Extract sender phone number (handle LID privacy scheme)
let senderPhone = ''; let senderPhone = '';
if (msg.key.senderPn && msg.key.senderPn.endsWith('@s.whatsapp.net')) { if (msg.key.senderPn && msg.key.senderPn.endsWith('@s.whatsapp.net')) {
@@ -180,12 +180,13 @@ async function startSession(session_key, webhook_url) {
senderPhone = remoteJid.split('@')[0]; senderPhone = remoteJid.split('@')[0];
} else if (remoteJid.endsWith('@lid')) { } else if (remoteJid.endsWith('@lid')) {
senderPhone = remoteJid.split('@')[0]; senderPhone = remoteJid.split('@')[0];
senderPhone = remoteJid.split('@')[0];
} }
if (!senderPhone) continue; if (!senderPhone) continue;
const senderName = msg.pushName || ''; const senderName = msg.pushName || '';
if (isAudio) { if (isAudio) {
console.log(`[Message] Received audio voice note from ${senderPhone} (JID: ${remoteJid})`); console.log(`[Message] Received audio voice note from ${senderPhone} (JID: ${remoteJid})`);
} else if (isImage) { } else if (isImage) {
@@ -193,7 +194,7 @@ async function startSession(session_key, webhook_url) {
} else { } else {
console.log(`[Message] Received from ${senderPhone} (JID: ${remoteJid}): ${body}`); console.log(`[Message] Received from ${senderPhone} (JID: ${remoteJid}): ${body}`);
} }
await sendWebhook(webhook_url, { await sendWebhook(webhook_url, {
session_key, session_key,
state: 'message_received', state: 'message_received',
@@ -317,13 +318,13 @@ async function sendMessage(session_key, phone, message, mediaUrl = null, audioBa
let jid = phone.includes('@') ? phone : `${phone}@s.whatsapp.net`; let jid = phone.includes('@') ? phone : `${phone}@s.whatsapp.net`;
let sentMsg; let sentMsg;
if (audioBase64) { if (audioBase64) {
const buffer = Buffer.from(audioBase64, 'base64'); const buffer = Buffer.from(audioBase64, 'base64');
sentMsg = await sock.sendMessage(jid, { sentMsg = await sock.sendMessage(jid, {
audio: buffer, audio: buffer,
mimetype: 'audio/mp4', mimetype: 'audio/mp4',
ptt: true ptt: true
}); });
} else if (mediaUrl) { } else if (mediaUrl) {
const ext = mediaUrl.split('.').pop().toLowerCase(); const ext = mediaUrl.split('.').pop().toLowerCase();
@@ -337,7 +338,7 @@ async function sendMessage(session_key, phone, message, mediaUrl = null, audioBa
} else { } else {
sentMsg = await sock.sendMessage(jid, { text: message }); sentMsg = await sock.sendMessage(jid, { text: message });
} }
// Cache outbound messages for E2EE decryption retries // Cache outbound messages for E2EE decryption retries
if (sentMsg && sentMsg.key && sentMsg.key.id && sentMsg.message) { if (sentMsg && sentMsg.key && sentMsg.key.id && sentMsg.message) {
recentMessages.set(sentMsg.key.id, sentMsg.message); recentMessages.set(sentMsg.key.id, sentMsg.message);
@@ -346,7 +347,7 @@ async function sendMessage(session_key, phone, message, mediaUrl = null, audioBa
recentMessages.delete(firstKey); recentMessages.delete(firstKey);
} }
} }
return sentMsg; return sentMsg;
} }