Skip to content

Commit 4a85ece

Browse files
Jean Gardanyclaude
andcommitted
fix: resolve @lid (LID) to phone number in messages handlers and fix QR code loop
- messages.upsert: mutate received.key.remoteJid to remoteJidAlt when @lid is detected, ensuring prepareMessage, chatbot emit, contact upsert and all downstream uses receive the correct @s.whatsapp.net JID instead of the LID identifier - messages.update: after finding the stored message by key.id, resolve @lid using remoteJidAlt (fork field) or findMessage.key.remoteJid as fallback; applies to DB status update, webhook to N8N, messageUpdate record and chat unread counter - connectionUpdate: guard against infinite QR code regeneration loop when connection closes before QR is scanned (no wuid, no statusCode) - Dockerfile: use tsup directly instead of npm run build to bypass pre-existing tsc type error on terminateCall; add openssl/libc6-compat to Alpine final stage for Prisma compatibility - docker-compose.yaml: switch from remote image to local build so fixes persist across container recreations Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1665654 commit 4a85ece

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ RUN chmod +x ./Docker/scripts/* && dos2unix ./Docker/scripts/*
2727

2828
RUN ./Docker/scripts/generate_database.sh
2929

30-
RUN npm run build
30+
RUN ./node_modules/.bin/tsup
3131

3232
FROM node:20-alpine AS final
3333

3434
RUN apk update && \
35-
apk add tzdata ffmpeg bash
35+
apk add tzdata ffmpeg bash openssl openssl-dev libc6-compat
3636

37-
ENV TZ=America/Sao_Paulo
37+
ENV TZ=America/Fortaleza
3838

3939
WORKDIR /evolution
4040

docker-compose.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
services:
22
api:
33
container_name: evolution_api
4-
image: atendai/evolution-api:homolog
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
57
restart: always
68
depends_on:
79
- redis
@@ -28,6 +30,7 @@ services:
2830
- evolution_redis:/data
2931
ports:
3032
- 6379:6379
33+
restart: always
3134

3235
postgres:
3336
container_name: postgres
@@ -39,7 +42,7 @@ services:
3942
ports:
4043
- 5432:5432
4144
environment:
42-
- POSTGRES_PASSWORD=PASSWORD
45+
- POSTGRES_PASSWORD=090271jd
4346
volumes:
4447
- postgres_data:/var/lib/postgresql/data
4548
expose:

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,14 @@ export class BaileysStartupService extends ChannelStartupService {
397397

398398
if (connection === 'close') {
399399
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
400+
401+
// Guard: if connection closed before QR was scanned (no wuid, no error code), do not reconnect
402+
// Without this guard, a premature close triggers infinite QR regeneration loop
403+
if (!this.instance.wuid && !statusCode) {
404+
this.logger.info('Connection closed before QR scan — skipping reconnect to prevent loop');
405+
return;
406+
}
407+
400408
const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];
401409
const shouldReconnect = !codesToNotReconnect.includes(statusCode);
402410
if (shouldReconnect) {
@@ -1130,6 +1138,11 @@ export class BaileysStartupService extends ChannelStartupService {
11301138
received.messageTimestamp = received.messageTimestamp?.toNumber();
11311139
}
11321140

1141+
// Resolve @lid to @s.whatsapp.net so all downstream uses (prepareMessage, chatbot, contact) get the correct JID
1142+
if (received.key.remoteJid?.includes('@lid') && (received.key as any)?.remoteJidAlt) {
1143+
received.key.remoteJid = (received.key as any).remoteJidAlt;
1144+
}
1145+
11331146
if (settings?.groupsIgnore && received.key.remoteJid.includes('@g.us')) {
11341147
continue;
11351148
}
@@ -1408,15 +1421,20 @@ export class BaileysStartupService extends ChannelStartupService {
14081421
continue;
14091422
}
14101423

1424+
// Resolve @lid to @s.whatsapp.net using the stored message key as source of truth
1425+
const resolvedRemoteJid: string = key.remoteJid?.includes('@lid')
1426+
? ((key as any)?.remoteJidAlt ?? (findMessage.key as any)?.remoteJid ?? key.remoteJid)
1427+
: key.remoteJid;
1428+
14111429
if (update.message === null && update.status === undefined) {
14121430
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
14131431

14141432
const message: any = {
14151433
messageId: findMessage.id,
14161434
keyId: key.id,
1417-
remoteJid: key.remoteJid,
1435+
remoteJid: resolvedRemoteJid,
14181436
fromMe: key.fromMe,
1419-
participant: key?.remoteJid,
1437+
participant: resolvedRemoteJid,
14201438
status: 'DELETED',
14211439
instanceId: this.instanceId,
14221440
};
@@ -1436,12 +1454,12 @@ export class BaileysStartupService extends ChannelStartupService {
14361454

14371455
continue;
14381456
} else if (update.status !== undefined && status[update.status] !== findMessage.status) {
1439-
if (!key.fromMe && key.remoteJid) {
1440-
readChatToUpdate[key.remoteJid] = true;
1457+
if (!key.fromMe && resolvedRemoteJid) {
1458+
readChatToUpdate[resolvedRemoteJid] = true;
14411459

14421460
if (status[update.status] === status[4]) {
1443-
this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`);
1444-
this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp);
1461+
this.logger.log(`Update as read ${resolvedRemoteJid} - ${findMessage.messageTimestamp}`);
1462+
this.updateMessagesReadedByTimestamp(resolvedRemoteJid, findMessage.messageTimestamp);
14451463
}
14461464
}
14471465

@@ -1454,9 +1472,9 @@ export class BaileysStartupService extends ChannelStartupService {
14541472
const message: any = {
14551473
messageId: findMessage.id,
14561474
keyId: key.id,
1457-
remoteJid: key.remoteJid,
1475+
remoteJid: resolvedRemoteJid,
14581476
fromMe: key.fromMe,
1459-
participant: key?.remoteJid,
1477+
participant: resolvedRemoteJid,
14601478
status: status[update.status],
14611479
pollUpdates,
14621480
instanceId: this.instanceId,

0 commit comments

Comments
 (0)