@@ -442,7 +442,21 @@ export class ChatwootService {
442442 } ) ;
443443 }
444444
445- if ( ! contact && contact ?. payload ?. length === 0 ) {
445+ // Se não encontrou e não é @lid, tenta buscar pelo número limpo
446+ if ( ( ! contact || contact ?. payload ?. length === 0 ) && ! isLid ) {
447+ const cleanNumber = `+${ phoneNumber . replace ( '@lid' , '' ) } ` ;
448+ this . logger . verbose ( `Contact not found by identifier, trying clean number: ${ cleanNumber } ` ) ;
449+
450+ contact = await chatwootRequest ( this . getClientCwConfig ( ) , {
451+ method : 'POST' ,
452+ url : `/api/v1/accounts/${ this . provider . accountId } /contacts/filter` ,
453+ body : {
454+ payload : this . getFilterPayload ( cleanNumber ) ,
455+ } ,
456+ } ) ;
457+ }
458+
459+ if ( ! contact || contact ?. payload ?. length === 0 ) {
446460 this . logger . warn ( 'contact not found' ) ;
447461 return null ;
448462 }
@@ -545,6 +559,19 @@ export class ChatwootService {
545559 return filterPayload ;
546560 }
547561
562+ private normalizeContactIdentifier ( msg : any ) {
563+ // Priority: senderLid > participantLid > remoteJid with @lid > normal number
564+ const lidIdentifier =
565+ msg . key . senderLid || msg . key . participantLid || ( msg . key . remoteJid ?. includes ( '@lid' ) ? msg . key . remoteJid : null ) ;
566+
567+ if ( lidIdentifier ) {
568+ return lidIdentifier ;
569+ }
570+
571+ // If it doesn't have @lid, return the normal number
572+ return msg . key . participant ?. split ( '@' ) [ 0 ] || msg . key . remoteJid ?. split ( '@' ) [ 0 ] ;
573+ }
574+
548575 public async createConversation ( instance : InstanceDto , body : any ) {
549576 const remoteJid = body . key . remoteJid ;
550577 const cacheKey = `${ instance . instanceName } :createConversation-${ remoteJid } ` ;
@@ -598,11 +625,16 @@ export class ChatwootService {
598625
599626 const isGroup = remoteJid . includes ( '@g.us' ) ;
600627 const isLid = remoteJid . includes ( '@lid' ) ;
601- const chatId = isGroup || isLid ? remoteJid : remoteJid . split ( '@' ) [ 0 ] ;
602- let nameContact = ! body . key . fromMe ? body . pushName : chatId ;
628+ this . logger . verbose ( 'is group: ' + isGroup ) ;
629+
630+ const chatId = this . normalizeContactIdentifier ( body ) ;
631+ this . logger . verbose ( 'chat id: ' + chatId ) ;
632+
603633 const filterInbox = await this . getInbox ( instance ) ;
604634 if ( ! filterInbox ) return null ;
605635
636+ let nameContact = ! body . key . fromMe ? body . pushName : chatId ;
637+
606638 if ( isGroup || isLid ) {
607639 this . logger . verbose ( `Processing group conversation` ) ;
608640 const group = await this . waMonitor . waInstances [ instance . instanceName ] . client . groupMetadata ( chatId ) ;
@@ -615,7 +647,10 @@ export class ChatwootService {
615647 ) ;
616648 this . logger . verbose ( `Participant profile picture URL: ${ JSON . stringify ( picture_url ) } ` ) ;
617649
618- const findParticipant = await this . findContact ( instance , body . key . participant . split ( '@' ) [ 0 ] ) ;
650+ const participantIdentifier = this . normalizeContactIdentifier ( body ) ;
651+ this . logger . verbose ( `Normalized participant identifier: ${ participantIdentifier } ` ) ;
652+
653+ const findParticipant = await this . findContact ( instance , participantIdentifier ) ;
619654 this . logger . verbose ( `Found participant: ${ JSON . stringify ( findParticipant ) } ` ) ;
620655
621656 if ( findParticipant ) {
@@ -628,7 +663,7 @@ export class ChatwootService {
628663 } else {
629664 await this . createContact (
630665 instance ,
631- body . key . participant . split ( '@' ) [ 0 ] ,
666+ participantIdentifier ,
632667 filterInbox . id ,
633668 false ,
634669 body . pushName ,
@@ -721,7 +756,8 @@ export class ChatwootService {
721756 }
722757 } else {
723758 inboxConversation = contactConversations . payload . find (
724- ( conversation ) => conversation && conversation . status !== 'resolved' && conversation . inbox_id == filterInbox . id ,
759+ ( conversation ) =>
760+ conversation && conversation . status !== 'resolved' && conversation . inbox_id == filterInbox . id ,
725761 ) ;
726762 this . logger . verbose ( `Found conversation: ${ JSON . stringify ( inboxConversation ) } ` ) ;
727763 }
0 commit comments