fix: translate WhatsApp LID JIDs to phone JIDs for self-chat messages (#62)
WhatsApp recently changed to send self-chat messages using LID (Linked ID) format (e.g., xxxxxx@lid) instead of phone number format (e.g., xxxxxx@s.whatsapp.net). This caused messages to yourself to be silently dropped because they didn't match any registered group. ## How to reproduce 1. Send a message to yourself on WhatsApp with the trigger 2. Message is received by Baileys but remoteJid is in LID format 3. LID JID doesn't match registered group JID (phone format) 4. Message is not stored and no response is sent ## The fix - Build a LID-to-phone mapping from sock.user on connection open - Translate incoming LID JIDs to phone JIDs before storing/processing messages - This allows self-chat messages to correctly match the registered main channel The mapping is populated from sock.user.id (phone) and sock.user.lid (LID) which Baileys provides after successful authentication.
This commit is contained in:
35
src/index.ts
35
src/index.ts
@@ -55,6 +55,23 @@ let lastTimestamp = '';
|
|||||||
let sessions: Session = {};
|
let sessions: Session = {};
|
||||||
let registeredGroups: Record<string, RegisteredGroup> = {};
|
let registeredGroups: Record<string, RegisteredGroup> = {};
|
||||||
let lastAgentTimestamp: Record<string, string> = {};
|
let lastAgentTimestamp: Record<string, string> = {};
|
||||||
|
// LID to phone number mapping (WhatsApp now sends LID JIDs for self-chats)
|
||||||
|
let lidToPhoneMap: Record<string, string> = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate a JID from LID format to phone format if we have a mapping.
|
||||||
|
* Returns the original JID if no mapping exists.
|
||||||
|
*/
|
||||||
|
function translateJid(jid: string): string {
|
||||||
|
if (!jid.endsWith('@lid')) return jid;
|
||||||
|
const lidUser = jid.split('@')[0].split(':')[0];
|
||||||
|
const phoneJid = lidToPhoneMap[lidUser];
|
||||||
|
if (phoneJid) {
|
||||||
|
logger.debug({ lidJid: jid, phoneJid }, 'Translated LID to phone JID');
|
||||||
|
return phoneJid;
|
||||||
|
}
|
||||||
|
return jid;
|
||||||
|
}
|
||||||
|
|
||||||
async function setTyping(jid: string, isTyping: boolean): Promise<void> {
|
async function setTyping(jid: string, isTyping: boolean): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@@ -670,6 +687,17 @@ async function connectWhatsApp(): Promise<void> {
|
|||||||
}
|
}
|
||||||
} else if (connection === 'open') {
|
} else if (connection === 'open') {
|
||||||
logger.info('Connected to WhatsApp');
|
logger.info('Connected to WhatsApp');
|
||||||
|
|
||||||
|
// Build LID to phone mapping from auth state for self-chat translation
|
||||||
|
if (sock.user) {
|
||||||
|
const phoneUser = sock.user.id.split(':')[0];
|
||||||
|
const lidUser = sock.user.lid?.split(':')[0];
|
||||||
|
if (lidUser && phoneUser) {
|
||||||
|
lidToPhoneMap[lidUser] = `${phoneUser}@s.whatsapp.net`;
|
||||||
|
logger.debug({ lidUser, phoneUser }, 'LID to phone mapping set');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Sync group metadata on startup (respects 24h cache)
|
// Sync group metadata on startup (respects 24h cache)
|
||||||
syncGroupMetadata().catch((err) =>
|
syncGroupMetadata().catch((err) =>
|
||||||
logger.error({ err }, 'Initial group sync failed'),
|
logger.error({ err }, 'Initial group sync failed'),
|
||||||
@@ -695,8 +723,11 @@ async function connectWhatsApp(): Promise<void> {
|
|||||||
sock.ev.on('messages.upsert', ({ messages }) => {
|
sock.ev.on('messages.upsert', ({ messages }) => {
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
if (!msg.message) continue;
|
if (!msg.message) continue;
|
||||||
const chatJid = msg.key.remoteJid;
|
const rawJid = msg.key.remoteJid;
|
||||||
if (!chatJid || chatJid === 'status@broadcast') continue;
|
if (!rawJid || rawJid === 'status@broadcast') continue;
|
||||||
|
|
||||||
|
// Translate LID JID to phone JID if applicable
|
||||||
|
const chatJid = translateJid(rawJid);
|
||||||
|
|
||||||
const timestamp = new Date(
|
const timestamp = new Date(
|
||||||
Number(msg.messageTimestamp) * 1000,
|
Number(msg.messageTimestamp) * 1000,
|
||||||
|
|||||||
Reference in New Issue
Block a user