@@ -92,6 +92,9 @@ export async function initWhatsApp(options: {
9292 // Handle credentials update
9393 waSocket . ev . on ( "creds.update" , saveCreds ) ;
9494
95+ // Owner's real phone number extracted from sock.user.id on connection
96+ let selfPhoneNumber = "" ;
97+
9598 // Handle connection updates
9699 waSocket . ev . on ( "connection.update" , ( update ) => {
97100 const { connection, lastDisconnect, qr } = update ;
@@ -115,7 +118,15 @@ export async function initWhatsApp(options: {
115118 isConnected = true ;
116119 isConnecting = false ;
117120 currentQRCode = null ; // Clear QR when connected
118- owLogger . info ( "channel" , "WhatsApp connected successfully" ) ;
121+
122+ // OpenClaw pattern (monitor.ts:68-69): extract the real phone number from sock.user.id
123+ // sock.user.id is always the real phone JID (e.g. "14378762880:5@s.whatsapp.net")
124+ // even on linked devices where remoteJid uses LID numbers.
125+ const selfJid = waSocket ?. user ?. id || "" ;
126+ const selfPhoneMatch = selfJid . match ( / ^ ( \d + ) (?: : \d + ) ? @ / ) ;
127+ selfPhoneNumber = selfPhoneMatch ? selfPhoneMatch [ 1 ] : "" ;
128+
129+ owLogger . info ( "channel" , "WhatsApp connected successfully" , { selfJid, selfPhoneNumber : selfPhoneNumber || "(unknown)" } ) ;
119130 console . log ( "\n✅ WhatsApp connected successfully!" ) ;
120131 options . onConnected ?.( ) ;
121132 }
@@ -189,31 +200,22 @@ export async function initWhatsApp(options: {
189200 const from = msg . key . remoteJid ?. replace ( "@s.whatsapp.net" , "" ) . replace ( "@lid" , "" ) || "" ;
190201 const effectiveTimestamp = msgTimestamp || Date . now ( ) / 1000 ;
191202
192- // OpenClaw pattern (access-control.ts:120 ): skip fromMe ONLY for DMs to other people.
203+ // OpenClaw pattern (access-control.ts:54 ): skip fromMe ONLY for DMs to other people.
193204 // When the conversation is with yourself (self-chat), fromMe messages ARE the owner
194205 // talking to the AI — those must be processed.
206+ // Use selfPhoneNumber (from sock.user.id) which is ALWAYS the real phone number,
207+ // even on linked devices where remoteJid uses LID numbers.
195208 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
206- const ownerNum = process . env . WHATSAPP_OWNER_NUMBER ?. replace ( / [ ^ 0 - 9 ] / g, "" ) || "" ;
207209 const fromDigits = from . replace ( / [ ^ 0 - 9 ] / g, "" ) ;
208- const isOwnerDigitMatch = ownerNum && fromDigits . includes ( ownerNum ) ;
210+ const isSelfChat = selfPhoneNumber && fromDigits . includes ( selfPhoneNumber ) ;
209211
210- if ( ! isSelfChat && ! isOwnerDigitMatch ) {
211- owLogger . debug ( "channel" , "WhatsApp skipping outbound to other contact" , { messageId, from, preview : text . slice ( 0 , 40 ) } ) ;
212+ if ( ! isSelfChat ) {
213+ owLogger . debug ( "channel" , "WhatsApp skipping outbound to other contact" , { messageId, from, selfPhoneNumber , preview : text . slice ( 0 , 40 ) } ) ;
212214 markMessageProcessed ( messageId , "outbound" , from , { content : text } ) ;
213215 continue ;
214216 }
215217 // Self-chat fromMe: owner is talking to AI, let it through
216- owLogger . info ( "channel" , "WhatsApp self-chat message (owner→AI)" , { messageId, from, selfJid : selfNormalized , isSelfChat , isOwnerDigitMatch , preview : text . slice ( 0 , 40 ) } ) ;
218+ owLogger . info ( "channel" , "WhatsApp self-chat message (owner→AI)" , { messageId, from, preview : text . slice ( 0 , 40 ) } ) ;
217219 }
218220
219221 owLogger . info ( "channel" , `WhatsApp message received` , { from, fromMe : msg . key . fromMe , messageId, preview : text . slice ( 0 , 60 ) , pushName : msg . pushName || null } ) ;
0 commit comments