Fix: Reply via LID JID instead of phone JID to prevent E2EE session conflicts

This commit is contained in:
Hamza-Ayed
2026-05-22 20:09:55 +03:00
parent d868364c18
commit 5269789b51

View File

@@ -10,6 +10,7 @@ const path = require('path');
const sessions = new Map(); // Store active sockets in memory const sessions = new Map(); // Store active sockets in memory
const retryCounters = new Map(); // Track reconnection attempts per session const retryCounters = new Map(); // Track reconnection attempts per session
const recentMessages = new Map(); // Cache of recent messages in memory to serve getMessage callback const recentMessages = new Map(); // Cache of recent messages in memory to serve getMessage callback
const phoneToLid = new Map(); // Map phone numbers to LID JIDs for correct E2EE routing
// Global retry counter cache — persists across socket reconnects // Global retry counter cache — persists across socket reconnects
// This is CRITICAL for E2EE: tracks message retry attempts so Baileys can // This is CRITICAL for E2EE: tracks message retry attempts so Baileys can
@@ -191,11 +192,17 @@ async function startSession(session_key, webhook_url) {
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')) {
senderPhone = msg.key.senderPn.split('@')[0]; senderPhone = msg.key.senderPn.split('@')[0];
// CRITICAL: Map this phone number to the LID JID for reply routing.
// Signal E2EE sessions are bound to the LID, so replies MUST go
// to the LID JID, not the phone JID, to prevent session conflicts.
if (remoteJid.endsWith('@lid')) {
phoneToLid.set(senderPhone, remoteJid);
console.log(`[LID] Mapped ${senderPhone}${remoteJid}`);
}
} else if (remoteJid.endsWith('@s.whatsapp.net')) { } else if (remoteJid.endsWith('@s.whatsapp.net')) {
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;
@@ -359,7 +366,19 @@ async function sendMessage(session_key, phone, message, mediaUrl = null, audioBa
throw new Error(`Session ${session_key} is not active or connected`); throw new Error(`Session ${session_key} is not active or connected`);
} }
let jid = phone.includes('@') ? phone : `${phone}@s.whatsapp.net`; // Use the LID JID if we have a mapping for this phone number.
// This is CRITICAL: Signal E2EE sessions are bound to the LID JID,
// so replying to the phone JID creates a separate session that
// conflicts with the LID session, causing "Waiting for this message".
let jid;
if (phone.includes('@')) {
jid = phone;
} else if (phoneToLid.has(phone)) {
jid = phoneToLid.get(phone);
console.log(`[LID] Routing reply to ${phone} via LID: ${jid}`);
} else {
jid = `${phone}@s.whatsapp.net`;
}
let sentMsg; let sentMsg;
if (audioBase64) { if (audioBase64) {