Skip to content

Commit c7b7a99

Browse files
Merge pull request #2319 from Vitordotpy/fix/remotejid-wrong-format
fix(baileys): normalize remote JIDs for consistent database lookups
2 parents 6f2bef6 + f46699e commit c7b7a99

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

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

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,10 +1201,10 @@ export class BaileysStartupService extends ChannelStartupService {
12011201
}
12021202
}
12031203

1204-
const messageRaw = this.prepareMessage(received);
1204+
const messageRaw = this.prepareMessage(received) as any;
12051205

12061206
if (messageRaw.messageType === 'pollUpdateMessage') {
1207-
const pollCreationKey = messageRaw.message.pollUpdateMessage.pollCreationMessageKey;
1207+
const pollCreationKey = (messageRaw.message as any).pollUpdateMessage.pollCreationMessageKey;
12081208
const pollMessage = (await this.getMessage(pollCreationKey, true)) as proto.IWebMessageInfo;
12091209
const pollMessageSecret = (await this.getMessage(pollCreationKey)) as any;
12101210

@@ -1213,7 +1213,7 @@ export class BaileysStartupService extends ChannelStartupService {
12131213
(pollMessage.message as any).pollCreationMessage?.options ||
12141214
(pollMessage.message as any).pollCreationMessageV3?.options ||
12151215
[];
1216-
const pollVote = messageRaw.message.pollUpdateMessage.vote;
1216+
const pollVote = (messageRaw.message as any).pollUpdateMessage.vote;
12171217

12181218
const voterJid = received.key.fromMe
12191219
? this.instance.wuid
@@ -1293,14 +1293,14 @@ export class BaileysStartupService extends ChannelStartupService {
12931293
})
12941294
.map((option) => option.optionName);
12951295

1296-
messageRaw.message.pollUpdateMessage.vote.selectedOptions = selectedOptionNames;
1296+
(messageRaw.message as any).pollUpdateMessage.vote.selectedOptions = selectedOptionNames;
12971297

12981298
const pollUpdates = pollOptions.map((option) => ({
12991299
name: option.optionName,
13001300
voters: selectedOptionNames.includes(option.optionName) ? [successfulVoterJid] : [],
13011301
}));
13021302

1303-
messageRaw.pollUpdates = pollUpdates;
1303+
(messageRaw as any).pollUpdates = pollUpdates;
13041304
}
13051305
}
13061306

@@ -1348,13 +1348,14 @@ export class BaileysStartupService extends ChannelStartupService {
13481348
});
13491349

13501350
if (openAiDefaultSettings && openAiDefaultSettings.openaiCredsId && openAiDefaultSettings.speechToText) {
1351-
messageRaw.message.speechToText = `[audio] ${await this.openaiService.speechToText(received, this)}`;
1351+
(messageRaw.message as any).speechToText =
1352+
`[audio] ${await this.openaiService.speechToText(received, this)}`;
13521353
}
13531354
}
13541355

13551356
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
13561357
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1357-
const { pollUpdates, ...messageData } = messageRaw;
1358+
const { pollUpdates, ...messageData } = messageRaw as any;
13581359
const msg = await this.prismaRepository.message.create({ data: messageData });
13591360

13601361
const { remoteJid } = received.key;
@@ -1430,7 +1431,7 @@ export class BaileysStartupService extends ChannelStartupService {
14301431

14311432
const mediaUrl = await s3Service.getObjectUrl(fullName);
14321433

1433-
messageRaw.message.mediaUrl = mediaUrl;
1434+
(messageRaw.message as any).mediaUrl = mediaUrl;
14341435

14351436
await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw });
14361437
}
@@ -1452,7 +1453,7 @@ export class BaileysStartupService extends ChannelStartupService {
14521453
);
14531454

