Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ RUN chmod +x ./Docker/scripts/* && dos2unix ./Docker/scripts/*

RUN ./Docker/scripts/generate_database.sh

RUN npm run build
RUN ./node_modules/.bin/tsup

FROM node:20-alpine AS final

RUN apk update && \
apk add tzdata ffmpeg bash
apk add tzdata ffmpeg bash openssl openssl-dev libc6-compat

ENV TZ=America/Sao_Paulo
ENV TZ=America/Fortaleza

WORKDIR /evolution

Expand Down
7 changes: 5 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
services:
api:
container_name: evolution_api
image: atendai/evolution-api:homolog
build:
context: .
dockerfile: Dockerfile
restart: always
depends_on:
- redis
Expand All @@ -28,6 +30,7 @@ services:
- evolution_redis:/data
ports:
- 6379:6379
restart: always

postgres:
container_name: postgres
Expand All @@ -39,7 +42,7 @@ services:
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=PASSWORD
- POSTGRES_PASSWORD=090271jd
volumes:
- postgres_data:/var/lib/postgresql/data
expose:
Expand Down
34 changes: 26 additions & 8 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ export class BaileysStartupService extends ChannelStartupService {

if (connection === 'close') {
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;

// Guard: if connection closed before QR was scanned (no wuid, no error code), do not reconnect
// Without this guard, a premature close triggers infinite QR regeneration loop
if (!this.instance.wuid && !statusCode) {
this.logger.info('Connection closed before QR scan — skipping reconnect to prevent loop');
return;
}

const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];
const shouldReconnect = !codesToNotReconnect.includes(statusCode);
if (shouldReconnect) {
Expand Down Expand Up @@ -1130,6 +1138,11 @@ export class BaileysStartupService extends ChannelStartupService {
received.messageTimestamp = received.messageTimestamp?.toNumber();
}

// Resolve @lid to @s.whatsapp.net so all downstream uses (prepareMessage, chatbot, contact) get the correct JID
if (received.key.remoteJid?.includes('@lid') && (received.key as any)?.remoteJidAlt) {
received.key.remoteJid = (received.key as any).remoteJidAlt;
}

if (settings?.groupsIgnore && received.key.remoteJid.includes('@g.us')) {
continue;
}
Expand Down Expand Up @@ -1408,15 +1421,20 @@ export class BaileysStartupService extends ChannelStartupService {
continue;
}

// Resolve @lid to @s.whatsapp.net using the stored message key as source of truth
const resolvedRemoteJid: string = key.remoteJid?.includes('@lid')
? ((key as any)?.remoteJidAlt ?? (findMessage.key as any)?.remoteJid ?? key.remoteJid)
: key.remoteJid;

if (update.message === null && update.status === undefined) {
this.sendDataWebhook(Events.MESSAGES_DELETE, key);

const message: any = {
messageId: findMessage.id,
keyId: key.id,
remoteJid: key.remoteJid,
remoteJid: resolvedRemoteJid,
fromMe: key.fromMe,
participant: key?.remoteJid,
participant: resolvedRemoteJid,
status: 'DELETED',
instanceId: this.instanceId,
};
Expand All @@ -1436,12 +1454,12 @@ export class BaileysStartupService extends ChannelStartupService {

continue;
} else if (update.status !== undefined && status[update.status] !== findMessage.status) {
if (!key.fromMe && key.remoteJid) {
readChatToUpdate[key.remoteJid] = true;
if (!key.fromMe && resolvedRemoteJid) {
readChatToUpdate[resolvedRemoteJid] = true;

if (status[update.status] === status[4]) {
this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`);
this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp);
this.logger.log(`Update as read ${resolvedRemoteJid} - ${findMessage.messageTimestamp}`);
this.updateMessagesReadedByTimestamp(resolvedRemoteJid, findMessage.messageTimestamp);
}
}

Expand All @@ -1454,9 +1472,9 @@ export class BaileysStartupService extends ChannelStartupService {
const message: any = {
messageId: findMessage.id,
keyId: key.id,
remoteJid: key.remoteJid,
remoteJid: resolvedRemoteJid,
fromMe: key.fromMe,
participant: key?.remoteJid,
participant: resolvedRemoteJid,
status: status[update.status],
pollUpdates,
instanceId: this.instanceId,
Expand Down