Skip to content

Commit 486645f

Browse files
committed
chore: update Baileys dependency to version 7.0.0-rc.3 and improve message key handling in WhatsApp service
1 parent bfe7030 commit 486645f

File tree

7 files changed

+2879
-1458
lines changed

7 files changed

+2879
-1458
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Testing
44

5-
* Baileys Updates: v7.0.0-rc.2 ([Link](https://github.com/WhiskeySockets/Baileys/releases/tag/v7.0.0-rc.2))
5+
* Baileys Updates: v7.0.0-rc.3 ([Link](https://github.com/WhiskeySockets/Baileys/releases/tag/v7.0.0-rc.3))
66

77
# 2.3.2 (2025-09-02)
88

package-lock.json

Lines changed: 2816 additions & 1378 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"amqplib": "^0.10.5",
6161
"audio-decode": "^2.2.3",
6262
"axios": "^1.7.9",
63-
"baileys": "github:WhiskeySockets/Baileys",
63+
"baileys": "^7.0.0-rc.3",
6464
"class-validator": "^0.14.1",
6565
"compression": "^1.7.5",
6666
"cors": "^2.8.5",
@@ -125,7 +125,7 @@
125125
"eslint-plugin-simple-import-sort": "^10.0.0",
126126
"prettier": "^3.4.2",
127127
"tsconfig-paths": "^4.2.0",
128-
"tsx": "^4.20.3",
128+
"tsx": "^4.20.5",
129129
"typescript": "^5.7.2"
130130
}
131131
}

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

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ import { v4 } from 'uuid';
151151
import { BaileysMessageProcessor } from './baileysMessage.processor';
152152
import { useVoiceCallsBaileys } from './voiceCalls/useVoiceCallsBaileys';
153153

154+
export interface ExtendedMessageKey extends WAMessageKey {
155+
senderPn?: string;
156+
previousRemoteJid?: string | null;
157+
}
158+
159+
export interface ExtendedIMessageKey extends proto.IMessageKey {
160+
senderPn?: string;
161+
remoteJidAlt?: string;
162+
participantAlt?: string;
163+
server_id?: string;
164+
isViewOnce?: boolean;
165+
}
166+
154167
const groupMetadataCache = new CacheService(new CacheEngine(configService, 'groups').getEngine());
155168

156169
// Adicione a função getVideoDuration no início do arquivo
@@ -986,8 +999,8 @@ export class BaileysStartupService extends ChannelStartupService {
986999
continue;
9871000
}
9881001

989-
if (m.key.remoteJid?.includes('@lid') && m.key.remoteJidAlt) {
990-
m.key.remoteJid = m.key.remoteJidAlt;
1002+
if (m.key.remoteJid?.includes('@lid') && (m.key as ExtendedIMessageKey).senderPn) {
1003+
m.key.remoteJid = (m.key as ExtendedIMessageKey).senderPn;
9911004
}
9921005

9931006
if (Long.isLong(m?.messageTimestamp)) {
@@ -1052,9 +1065,9 @@ export class BaileysStartupService extends ChannelStartupService {
10521065
) => {
10531066
try {
10541067
for (const received of messages) {
1055-
if (received.key.remoteJid?.includes('@lid') && received.key.remoteJidAlt) {
1056-
(received.key as { previousRemoteJid?: string | null }).previousRemoteJid = received.key.remoteJid;
1057-
received.key.remoteJid = received.key.remoteJidAlt;
1068+
if (received.key.remoteJid?.includes('@lid') && (received.key as ExtendedMessageKey).senderPn) {
1069+
(received.key as ExtendedMessageKey).previousRemoteJid = received.key.remoteJid;
1070+
received.key.remoteJid = (received.key as ExtendedMessageKey).senderPn;
10581071
}
10591072
if (
10601073
received?.messageStubParameters?.some?.((param) =>
@@ -4308,47 +4321,30 @@ export class BaileysStartupService extends ChannelStartupService {
43084321
throw new Error('Method not available in the Baileys service');
43094322
}
43104323

4311-
private sanitizeMessageContent(messageContent: any): any {
4312-
if (!messageContent) return messageContent;
4313-
4314-
// Deep clone and sanitize to avoid modifying original
4315-
return JSON.parse(
4316-
JSON.stringify(messageContent, (key, value) => {
4317-
// Convert Long objects to numbers
4318-
if (Long.isLong(value)) {
4319-
return value.toNumber();
4320-
}
4321-
4322-
// Convert Uint8Array to regular arrays
4323-
if (value instanceof Uint8Array) {
4324-
return Array.from(value);
4325-
}
4324+
private convertLongToNumber(obj: any): any {
4325+
if (obj === null || obj === undefined) {
4326+
return obj;
4327+
}
43264328

4327-
// Remove functions and other non-serializable objects
4328-
if (typeof value === 'function') {
4329-
return undefined;
4330-
}
4329+
if (Long.isLong(obj)) {
4330+
return obj.toNumber();
4331+
}
43314332

4332-
// Handle objects with toJSON method
4333-
if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
4334-
return value.toJSON();
4335-
}
4333+
if (Array.isArray(obj)) {
4334+
return obj.map((item) => this.convertLongToNumber(item));
4335+
}
43364336

4337-
// Handle special objects that might not serialize properly
4338-
if (value && typeof value === 'object') {
4339-
// Check if it's a plain object or has prototype issues
4340-
try {
4341-
JSON.stringify(value);
4342-
return value;
4343-
} catch (e) {
4344-
// If it can't be stringified, return a safe representation
4345-
return '[Non-serializable object]';
4346-
}
4337+
if (typeof obj === 'object') {
4338+
const converted: any = {};
4339+
for (const key in obj) {
4340+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
4341+
converted[key] = this.convertLongToNumber(obj[key]);
43474342
}
4343+
}
4344+
return converted;
4345+
}
43484346

4349-
return value;
4350-
}),
4351-
);
4347+
return obj;
43524348
}
43534349

43544350
private prepareMessage(message: proto.IWebMessageInfo): any {
@@ -4363,11 +4359,11 @@ export class BaileysStartupService extends ChannelStartupService {
43634359
? 'Você'
43644360
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
43654361
status: status[message.status],
4366-
message: this.sanitizeMessageContent({ ...message.message }),
4367-
contextInfo: this.sanitizeMessageContent(contentMsg?.contextInfo),
4362+
message: this.convertLongToNumber({ ...message.message }),
4363+
contextInfo: this.convertLongToNumber(contentMsg?.contextInfo),
43684364
messageType: contentType || 'unknown',
43694365
messageTimestamp: Long.isLong(message.messageTimestamp)
4370-
? (message.messageTimestamp as Long).toNumber()
4366+
? message.messageTimestamp.toNumber()
43714367
: (message.messageTimestamp as number),
43724368
instanceId: this.instanceId,
43734369
source: getDevice(message.key.id),

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

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { InstanceDto } from '@api/dto/instance.dto';
22
import { Options, Quoted, SendAudioDto, SendMediaDto, SendTextDto } from '@api/dto/sendMessage.dto';
3+
import { ExtendedMessageKey } from '@api/integrations/channel/whatsapp/whatsapp.baileys.service';
34
import { ChatwootDto } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto';
45
import { postgresClient } from '@api/integrations/chatbot/chatwoot/libs/postgres.client';
56
import { chatwootImport } from '@api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper';
@@ -1284,12 +1285,7 @@ export class ChatwootService {
12841285
});
12851286

12861287
if (message) {
1287-
const key = message.key as {
1288-
id: string;
1289-
remoteJid: string;
1290-
fromMe: boolean;
1291-
participant: string;
1292-
};
1288+
const key = message.key as ExtendedMessageKey;
12931289

12941290
await waInstance?.client.sendMessage(key.remoteJid, { delete: key });
12951291

@@ -1487,12 +1483,7 @@ export class ChatwootService {
14871483
},
14881484
});
14891485
if (lastMessage && !lastMessage.chatwootIsRead) {
1490-
const key = lastMessage.key as {
1491-
id: string;
1492-
fromMe: boolean;
1493-
remoteJid: string;
1494-
participant?: string;
1495-
};
1486+
const key = lastMessage.key as ExtendedMessageKey;
14961487

14971488
waInstance?.markMessageAsRead({
14981489
readMessages: [
@@ -1550,12 +1541,7 @@ export class ChatwootService {
15501541
chatwootMessageIds: ChatwootMessage,
15511542
instance: InstanceDto,
15521543
) {
1553-
const key = message.key as {
1554-
id: string;
1555-
fromMe: boolean;
1556-
remoteJid: string;
1557-
participant?: string;
1558-
};
1544+
const key = message.key as ExtendedMessageKey;
15591545

15601546
if (!chatwootMessageIds.messageId || !key?.id) {
15611547
return;
@@ -1623,12 +1609,7 @@ export class ChatwootService {
16231609
},
16241610
});
16251611

1626-
const key = message?.key as {
1627-
id: string;
1628-
fromMe: boolean;
1629-
remoteJid: string;
1630-
participant?: string;
1631-
};
1612+
const key = message?.key as ExtendedMessageKey;
16321613

16331614
if (message && key?.id) {
16341615
return {
@@ -2258,12 +2239,13 @@ export class ChatwootService {
22582239
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
22592240
}\n\n_\`${i18next.t('cw.message.edited')}.\`_`;
22602241
const message = await this.getMessageByKeyId(instance, body?.key?.id);
2261-
const key = message.key as {
2262-
id: string;
2263-
fromMe: boolean;
2264-
remoteJid: string;
2265-
participant?: string;
2266-
};
2242+
2243+
if (!message) {
2244+
this.logger.warn('Message not found for edit event');
2245+
return;
2246+
}
2247+
2248+
const key = message.key as ExtendedMessageKey;
22672249

22682250
const messageType = key?.fromMe ? 'outgoing' : 'incoming';
22692251

@@ -2541,7 +2523,7 @@ export class ChatwootService {
25412523
const savedMessages = await this.prismaRepository.message.findMany({
25422524
where: {
25432525
Instance: { name: instance.instanceName },
2544-
messageTimestamp: { gte: dayjs().subtract(6, 'hours').unix() },
2526+
messageTimestamp: { gte: Number(dayjs().subtract(6, 'hours').unix()) },
25452527
AND: ids.map((id) => ({ key: { path: ['id'], not: id } })),
25462528
},
25472529
});

src/api/services/monitor.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class WAMonitoringService {
2929

3030
Object.assign(this.db, configService.get<Database>('DATABASE'));
3131
Object.assign(this.redis, configService.get<CacheConf>('CACHE'));
32+
33+
(this as any).providerSession = Object.freeze(configService.get<ProviderSession>('PROVIDER'));
3234
}
3335

3436
private readonly db: Partial<Database> = {};
@@ -37,7 +39,7 @@ export class WAMonitoringService {
3739
private readonly logger = new Logger('WAMonitoringService');
3840
public readonly waInstances: Record<string, any> = {};
3941

40-
private readonly providerSession = Object.freeze(this.configService.get<ProviderSession>('PROVIDER'));
42+
private readonly providerSession: ProviderSession;
4143

4244
public delInstanceTime(instance: string) {
4345
const time = this.configService.get<DelInstance>('DEL_INSTANCE');

src/utils/i18n.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import fs from 'fs';
33
import i18next from 'i18next';
44
import path from 'path';
55

6+
const __dirname = path.resolve(process.cwd(), 'src', 'utils');
7+
68
const languages = ['en', 'pt-BR', 'es'];
79
const translationsPath = path.join(__dirname, 'translations');
810
const configService: ConfigService = new ConfigService();
@@ -12,8 +14,9 @@ const resources: any = {};
1214
languages.forEach((language) => {
1315
const languagePath = path.join(translationsPath, `${language}.json`);
1416
if (fs.existsSync(languagePath)) {
17+
const translationContent = fs.readFileSync(languagePath, 'utf8');
1518
resources[language] = {
16-
translation: require(languagePath),
19+
translation: JSON.parse(translationContent),
1720
};
1821
}
1922
});

0 commit comments

Comments
 (0)