Skip to content

Commit baff4e8

Browse files
committed
fix: update remoteJid handling to avoid unnecessary splitting for message number
1 parent 13f96a3 commit baff4e8

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,9 +1614,9 @@ export class BaileysStartupService extends ChannelStartupService {
16141614
// This enables LID to phoneNumber conversion without breaking existing webhook consumers
16151615

16161616
// Helper to normalize participantId as phone number
1617-
const normalizePhoneNumber = (id: string): string => {
1617+
const normalizePhoneNumber = (id: string | any): string => {
16181618
// Remove @lid, @s.whatsapp.net suffixes and extract just the number part
1619-
return id.split('@')[0];
1619+
return String(id || '').split('@')[0];
16201620
};
16211621

16221622
try {

src/api/integrations/chatbot/base-chatbot.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
211211
try {
212212
if (mediaType === 'audio') {
213213
await instance.audioWhatsapp({
214-
number: remoteJid.split('@')[0],
214+
number: remoteJid,
215215
delay: (settings as any)?.delayMessage || 1000,
216216
audio: url,
217217
caption: altText,
218218
});
219219
} else {
220220
await instance.mediaMessage(
221221
{
222-
number: remoteJid.split('@')[0],
222+
number: remoteJid,
223223
delay: (settings as any)?.delayMessage || 1000,
224224
mediatype: mediaType,
225225
media: url,
@@ -290,7 +290,7 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
290290
setTimeout(async () => {
291291
await instance.textMessage(
292292
{
293-
number: remoteJid.split('@')[0],
293+
number: remoteJid,
294294
delay: settings?.delayMessage || 1000,
295295
text: message,
296296
linkPreview,

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

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,20 @@ export class ChatwootService {
346346

347347
return contact;
348348
} catch (error) {
349+
if (
350+
(error.status === 422 || error.response?.status === 422) &&
351+
(error.message?.includes('taken') || error.response?.data?.message?.includes('taken')) &&
352+
jid
353+
) {
354+
this.logger.warn(`Contact with identifier ${jid} already exists, trying to find it...`);
355+
const existingContact = await this.findContactByIdentifier(instance, jid);
356+
if (existingContact) {
357+
const contactId = existingContact.id;
358+
await this.addLabelToContact(this.provider.nameInbox, contactId);
359+
return existingContact;
360+
}
361+
}
362+
349363
this.logger.error('Error creating contact');
350364
console.log(error);
351365
return null;
@@ -415,6 +429,55 @@ export class ChatwootService {
415429
}
416430
}
417431

432+
public async findContactByIdentifier(instance: InstanceDto, identifier: string) {
433+
const client = await this.clientCw(instance);
434+
435+
if (!client) {
436+
this.logger.warn('client not found');
437+
return null;
438+
}
439+
440+
// Direct search by query (q) - most common way to search by identifier/email/phone
441+
const contact = (await (client as any).get('contacts/search', {
442+
params: {
443+
q: identifier,
444+
sort: 'name',
445+
},
446+
})) as any;
447+
448+
if (contact && contact.data && contact.data.payload && contact.data.payload.length > 0) {
449+
return contact.data.payload[0];
450+
}
451+
452+
// Fallback for older API versions or different response structures
453+
if (contact && contact.payload && contact.payload.length > 0) {
454+
return contact.payload[0];
455+
}
456+
457+
// Try search by attribute
458+
const contactByAttr = (await (client as any).post('contacts/filter', {
459+
payload: [
460+
{
461+
attribute_key: 'identifier',
462+
filter_operator: 'equal_to',
463+
values: [identifier],
464+
query_operator: null,
465+
},
466+
],
467+
})) as any;
468+
469+
if (contactByAttr && contactByAttr.payload && contactByAttr.payload.length > 0) {
470+
return contactByAttr.payload[0];
471+
}
472+
473+
// Check inside data property if using axios interceptors wrapper
474+
if (contactByAttr && contactByAttr.data && contactByAttr.data.payload && contactByAttr.data.payload.length > 0) {
475+
return contactByAttr.data.payload[0];
476+
}
477+
478+
return null;
479+
}
480+
418481
public async findContact(instance: InstanceDto, phoneNumber: string) {
419482
const client = await this.clientCw(instance);
420483

@@ -1574,7 +1637,11 @@ export class ChatwootService {
15741637
this.logger.verbose(`Update result: ${result} rows affected`);
15751638

15761639
if (this.isImportHistoryAvailable()) {
1577-
chatwootImport.updateMessageSourceID(chatwootMessageIds.messageId, key.id);
1640+
try {
1641+
await chatwootImport.updateMessageSourceID(chatwootMessageIds.messageId, key.id);
1642+
} catch (error) {
1643+
this.logger.error(`Error updating Chatwoot message source ID: ${error}`);
1644+
}
15781645
}
15791646
}
15801647

@@ -2024,7 +2091,7 @@ export class ChatwootService {
20242091
if (body.key.remoteJid.includes('@g.us')) {
20252092
const participantName = body.pushName;
20262093
const rawPhoneNumber =
2027-
body.key.addressingMode === 'lid' && !body.key.fromMe
2094+
body.key.addressingMode === 'lid' && !body.key.fromMe && body.key.participantAlt
20282095
? body.key.participantAlt.split('@')[0].split(':')[0]
20292096
: body.key.participant.split('@')[0].split(':')[0];
20302097
const formattedPhoneNumber = parsePhoneNumberFromString(`+${rawPhoneNumber}`).formatInternational();
@@ -2206,7 +2273,7 @@ export class ChatwootService {
22062273
if (body.key.remoteJid.includes('@g.us')) {
22072274
const participantName = body.pushName;
22082275
const rawPhoneNumber =
2209-
body.key.addressingMode === 'lid' && !body.key.fromMe
2276+
body.key.addressingMode === 'lid' && !body.key.fromMe && body.key.participantAlt
22102277
? body.key.participantAlt.split('@')[0].split(':')[0]
22112278
: body.key.participant.split('@')[0].split(':')[0];
22122279
const formattedPhoneNumber = parsePhoneNumberFromString(`+${rawPhoneNumber}`).formatInternational();
@@ -2465,7 +2532,10 @@ export class ChatwootService {
24652532
}
24662533

24672534
public getNumberFromRemoteJid(remoteJid: string) {
2468-
return remoteJid.replace(/:\d+/, '').split('@')[0];
2535+
if (!remoteJid) {
2536+
return '';
2537+
}
2538+
return remoteJid.replace(/:\d+/, '').replace('@s.whatsapp.net', '').replace('@g.us', '').replace('@lid', '');
24692539
}
24702540

24712541
public startImportHistoryMessages(instance: InstanceDto) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
327327
if (message.type === 'image') {
328328
await instance.mediaMessage(
329329
{
330-
number: session.remoteJid.split('@')[0],
330+
number: session.remoteJid,
331331
delay: settings?.delayMessage || 1000,
332332
mediatype: 'image',
333333
media: message.content.url,
@@ -342,7 +342,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
342342
if (message.type === 'video') {
343343
await instance.mediaMessage(
344344
{
345-
number: session.remoteJid.split('@')[0],
345+
number: session.remoteJid,
346346
delay: settings?.delayMessage || 1000,
347347
mediatype: 'video',
348348
media: message.content.url,
@@ -357,7 +357,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
357357
if (message.type === 'audio') {
358358
await instance.audioWhatsapp(
359359
{
360-
number: session.remoteJid.split('@')[0],
360+
number: session.remoteJid,
361361
delay: settings?.delayMessage || 1000,
362362
encoding: true,
363363
audio: message.content.url,
@@ -441,7 +441,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
441441
*/
442442
private async processListMessage(instance: any, formattedText: string, remoteJid: string) {
443443
const listJson = {
444-
number: remoteJid.split('@')[0],
444+
number: remoteJid,
445445
title: '',
446446
description: '',
447447
buttonText: '',
@@ -490,7 +490,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
490490
*/
491491
private async processButtonMessage(instance: any, formattedText: string, remoteJid: string) {
492492
const buttonJson = {
493-
number: remoteJid.split('@')[0],
493+
number: remoteJid,
494494
thumbnailUrl: undefined,
495495
title: '',
496496
description: '',

0 commit comments

Comments
 (0)