diff --git a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts index c2c5931e..fbe1864a 100644 --- a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts +++ b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts @@ -19,6 +19,22 @@ export class BaileysMessageProcessor { }>(); mount({ onMessageReceive }: MountProps) { + // Se já existe subscription, fazer cleanup primeiro + if (this.subscription && !this.subscription.closed) { + this.subscription.unsubscribe(); + } + + // Se o Subject foi completado, recriar + if (this.messageSubject.closed) { + this.processorLogs.warn('MessageSubject was closed, recreating...'); + this.messageSubject = new Subject<{ + messages: WAMessage[]; + type: MessageUpsertType; + requestId?: string; + settings: any; + }>(); + } + this.subscription = this.messageSubject .pipe( tap(({ messages }) => { diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 1e3bdcf1..60082c4b 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -710,6 +710,11 @@ export class BaileysStartupService extends ChannelStartupService { this.loadWebhook(); this.loadProxy(); + // Remontar o messageProcessor para garantir que está funcionando após reconexão + this.messageProcessor.mount({ + onMessageReceive: this.messageHandle['messages.upsert'].bind(this), + }); + return await this.createClient(number); } catch (error) { this.logger.error(error);