mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-27 07:37:44 -06:00
Compare commits
7 Commits
c7b7a9992e
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6efa879081 | ||
|
|
2534ec2307 | ||
|
|
933a28de26 | ||
|
|
b1b07b7e7f | ||
|
|
bb831d590f | ||
|
|
cb41e65e29 | ||
|
|
52a8d9ea71 |
@@ -0,0 +1,3 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Instance` MODIFY `token` VARCHAR(500);
|
||||
|
||||
@@ -70,7 +70,7 @@ model Instance {
|
||||
integration String? @db.VarChar(100)
|
||||
number String? @db.VarChar(100)
|
||||
businessId String? @db.VarChar(100)
|
||||
token String? @db.VarChar(255)
|
||||
token String? @db.VarChar(500)
|
||||
clientName String? @db.VarChar(100)
|
||||
disconnectionReasonCode Int? @db.Int
|
||||
disconnectionObject Json? @db.Json
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "Instance" ALTER COLUMN "token" TYPE VARCHAR(500);
|
||||
|
||||
@@ -70,7 +70,7 @@ model Instance {
|
||||
integration String? @db.VarChar(100)
|
||||
number String? @db.VarChar(100)
|
||||
businessId String? @db.VarChar(100)
|
||||
token String? @db.VarChar(255)
|
||||
token String? @db.VarChar(500)
|
||||
clientName String? @db.VarChar(100)
|
||||
disconnectionReasonCode Int? @db.Integer
|
||||
disconnectionObject Json? @db.JsonB
|
||||
|
||||
@@ -71,7 +71,7 @@ model Instance {
|
||||
integration String? @db.VarChar(100)
|
||||
number String? @db.VarChar(100)
|
||||
businessId String? @db.VarChar(100)
|
||||
token String? @db.VarChar(255)
|
||||
token String? @db.VarChar(500)
|
||||
clientName String? @db.VarChar(100)
|
||||
disconnectionReasonCode Int? @db.Integer
|
||||
disconnectionObject Json? @db.JsonB
|
||||
|
||||
@@ -249,6 +249,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
|
||||
private readonly userDevicesCache: CacheStore = new NodeCache({ stdTTL: 300000, useClones: false });
|
||||
private endSession = false;
|
||||
private isDeleting = false; // Flag to prevent reconnection during deletion
|
||||
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
||||
private eventProcessingQueue: Promise<void> = Promise.resolve();
|
||||
|
||||
@@ -265,10 +266,27 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
}
|
||||
|
||||
public async logoutInstance() {
|
||||
this.messageProcessor.onDestroy();
|
||||
await this.client?.logout('Log out instance: ' + this.instanceName);
|
||||
// Mark instance as deleting to prevent reconnection attempts
|
||||
this.isDeleting = true;
|
||||
this.endSession = true;
|
||||
|
||||
this.client?.ws?.close();
|
||||
this.messageProcessor.onDestroy();
|
||||
|
||||
if (this.client) {
|
||||
try {
|
||||
await this.client.logout('Log out instance: ' + this.instanceName);
|
||||
} catch (error) {
|
||||
this.logger.error({ message: 'Error during logout', error });
|
||||
}
|
||||
|
||||
// Improved socket cleanup
|
||||
try {
|
||||
this.client.ws?.close();
|
||||
this.client.end(new Error('Instance logout'));
|
||||
} catch (error) {
|
||||
this.logger.error({ message: 'Error during socket cleanup', error });
|
||||
}
|
||||
}
|
||||
|
||||
const db = this.configService.get<Database>('DATABASE');
|
||||
const cache = this.configService.get<CacheConf>('CACHE');
|
||||
@@ -332,6 +350,18 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
}
|
||||
|
||||
private async connectionUpdate({ qr, connection, lastDisconnect }: Partial<ConnectionState>) {
|
||||
// Enhanced logging for connection updates
|
||||
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
|
||||
this.logger.info({
|
||||
message: 'Connection update received',
|
||||
connection,
|
||||
hasQr: !!qr,
|
||||
statusCode,
|
||||
instanceName: this.instance.name,
|
||||
isDeleting: this.isDeleting,
|
||||
endSession: this.endSession,
|
||||
});
|
||||
|
||||
if (qr) {
|
||||
if (this.instance.qrcode.count === this.configService.get<QrCode>('QRCODE').LIMIT) {
|
||||
this.sendDataWebhook(Events.QRCODE_UPDATED, {
|
||||
@@ -424,11 +454,29 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
}
|
||||
|
||||
if (connection === 'close') {
|
||||
// Check if instance is being deleted or session is ending
|
||||
if (this.isDeleting || this.endSession) {
|
||||
this.logger.info('Instance is being deleted/ended, skipping reconnection attempt');
|
||||
return;
|
||||
}
|
||||
|
||||
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
|
||||
const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];
|
||||
const shouldReconnect = !codesToNotReconnect.includes(statusCode);
|
||||
|
||||
this.logger.info({
|
||||
message: 'Connection closed, evaluating reconnection',
|
||||
statusCode,
|
||||
shouldReconnect,
|
||||
instanceName: this.instance.name,
|
||||
});
|
||||
|
||||
if (shouldReconnect) {
|
||||
await this.connectToWhatsapp(this.phoneNumber);
|
||||
// Add 3 second delay before reconnection to prevent rapid reconnection loops
|
||||
this.logger.info('Reconnecting in 3 seconds...');
|
||||
setTimeout(async () => {
|
||||
await this.connectToWhatsapp(this.phoneNumber);
|
||||
}, 3000);
|
||||
} else {
|
||||
this.sendDataWebhook(Events.STATUS_INSTANCE, {
|
||||
instance: this.instance.name,
|
||||
@@ -591,10 +639,11 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
this.logger.info(`Browser: ${browser}`);
|
||||
}
|
||||
|
||||
// Fetch latest WhatsApp Web version automatically
|
||||
const baileysVersion = await fetchLatestWaWebVersion({});
|
||||
const version = baileysVersion.version;
|
||||
const log = `Baileys version: ${version.join('.')}`;
|
||||
|
||||
const log = `Baileys version: ${version.join('.')}`;
|
||||
this.logger.info(log);
|
||||
|
||||
this.logger.info(`Group Ignore: ${this.localSettings.groupsIgnore}`);
|
||||
@@ -602,7 +651,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
let options;
|
||||
|
||||
if (this.localProxy?.enabled) {
|
||||
this.logger.info('Proxy enabled: ' + this.localProxy?.host);
|
||||
this.logger.verbose('Proxy enabled');
|
||||
|
||||
if (this.localProxy?.host?.includes('proxyscrape')) {
|
||||
try {
|
||||
@@ -611,9 +660,10 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
const proxyUrls = text.split('\r\n');
|
||||
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
|
||||
const proxyUrl = 'http://' + proxyUrls[rand];
|
||||
this.logger.info('Proxy url: ' + proxyUrl);
|
||||
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgentUndici(proxyUrl) };
|
||||
} catch {
|
||||
this.localProxy.enabled = false;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
} else {
|
||||
options = {
|
||||
@@ -1565,8 +1615,15 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
|
||||
for await (const { key, update } of args) {
|
||||
const keyAny = key as any;
|
||||
const normalizedRemoteJid = keyAny.remoteJid?.replace(/:.*$/, '');
|
||||
const normalizedParticipant = keyAny.participant?.replace(/:.*$/, '');
|
||||
if (keyAny.remoteJid) {
|
||||
keyAny.remoteJid = keyAny.remoteJid.replace(/:.*$/, '');
|
||||
}
|
||||
if (keyAny.participant) {
|
||||
keyAny.participant = keyAny.participant.replace(/:.*$/, '');
|
||||
}
|
||||
|
||||
const normalizedRemoteJid = keyAny.remoteJid;
|
||||
const normalizedParticipant = keyAny.participant;
|
||||
|
||||
if (settings?.groupsIgnore && normalizedRemoteJid?.includes('@g.us')) {
|
||||
continue;
|
||||
@@ -1644,18 +1701,48 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
|
||||
const searchId = originalMessageId || key.id;
|
||||
|
||||
const messages = (await this.prismaRepository.$queryRaw`
|
||||
SELECT * FROM "Message"
|
||||
WHERE "instanceId" = ${this.instanceId}
|
||||
AND "key"->>'id' = ${searchId}
|
||||
LIMIT 1
|
||||
`) as any[];
|
||||
findMessage = messages[0] || null;
|
||||
let retries = 0;
|
||||
const maxRetries = 3;
|
||||
const retryDelay = 500; // 500ms delay to avoid blocking for too long
|
||||
|
||||
while (retries < maxRetries) {
|
||||
const messages = (await this.prismaRepository.$queryRaw`
|
||||
SELECT * FROM "Message"
|
||||
WHERE "instanceId" = ${this.instanceId}
|
||||
AND "key"->>'id' = ${searchId}
|
||||
LIMIT 1
|
||||
`) as any[];
|
||||
findMessage = messages[0] || null;
|
||||
|
||||
if (findMessage?.id) {
|
||||
break;
|
||||
}
|
||||
|
||||
retries++;
|
||||
if (retries < maxRetries) {
|
||||
await delay(retryDelay);
|
||||
}
|
||||
}
|
||||
|
||||
if (!findMessage?.id) {
|
||||
this.logger.warn(`Original message not found for update. Skipping. Key: ${JSON.stringify(key)}`);
|
||||
this.logger.verbose(
|
||||
`Original message not found for update after ${maxRetries} retries. Skipping. This is expected for protocol messages or ephemeral events not saved to the database. Key: ${JSON.stringify(key)}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sync the incoming key.remoteJid with the stored one.
|
||||
// This mutation is safe and necessary because Baileys events might use LIDs while we store Phone JIDs (or vice versa).
|
||||
// Normalizing ensuring downstream logic uses the identifier that exists in our database.
|
||||
if (findMessage?.key?.remoteJid && key.remoteJid !== findMessage.key.remoteJid) {
|
||||
key.remoteJid = findMessage.key.remoteJid;
|
||||
}
|
||||
if (findMessage?.key?.remoteJid && findMessage.key.remoteJid !== key.remoteJid) {
|
||||
this.logger.verbose(
|
||||
`Updating key.remoteJid from ${key.remoteJid} to ${findMessage.key.remoteJid} based on stored message`,
|
||||
);
|
||||
key.remoteJid = findMessage.key.remoteJid;
|
||||
}
|
||||
message.messageId = findMessage.id;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,9 +124,20 @@ export class WebhookController extends EventController implements EventControlle
|
||||
|
||||
try {
|
||||
if (instance?.enabled && regex.test(instance.url)) {
|
||||
// Add custom headers for better webhook tracking and debugging
|
||||
const enhancedHeaders = {
|
||||
...webhookHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Instance-ID': this.monitor.waInstances[instanceName].instanceId,
|
||||
'X-Instance-Name': instanceName,
|
||||
'X-Event-Type': event,
|
||||
'X-Timestamp': Date.now().toString(),
|
||||
'User-Agent': 'EvolutionAPI-Webhook/2.3.7',
|
||||
};
|
||||
|
||||
const httpService = axios.create({
|
||||
baseURL,
|
||||
headers: webhookHeaders as Record<string, string> | undefined,
|
||||
headers: enhancedHeaders as Record<string, string>,
|
||||
timeout: webhookConfig.REQUEST?.TIMEOUT_MS ?? 30000,
|
||||
});
|
||||
|
||||
|
||||
@@ -313,6 +313,7 @@ export type Webhook = {
|
||||
};
|
||||
export type Pusher = { ENABLED: boolean; GLOBAL?: GlobalPusher; EVENTS: EventsPusher };
|
||||
export type ConfigSessionPhone = { CLIENT: string; NAME: string };
|
||||
export type Baileys = { VERSION?: string };
|
||||
export type QrCode = { LIMIT: number; COLOR: string };
|
||||
export type Typebot = { ENABLED: boolean; API_VERSION: string; SEND_MEDIA_BASE64: boolean };
|
||||
export type Chatwoot = {
|
||||
@@ -410,6 +411,7 @@ export interface Env {
|
||||
WEBHOOK: Webhook;
|
||||
PUSHER: Pusher;
|
||||
CONFIG_SESSION_PHONE: ConfigSessionPhone;
|
||||
BAILEYS: Baileys;
|
||||
QRCODE: QrCode;
|
||||
TYPEBOT: Typebot;
|
||||
CHATWOOT: Chatwoot;
|
||||
@@ -800,6 +802,9 @@ export class ConfigService {
|
||||
CLIENT: process.env?.CONFIG_SESSION_PHONE_CLIENT || 'Evolution API',
|
||||
NAME: process.env?.CONFIG_SESSION_PHONE_NAME || 'Chrome',
|
||||
},
|
||||
BAILEYS: {
|
||||
VERSION: process.env?.CONFIG_BAILEYS_VERSION,
|
||||
},
|
||||
QRCODE: {
|
||||
LIMIT: Number.parseInt(process.env.QRCODE_LIMIT) || 30,
|
||||
COLOR: process.env.QRCODE_COLOR || '#198754',
|
||||
|
||||
@@ -1,7 +1,24 @@
|
||||
import axios, { AxiosRequestConfig } from 'axios';
|
||||
import { fetchLatestBaileysVersion, WAVersion } from 'baileys';
|
||||
|
||||
import { Baileys, configService } from '../config/env.config';
|
||||
|
||||
export const fetchLatestWaWebVersion = async (options: AxiosRequestConfig<{}>) => {
|
||||
// Check if manual version is set via configuration
|
||||
const baileysConfig = configService.get<Baileys>('BAILEYS');
|
||||
const manualVersion = baileysConfig?.VERSION;
|
||||
|
||||
if (manualVersion) {
|
||||
const versionParts = manualVersion.split('.').map(Number);
|
||||
if (versionParts.length === 3 && !versionParts.some(isNaN)) {
|
||||
return {
|
||||
version: versionParts as WAVersion,
|
||||
isLatest: false,
|
||||
isManual: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const { data } = await axios.get('https://web.whatsapp.com/sw.js', {
|
||||
...options,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { prismaRepository } from '@api/server.module';
|
||||
import { configService, Database } from '@config/env.config';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
const logger = new Logger('OnWhatsappCache');
|
||||
@@ -164,9 +165,28 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
||||
logger.verbose(
|
||||
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
|
||||
);
|
||||
await prismaRepository.isOnWhatsapp.create({
|
||||
data: dataPayload,
|
||||
});
|
||||
try {
|
||||
await prismaRepository.isOnWhatsapp.create({
|
||||
data: dataPayload,
|
||||
});
|
||||
} catch (error: any) {
|
||||
// Check for unique constraint violation (Prisma error code P2002)
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2002' &&
|
||||
(error.meta?.target as string[])?.includes('remoteJid')
|
||||
) {
|
||||
logger.verbose(
|
||||
`[saveOnWhatsappCache] Race condition detected for ${remoteJid}, updating existing record instead.`,
|
||||
);
|
||||
await prismaRepository.isOnWhatsapp.update({
|
||||
where: { remoteJid: remoteJid },
|
||||
data: dataPayload,
|
||||
});
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// Loga o erro mas não para a execução dos outros promises
|
||||
|
||||
Reference in New Issue
Block a user