From 3fa9d103dfd2357899d7ce5b7be14007db75fd38 Mon Sep 17 00:00:00 2001 From: Victor Calazans Date: Sat, 26 Jul 2025 17:36:06 -0300 Subject: [PATCH] =?UTF-8?q?Novas=20corre=C3=A7=C3=B5es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whatsapp/whatsapp.baileys.service.ts | 59 +++++++++++++++++-- src/utils/onWhatsappCache.ts | 14 +++-- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index be075a38..ef5ab8ca 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -138,6 +138,7 @@ import Long from 'long'; import mimeTypes from 'mime-types'; import NodeCache from 'node-cache'; import cron from 'node-cron'; +import dayjs from 'dayjs'; import { release } from 'os'; import { join } from 'path'; import P from 'pino'; @@ -173,6 +174,30 @@ function normalizeJid(jid: string): string { return jid; } +// Function to clear corrupted session data +async function clearCorruptedSessionData(instanceId: string, baileysCache: CacheService) { + try { + // Clear all baileys cache for this instance + await baileysCache.deleteAll(instanceId); + + // Clear session-related cache patterns + const patterns = [ + `${instanceId}_*`, + `*${instanceId}*`, + `*session*${instanceId}*`, + `*prekey*${instanceId}*` + ]; + + for (const pattern of patterns) { + await baileysCache.deleteAll(pattern); + } + + console.log(`Cleared corrupted session data for instance: ${instanceId}`); + } catch (error) { + console.error('Error clearing session data:', error); + } +} + // Adicione a função getVideoDuration no início do arquivo async function getVideoDuration(input: Buffer | string | Readable): Promise { const MediaInfoFactory = (await import('mediainfo.js')).default; @@ -675,6 +700,9 @@ export class BaileysStartupService extends ChannelStartupService { this.endSession = false; + // Clear any corrupted session data before connecting + await clearCorruptedSessionData(this.instanceId, this.baileysCache); + this.client = makeWASocket(socketConfig); if (this.localSettings.wavoipToken && this.localSettings.wavoipToken.length > 0) { @@ -3188,12 +3216,35 @@ export class BaileysStartupService extends ChannelStartupService { const cachedNumbers = await getOnWhatsappCache(numbersToVerify); console.log('cachedNumbers', cachedNumbers); - const filteredNumbers = numbersToVerify.filter( - (jid) => !cachedNumbers.some((cached) => cached.jidOptions.includes(jid)), - ); + // Filter numbers that are not cached OR should be re-verified + const filteredNumbers = numbersToVerify.filter((jid) => { + const cached = cachedNumbers.find((cached) => cached.jidOptions.includes(jid)); + // If not cached, we should verify + if (!cached) return true; + + // For Brazilian numbers, force verification if both formats exist in cache + // to ensure we're using the correct format + const isBrazilian = jid.startsWith('55') && jid.includes('@s.whatsapp.net'); + if (isBrazilian) { + const numberPart = jid.replace('@s.whatsapp.net', ''); + const hasDigit9 = numberPart.length === 13 && numberPart.slice(4, 5) === '9'; + const altFormat = hasDigit9 + ? numberPart.slice(0, 4) + numberPart.slice(5) + : numberPart.slice(0, 4) + '9' + numberPart.slice(4); + const altJid = altFormat + '@s.whatsapp.net'; + + // If both formats exist in cache, prefer the one with 9 + const altCached = cachedNumbers.find((c) => c.jidOptions.includes(altJid)); + if (cached && altCached && !hasDigit9) { + return true; // Force verification to get the correct format + } + } + + return false; // Use cached result + }); console.log('filteredNumbers', filteredNumbers); - const verify = await this.client.onWhatsApp(...filteredNumbers); + const verify = filteredNumbers.length > 0 ? await this.client.onWhatsApp(...filteredNumbers) : []; console.log('verify', verify); normalVerifiedUsers = await Promise.all( normalUsers.map(async (user) => { diff --git a/src/utils/onWhatsappCache.ts b/src/utils/onWhatsappCache.ts index 68f88ba4..a82b96b8 100644 --- a/src/utils/onWhatsappCache.ts +++ b/src/utils/onWhatsappCache.ts @@ -11,14 +11,16 @@ function getAvailableNumbers(remoteJid: string) { const [number, domain] = remoteJid.split('@'); - // Brazilian numbers + // Brazilian numbers - prioritize format with 9 if (remoteJid.startsWith('55')) { const numberWithDigit = number.slice(4, 5) === '9' && number.length === 13 ? number : `${number.slice(0, 4)}9${number.slice(4)}`; const numberWithoutDigit = number.length === 12 ? number : number.slice(0, 4) + number.slice(5); - numbersAvailable.push(numberWithDigit); - numbersAvailable.push(numberWithoutDigit); + // Add the format WITH 9 first (prioritized) + numbersAvailable.push(`${numberWithDigit}@${domain || 's.whatsapp.net'}`); + // Add the format WITHOUT 9 second (fallback) + numbersAvailable.push(`${numberWithoutDigit}@${domain || 's.whatsapp.net'}`); } // Mexican/Argentina numbers @@ -38,8 +40,8 @@ function getAvailableNumbers(remoteJid: string) { : `${number.slice(0, 2)}${prefix}${number.slice(2)}`; const numberWithoutDigit = number.length === 12 ? number : number.slice(0, 2) + number.slice(3); - numbersAvailable.push(numberWithDigit); - numbersAvailable.push(numberWithoutDigit); + numbersAvailable.push(`${numberWithDigit}@${domain || 's.whatsapp.net'}`); + numbersAvailable.push(`${numberWithoutDigit}@${domain || 's.whatsapp.net'}`); } // Other countries @@ -47,7 +49,7 @@ function getAvailableNumbers(remoteJid: string) { numbersAvailable.push(remoteJid); } - return numbersAvailable.map((number) => `${number}@${domain}`); + return numbersAvailable; } interface ISaveOnWhatsappCacheParams {