From fff11ea4527fc204218d02c894f60a74b2ccbcb9 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 22 Aug 2024 23:08:42 -0300 Subject: [PATCH] fix: update baileys version --- CHANGELOG.md | 1 + package.json | 2 +- src/api/dto/chat.dto.ts | 11 ++- .../baileys/whatsapp.baileys.service.ts | 96 ++++--------------- 4 files changed, 29 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d741dee1..316a20a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * Refactor integrations structure for modular system * Fixed dify agent integration +* Update Baileys Version # 2.0.10 (2024-08-16 16:23) diff --git a/package.json b/package.json index c8e42e0b..8d9fbace 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "@sentry/profiling-node": "^8.26.0", "amqplib": "^0.10.3", "axios": "^1.6.5", - "baileys": "6.7.5", + "baileys": "6.7.7", "class-validator": "^0.14.1", "compression": "^1.7.4", "cors": "^2.8.5", diff --git a/src/api/dto/chat.dto.ts b/src/api/dto/chat.dto.ts index fc2ff5d3..00da7fdd 100644 --- a/src/api/dto/chat.dto.ts +++ b/src/api/dto/chat.dto.ts @@ -1,4 +1,11 @@ -import { proto, WAPresence, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from 'baileys'; +import { + proto, + WAPresence, + WAPrivacyGroupAddValue, + WAPrivacyOnlineValue, + WAPrivacyValue, + WAReadReceiptsValue, +} from 'baileys'; export class OnWhatsAppDto { constructor( @@ -84,7 +91,7 @@ export class PrivacySettingDto { status: WAPrivacyValue; online: WAPrivacyOnlineValue; last: WAPrivacyValue; - groupadd: WAPrivacyValue; + groupadd: WAPrivacyGroupAddValue; } export class DeleteMessage { diff --git a/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts index 48be6750..c8d6607f 100644 --- a/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts @@ -92,10 +92,10 @@ import makeWASocket, { getContentType, getDevice, GroupMetadata, - GroupParticipant, + // GroupParticipant, isJidBroadcast, isJidGroup, - // isJidNewsletter, + isJidNewsletter, isJidUser, makeCacheableSignalKeyStore, MessageUpsertType, @@ -144,7 +144,6 @@ export class BaileysStartupService extends ChannelStartupService { ) { super(configService, eventEmitter, prismaRepository, chatwootCache); this.instance.qrcode = { count: 0 }; - // this.recoveringMessages(); this.authStateProvider = new AuthStateProvider(this.providerFiles); } @@ -159,58 +158,6 @@ export class BaileysStartupService extends ChannelStartupService { public phoneNumber: string; - // private async recoveringMessages() { - // const cacheConf = this.configService.get('CACHE'); - - // if ((cacheConf?.REDIS?.ENABLED && cacheConf?.REDIS?.URI !== '') || cacheConf?.LOCAL?.ENABLED) { - // this.logger.info('Recovering messages lost from cache'); - // setInterval(async () => { - // this.baileysCache.keys().then((keys) => { - // keys.forEach(async (key) => { - // const data = await this.baileysCache.get(key.split(':')[2]); - - // let message: any; - // let retry: number; - - // if (!data?.message) { - // message = data; - // retry = 0; - // } else { - // message = data.message; - // retry = data.retry; - // } - - // if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node') { - // retry = retry + 1; - // this.logger.info(`Message absent from node, retrying to send, key: ${key.split(':')[2]} retry: ${retry}`); - // if (message.messageStubParameters[1]) { - // await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver)); - // } - - // this.baileysCache.set(key.split(':')[2], { message, retry }); - - // if (retry >= 100) { - // this.logger.warn(`Message absent from node, retry limit reached, key: ${key.split(':')[2]}`); - // this.baileysCache.delete(key.split(':')[2]); - // return; - // } - // } - // }); - // }); - // // 15 minutes - // }, 15 * 60 * 1000); - // } - // } - - // private async forceUpdateGroupMetadataCache() { - // this.logger.verbose('Force update group metadata cache'); - // const groups = await this.fetchAllGroups({ getParticipants: 'false' }); - - // for (const group of groups) { - // await this.updateGroupMetadataCache(group.id); - // } - // } - public get connectionStatus() { return this.stateConnection; } @@ -418,17 +365,6 @@ export class BaileysStartupService extends ChannelStartupService { } } - // if (connection === 'connecting') { - // if (this.configService.get('DATABASE').ENABLED) { - // await this.prismaRepository.instance.update({ - // where: { id: this.instanceId }, - // data: { - // connectionStatus: 'connecting', - // }, - // }); - // } - // } - if (connection === 'open') { this.instance.wuid = this.client.user.id.replace(/:\d+/, ''); this.instance.profilePictureUrl = (await this.profilePicture(this.instance.wuid)).profilePictureUrl; @@ -625,8 +561,8 @@ export class BaileysStartupService extends ChannelStartupService { shouldIgnoreJid: (jid) => { const isGroupJid = this.localSettings.groupsIgnore && isJidGroup(jid); const isBroadcast = !this.localSettings.readStatus && isJidBroadcast(jid); - // const isNewsletter = !isJidNewsletter(jid); - const isNewsletter = jid && jid.includes('newsletter'); + const isNewsletter = isJidNewsletter(jid); + // const isNewsletter = jid && jid.includes('newsletter'); return isGroupJid || isBroadcast || isNewsletter; }, @@ -634,7 +570,7 @@ export class BaileysStartupService extends ChannelStartupService { shouldSyncHistoryMessage: (msg: proto.Message.IHistorySyncNotification) => { return this.historySyncNotification(msg); }, - // cachedGroupMetadata: this.getGroupMetadataCache, + cachedGroupMetadata: this.getGroupMetadataCache, userDevicesCache: this.userDevicesCache, transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 }, }; @@ -798,6 +734,10 @@ export class BaileysStartupService extends ChannelStartupService { const findParticipant = await this.chatwootService.findContact(instance, contact.remoteJid.split('@')[0]); + if (!findParticipant) { + return; + } + this.chatwootService.updateContact(instance, findParticipant.id, { name: contact.pushName, avatar_url: contact.profilePicUrl, @@ -1024,15 +964,15 @@ export class BaileysStartupService extends ChannelStartupService { if (received.message?.conversation || received.message?.extendedTextMessage?.text) { const text = received.message?.conversation || received.message?.extendedTextMessage?.text; if (text == 'requestPlaceholder' && !requestId) { - // const messageId = await this.client.requestPlaceholderResend(received.key); - // console.log('requested placeholder resync, id=', messageId); + const messageId = await this.client.requestPlaceholderResend(received.key); + console.log('requested placeholder resync, id=', messageId); } else if (requestId) { console.log('Message received from phone, id=', requestId, received); } if (text == 'onDemandHistSync') { - // const messageId = await this.client.fetchMessageHistory(50, received.key, received.messageTimestamp!); - // console.log('requested on-demand sync, id=', messageId); + const messageId = await this.client.fetchMessageHistory(50, received.key, received.messageTimestamp!); + console.log('requested on-demand sync, id=', messageId); } } @@ -1737,7 +1677,7 @@ export class BaileysStartupService extends ChannelStartupService { quoted: any, messageId?: string, ephemeralExpiration?: number, - participants?: GroupParticipant[], + // participants?: GroupParticipant[], ) { const option: any = { quoted, @@ -1745,10 +1685,10 @@ export class BaileysStartupService extends ChannelStartupService { if (isJidGroup(sender)) { option.useCachedGroupMetadata = true; - if (participants) - option.cachedGroupMetadata = async () => { - return { participants: participants as GroupParticipant[] }; - }; + // if (participants) + // option.cachedGroupMetadata = async () => { + // return { participants: participants as GroupParticipant[] }; + // }; } if (ephemeralExpiration) option.ephemeralExpiration = ephemeralExpiration;