From 534c54a1713854a03631c8836254c7e06ed188b7 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 13 Jun 2025 08:28:09 -0300 Subject: [PATCH] fix(chatwoot.service): enhance contact retrieval logic and normalize identifiers - Updated contact retrieval to handle cases where the identifier is not found, specifically for @lid scenarios. - Introduced a new method to normalize contact identifiers, prioritizing senderLid and participantLid. - Improved logging for better traceability during contact lookups and conversation handling. --- CHANGELOG.md | 3 +- .../chatwoot/services/chatwoot.service.ts | 48 ++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38211249..8d5424b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ ### Fixed * Shell injection vulnerability -* Update Baileys Version v6.7.17 +* Update Baileys Version v6.7.18 * Audio send duplicate from chatwoot * Chatwoot csat creating new conversation in another language * Refactor SQS controller to correct bug in sqs events by instance @@ -21,6 +21,7 @@ * Preventing use conversation from other inbox for the same user * Ensure full WhatsApp compatibility for audio conversion (libopus, 48kHz, mono) * Enhance message fetching and processing logic +* Fixed issue with @lid in chatwoot ### Security diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index cafa14cb..55ab1540 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -442,7 +442,21 @@ export class ChatwootService { }); } - if (!contact && contact?.payload?.length === 0) { + // Se não encontrou e não é @lid, tenta buscar pelo número limpo + if ((!contact || contact?.payload?.length === 0) && !isLid) { + const cleanNumber = `+${phoneNumber.replace('@lid', '')}`; + this.logger.verbose(`Contact not found by identifier, trying clean number: ${cleanNumber}`); + + contact = await chatwootRequest(this.getClientCwConfig(), { + method: 'POST', + url: `/api/v1/accounts/${this.provider.accountId}/contacts/filter`, + body: { + payload: this.getFilterPayload(cleanNumber), + }, + }); + } + + if (!contact || contact?.payload?.length === 0) { this.logger.warn('contact not found'); return null; } @@ -545,6 +559,19 @@ export class ChatwootService { return filterPayload; } + private normalizeContactIdentifier(msg: any) { + // Priority: senderLid > participantLid > remoteJid with @lid > normal number + const lidIdentifier = + msg.key.senderLid || msg.key.participantLid || (msg.key.remoteJid?.includes('@lid') ? msg.key.remoteJid : null); + + if (lidIdentifier) { + return lidIdentifier; + } + + // If it doesn't have @lid, return the normal number + return msg.key.participant?.split('@')[0] || msg.key.remoteJid?.split('@')[0]; + } + public async createConversation(instance: InstanceDto, body: any) { const remoteJid = body.key.remoteJid; const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`; @@ -598,11 +625,16 @@ export class ChatwootService { const isGroup = remoteJid.includes('@g.us'); const isLid = remoteJid.includes('@lid'); - const chatId = isGroup || isLid ? remoteJid : remoteJid.split('@')[0]; - let nameContact = !body.key.fromMe ? body.pushName : chatId; + this.logger.verbose('is group: ' + isGroup); + + const chatId = this.normalizeContactIdentifier(body); + this.logger.verbose('chat id: ' + chatId); + const filterInbox = await this.getInbox(instance); if (!filterInbox) return null; + let nameContact = !body.key.fromMe ? body.pushName : chatId; + if (isGroup || isLid) { this.logger.verbose(`Processing group conversation`); const group = await this.waMonitor.waInstances[instance.instanceName].client.groupMetadata(chatId); @@ -615,7 +647,10 @@ export class ChatwootService { ); this.logger.verbose(`Participant profile picture URL: ${JSON.stringify(picture_url)}`); - const findParticipant = await this.findContact(instance, body.key.participant.split('@')[0]); + const participantIdentifier = this.normalizeContactIdentifier(body); + this.logger.verbose(`Normalized participant identifier: ${participantIdentifier}`); + + const findParticipant = await this.findContact(instance, participantIdentifier); this.logger.verbose(`Found participant: ${JSON.stringify(findParticipant)}`); if (findParticipant) { @@ -628,7 +663,7 @@ export class ChatwootService { } else { await this.createContact( instance, - body.key.participant.split('@')[0], + participantIdentifier, filterInbox.id, false, body.pushName, @@ -721,7 +756,8 @@ export class ChatwootService { } } else { inboxConversation = contactConversations.payload.find( - (conversation) => conversation && conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id, + (conversation) => + conversation && conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id, ); this.logger.verbose(`Found conversation: ${JSON.stringify(inboxConversation)}`); }