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.
This commit is contained in:
Davidson Gomes 2025-06-13 08:28:09 -03:00
parent 8603e6def0
commit 534c54a171
2 changed files with 44 additions and 7 deletions

View File

@ -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

View File

@ -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)}`);
}