diff --git a/src/channels/whatsapp.ts b/src/channels/whatsapp.ts index 71d2f8a..96cea30 100644 --- a/src/channels/whatsapp.ts +++ b/src/channels/whatsapp.ts @@ -228,13 +228,12 @@ Ask the bot owner to approve with: const remoteJid = m.key.remoteJid || ''; // Detect self-chat: message from ourselves to ourselves - // For self-chat, senderPn is undefined, so we detect by: fromMe + LID + selfChatMode const senderPn = (m.key as any).senderPn as string | undefined; const isSelfChat = m.key.fromMe && ( remoteJid === this.myJid || remoteJid.replace(/@.*/, '') === this.myNumber || - // In selfChatMode, fromMe + LID (with no senderPn) = self-chat - (this.config.selfChatMode && remoteJid.includes('@lid') && !senderPn) + // In selfChatMode, fromMe + LID = self-chat (don't require !senderPn as it can vary) + (this.config.selfChatMode && remoteJid.includes('@lid')) ); // Track self-chat LID for reply conversion @@ -336,8 +335,11 @@ Ask the bot owner to approve with: } else if (this.lidToJid.has(targetJid)) { // Friend LID -> their real JID from senderPn targetJid = this.lidToJid.get(targetJid)!; + } else { + // FAIL SAFE: Don't send to unknown LID - could go to wrong person + console.error(`[WhatsApp] Cannot send to unknown LID: ${targetJid}`); + throw new Error(`Cannot send to unknown LID - no mapping found`); } - // If no mapping, keep as-is and hope baileys handles it } try { diff --git a/src/config/io.ts b/src/config/io.ts index 59dc492..a2cf916 100644 --- a/src/config/io.ts +++ b/src/config/io.ts @@ -123,8 +123,9 @@ export function configToEnv(config: LettaBotConfig): Record { } if (config.channels.whatsapp?.enabled) { env.WHATSAPP_ENABLED = 'true'; - if (config.channels.whatsapp.selfChat) { - env.WHATSAPP_SELF_CHAT_MODE = 'true'; + // WhatsApp selfChat defaults to true, so only set env if explicitly false + if (config.channels.whatsapp.selfChat === false) { + env.WHATSAPP_SELF_CHAT_MODE = 'false'; } } if (config.channels.signal?.phone) { diff --git a/src/main.ts b/src/main.ts index aa21bb1..288979b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -161,7 +161,7 @@ const config = { sessionPath: process.env.WHATSAPP_SESSION_PATH || './data/whatsapp-session', dmPolicy: (process.env.WHATSAPP_DM_POLICY || 'pairing') as 'pairing' | 'allowlist' | 'open', allowedUsers: process.env.WHATSAPP_ALLOWED_USERS?.split(',').filter(Boolean) || [], - selfChatMode: process.env.WHATSAPP_SELF_CHAT_MODE === 'true', + selfChatMode: process.env.WHATSAPP_SELF_CHAT_MODE !== 'false', // Default true }, signal: { enabled: !!process.env.SIGNAL_PHONE_NUMBER, diff --git a/src/onboard.ts b/src/onboard.ts index c61528f..5744377 100644 --- a/src/onboard.ts +++ b/src/onboard.ts @@ -1395,13 +1395,13 @@ export async function onboard(options?: { nonInteractive?: boolean }): Promise