Novas correções

This commit is contained in:
Victor Calazans 2025-07-26 17:36:06 -03:00
parent c313f5b0b0
commit 3fa9d103df
2 changed files with 63 additions and 10 deletions

View File

@ -138,6 +138,7 @@ import Long from 'long';
import mimeTypes from 'mime-types'; import mimeTypes from 'mime-types';
import NodeCache from 'node-cache'; import NodeCache from 'node-cache';
import cron from 'node-cron'; import cron from 'node-cron';
import dayjs from 'dayjs';
import { release } from 'os'; import { release } from 'os';
import { join } from 'path'; import { join } from 'path';
import P from 'pino'; import P from 'pino';
@ -173,6 +174,30 @@ function normalizeJid(jid: string): string {
return jid; 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 // Adicione a função getVideoDuration no início do arquivo
async function getVideoDuration(input: Buffer | string | Readable): Promise<number> { async function getVideoDuration(input: Buffer | string | Readable): Promise<number> {
const MediaInfoFactory = (await import('mediainfo.js')).default; const MediaInfoFactory = (await import('mediainfo.js')).default;
@ -675,6 +700,9 @@ export class BaileysStartupService extends ChannelStartupService {
this.endSession = false; this.endSession = false;
// Clear any corrupted session data before connecting
await clearCorruptedSessionData(this.instanceId, this.baileysCache);
this.client = makeWASocket(socketConfig); this.client = makeWASocket(socketConfig);
if (this.localSettings.wavoipToken && this.localSettings.wavoipToken.length > 0) { if (this.localSettings.wavoipToken && this.localSettings.wavoipToken.length > 0) {
@ -3188,12 +3216,35 @@ export class BaileysStartupService extends ChannelStartupService {
const cachedNumbers = await getOnWhatsappCache(numbersToVerify); const cachedNumbers = await getOnWhatsappCache(numbersToVerify);
console.log('cachedNumbers', cachedNumbers); console.log('cachedNumbers', cachedNumbers);
const filteredNumbers = numbersToVerify.filter( // Filter numbers that are not cached OR should be re-verified
(jid) => !cachedNumbers.some((cached) => cached.jidOptions.includes(jid)), 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); 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); console.log('verify', verify);
normalVerifiedUsers = await Promise.all( normalVerifiedUsers = await Promise.all(
normalUsers.map(async (user) => { normalUsers.map(async (user) => {

View File

@ -11,14 +11,16 @@ function getAvailableNumbers(remoteJid: string) {
const [number, domain] = remoteJid.split('@'); const [number, domain] = remoteJid.split('@');
// Brazilian numbers // Brazilian numbers - prioritize format with 9
if (remoteJid.startsWith('55')) { if (remoteJid.startsWith('55')) {
const numberWithDigit = const numberWithDigit =
number.slice(4, 5) === '9' && number.length === 13 ? number : `${number.slice(0, 4)}9${number.slice(4)}`; 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); const numberWithoutDigit = number.length === 12 ? number : number.slice(0, 4) + number.slice(5);
numbersAvailable.push(numberWithDigit); // Add the format WITH 9 first (prioritized)
numbersAvailable.push(numberWithoutDigit); numbersAvailable.push(`${numberWithDigit}@${domain || 's.whatsapp.net'}`);
// Add the format WITHOUT 9 second (fallback)
numbersAvailable.push(`${numberWithoutDigit}@${domain || 's.whatsapp.net'}`);
} }
// Mexican/Argentina numbers // Mexican/Argentina numbers
@ -38,8 +40,8 @@ function getAvailableNumbers(remoteJid: string) {
: `${number.slice(0, 2)}${prefix}${number.slice(2)}`; : `${number.slice(0, 2)}${prefix}${number.slice(2)}`;
const numberWithoutDigit = number.length === 12 ? number : number.slice(0, 2) + number.slice(3); const numberWithoutDigit = number.length === 12 ? number : number.slice(0, 2) + number.slice(3);
numbersAvailable.push(numberWithDigit); numbersAvailable.push(`${numberWithDigit}@${domain || 's.whatsapp.net'}`);
numbersAvailable.push(numberWithoutDigit); numbersAvailable.push(`${numberWithoutDigit}@${domain || 's.whatsapp.net'}`);
} }
// Other countries // Other countries
@ -47,7 +49,7 @@ function getAvailableNumbers(remoteJid: string) {
numbersAvailable.push(remoteJid); numbersAvailable.push(remoteJid);
} }
return numbersAvailable.map((number) => `${number}@${domain}`); return numbersAvailable;
} }
interface ISaveOnWhatsappCacheParams { interface ISaveOnWhatsappCacheParams {