Skip to content

Commit 534c54a

Browse files
committed
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.
1 parent 8603e6d commit 534c54a

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
### Fixed
1313

1414
* Shell injection vulnerability
15-
* Update Baileys Version v6.7.17
15+
* Update Baileys Version v6.7.18
1616
* Audio send duplicate from chatwoot
1717
* Chatwoot csat creating new conversation in another language
1818
* Refactor SQS controller to correct bug in sqs events by instance
@@ -21,6 +21,7 @@
2121
* Preventing use conversation from other inbox for the same user
2222
* Ensure full WhatsApp compatibility for audio conversion (libopus, 48kHz, mono)
2323
* Enhance message fetching and processing logic
24+
* Fixed issue with @lid in chatwoot
2425

2526
### Security
2627

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)