14541455
if (buffer) {
1455-
messageRaw.message.base64 = buffer.toString('base64');
1456+
(messageRaw.message as any).base64 = buffer.toString('base64');
14561457
} else {
14571458
// retry to download media
14581459
const buffer = await downloadMediaMessage(
@@ -1463,7 +1464,7 @@ export class BaileysStartupService extends ChannelStartupService {
14631464
);
14641465

14651466
if (buffer) {
1466-
messageRaw.message.base64 = buffer.toString('base64');
1467+
(messageRaw.message as any).base64 = buffer.toString('base64');
14671468
}
14681469
}
14691470
} catch (error) {
@@ -1475,16 +1476,16 @@ export class BaileysStartupService extends ChannelStartupService {
14751476
this.logger.verbose(messageRaw);
14761477

14771478
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
1478-
if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) {
1479-
messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt;
1479+
if ((messageRaw.key as any).remoteJid?.includes('@lid') && (messageRaw.key as any).remoteJidAlt) {
1480+
(messageRaw.key as any).remoteJid = (messageRaw.key as any).remoteJidAlt;
14801481
}
14811482
console.log(messageRaw);
14821483

14831484
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
14841485

14851486
await chatbotController.emit({
14861487
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
1487-
remoteJid: messageRaw.key.remoteJid,
1488+
remoteJid: (messageRaw.key as any).remoteJid,
14881489
msg: messageRaw,
14891490
pushName: messageRaw.pushName,
14901491
});
@@ -1513,9 +1514,11 @@ export class BaileysStartupService extends ChannelStartupService {
15131514
await saveOnWhatsappCache([
15141515
{
15151516
remoteJid:
1516-
messageRaw.key.addressingMode === 'lid' ? messageRaw.key.remoteJidAlt : messageRaw.key.remoteJid,
1517-
remoteJidAlt: messageRaw.key.remoteJidAlt,
1518-
lid: messageRaw.key.addressingMode === 'lid' ? 'lid' : null,
1517+
(messageRaw.key as any).addressingMode === 'lid'
1518+
? (messageRaw.key as any).remoteJidAlt
1519+
: (messageRaw.key as any).remoteJid,
1520+
remoteJidAlt: (messageRaw.key as any).remoteJidAlt,
1521+
lid: (messageRaw.key as any).addressingMode === 'lid' ? 'lid' : null,
15191522
},
15201523
]);
15211524
}
@@ -1561,7 +1564,11 @@ export class BaileysStartupService extends ChannelStartupService {
15611564
const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}
15621565

15631566
for await (const { key, update } of args) {
1564-
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
1567+
const keyAny = key as any;
1568+
const normalizedRemoteJid = keyAny.remoteJid?.replace(/:.*$/, '');
1569+
const normalizedParticipant = keyAny.participant?.replace(/:.*$/, '');
1570+
1571+
if (settings?.groupsIgnore && normalizedRemoteJid?.includes('@g.us')) {
15651572
continue;
15661573
}
15671574

@@ -1612,9 +1619,9 @@ export class BaileysStartupService extends ChannelStartupService {
16121619

16131620
const message: any = {
16141621
keyId: key.id,
1615-
remoteJid: key?.remoteJid,
1622+
remoteJid: normalizedRemoteJid,
16161623
fromMe: key.fromMe,
1617-
participant: key?.participant,
1624+
participant: normalizedParticipant,
16181625
status: status[update.status] ?? 'SERVER_ACK',
16191626
pollUpdates,
16201627
instanceId: this.instanceId,
@@ -2422,7 +2429,7 @@ export class BaileysStartupService extends ChannelStartupService {
24222429
messageSent.messageTimestamp = messageSent.messageTimestamp?.toNumber();
24232430
}
24242431

2425-
const messageRaw = this.prepareMessage(messageSent);
2432+
const messageRaw = this.prepareMessage(messageSent) as any;
24262433

24272434
const isMedia =
24282435
messageSent?.message?.imageMessage ||
@@ -2444,14 +2451,15 @@ export class BaileysStartupService extends ChannelStartupService {
24442451
);
24452452
}
24462453

2447-
if (this.configService.get<Openai>('OPENAI').ENABLED && messageRaw?.message?.audioMessage) {
2454+
if (this.configService.get<Openai>('OPENAI').ENABLED && (messageRaw as any)?.message?.audioMessage) {
24482455
const openAiDefaultSettings = await this.prismaRepository.openaiSetting.findFirst({
24492456
where: { instanceId: this.instanceId },
24502457
include: { OpenaiCreds: true },
24512458
});
24522459

24532460
if (openAiDefaultSettings && openAiDefaultSettings.openaiCredsId && openAiDefaultSettings.speechToText) {
2454-
messageRaw.message.speechToText = `[audio] ${await this.openaiService.speechToText(messageRaw, this)}`;
2461+
(messageRaw.message as any).speechToText =
2462+
`[audio] ${await this.openaiService.speechToText(messageRaw, this)}`;
24552463
}
24562464
}
24572465

@@ -4662,26 +4670,28 @@ export class BaileysStartupService extends ChannelStartupService {
46624670
return obj;
46634671
}
46644672

4665-
private prepareMessage(message: proto.IWebMessageInfo): any {
4666-
const contentType = getContentType(message.message);
4667-
const contentMsg = message?.message[contentType] as any;
4668-
4669-
const messageRaw = {
4670-
key: message.key, // Save key exactly as it comes from Baileys
4673+
private prepareMessage(message: WAMessage): Message {
4674+
const keyAny = message.key as any;
4675+
const messageRaw: any = {
4676+
key: {
4677+
...message.key,
4678+
remoteJid: keyAny.remoteJid?.replace(/:.*$/, ''),
4679+
participant: keyAny.participant?.replace(/:.*$/, ''),
4680+
},
46714681
pushName:
46724682
message.pushName ||
46734683
(message.key.fromMe
46744684
? 'Você'
46754685
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
4676-
status: status[message.status],
46774686
message: this.deserializeMessageBuffers({ ...message.message }),
4678-
contextInfo: this.deserializeMessageBuffers(contentMsg?.contextInfo),
4679-
messageType: contentType || 'unknown',
4687+
messageType: getContentType(message.message),
46804688
messageTimestamp: Long.isLong(message.messageTimestamp)
46814689
? message.messageTimestamp.toNumber()
46824690
: (message.messageTimestamp as number),
4691+
source: getDevice(keyAny.id),
46834692
instanceId: this.instanceId,
4684-
source: getDevice(message.key.id),
4693+
status: status[message.status],
4694+
contextInfo: this.deserializeMessageBuffers(message.message?.messageContextInfo),
46854695
};
46864696

46874697
if (!messageRaw.status && message.key.fromMe === false) {

0 commit comments

Comments
 (0)