@@ -193,17 +193,27 @@ export async function initWhatsApp(options: {
193193 // When the conversation is with yourself (self-chat), fromMe messages ARE the owner
194194 // talking to the AI — those must be processed.
195195 if ( msg . key . fromMe ) {
196+ // Primary: compare remoteJid with sock.user.id (works for both LID and phone JIDs)
197+ // Baileys knows the connected user's JID regardless of LID vs phone format.
198+ const selfJid = waSocket ?. user ?. id ;
199+ const remoteJid = msg . key . remoteJid || "" ;
200+ // Normalize: strip :XX device suffix from selfJid for comparison
201+ // e.g. "14378762880:12@s.whatsapp.net" → "14378762880@s.whatsapp.net"
202+ const selfNormalized = selfJid ?. replace ( / : \d + @ / , "@" ) || "" ;
203+ const isSelfChat = selfNormalized && remoteJid === selfNormalized ;
204+
205+ // Fallback: also check owner number digits in case JID formats differ
196206 const ownerNum = process . env . WHATSAPP_OWNER_NUMBER ?. replace ( / [ ^ 0 - 9 ] / g, "" ) || "" ;
197207 const fromDigits = from . replace ( / [ ^ 0 - 9 ] / g, "" ) ;
198- const isSelfChat = ownerNum && fromDigits . includes ( ownerNum ) ;
208+ const isOwnerDigitMatch = ownerNum && fromDigits . includes ( ownerNum ) ;
199209
200- if ( ! isSelfChat ) {
210+ if ( ! isSelfChat && ! isOwnerDigitMatch ) {
201211 owLogger . debug ( "channel" , "WhatsApp skipping outbound to other contact" , { messageId, from, preview : text . slice ( 0 , 40 ) } ) ;
202212 markMessageProcessed ( messageId , "outbound" , from , { content : text } ) ;
203213 continue ;
204214 }
205215 // Self-chat fromMe: owner is talking to AI, let it through
206- owLogger . info ( "channel" , "WhatsApp self-chat message (owner→AI)" , { messageId, from, preview : text . slice ( 0 , 40 ) } ) ;
216+ owLogger . info ( "channel" , "WhatsApp self-chat message (owner→AI)" , { messageId, from, selfJid : selfNormalized , isSelfChat , isOwnerDigitMatch , preview : text . slice ( 0 , 40 ) } ) ;
207217 }
208218
209219 owLogger . info ( "channel" , `WhatsApp message received` , { from, fromMe : msg . key . fromMe , messageId, preview : text . slice ( 0 , 60 ) , pushName : msg . pushName || null } ) ;
0 commit comments