From 9f32505899c2aea08f8d472a5181d7027f3effcd Mon Sep 17 00:00:00 2001 From: Renato Montagna Junior Date: Thu, 28 May 2026 08:08:54 +0100 Subject: [PATCH 1/2] fix(baileys): send mediaUrl in webhook without SAVE_DATA.NEW_MESSAGE --- .../whatsapp/whatsapp.baileys.service.ts | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index e04262999..4023bcf3d 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1542,10 +1542,11 @@ export class BaileysStartupService extends ChannelStartupService { } } + let msg: any = null; if (this.configService.get('DATABASE').SAVE_DATA.NEW_MESSAGE) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { pollUpdates, ...messageData } = messageRaw as any; - const msg = await this.prismaRepository.message.create({ data: messageData }); + msg = await this.prismaRepository.message.create({ data: messageData }); const { remoteJid } = received.key; const timestamp = msg.messageTimestamp; @@ -1573,41 +1574,43 @@ export class BaileysStartupService extends ChannelStartupService { } else { this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`); } + } - if (isMedia) { - if (this.configService.get('S3').ENABLE) { - try { - if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { - this.logger.warn('Video upload is disabled. Skipping video upload.'); - // Skip video upload by returning early from this block - return; - } + if (isMedia) { + if (this.configService.get('S3').ENABLE) { + try { + if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { + this.logger.warn('Video upload is disabled. Skipping video upload.'); + // Skip video upload by returning early from this block + return; + } - const message: any = received; + const message: any = received; - // Verificação adicional para garantir que há conteúdo de mídia real - const hasRealMedia = this.hasValidMediaContent(message); + // Verificação adicional para garantir que há conteúdo de mídia real + const hasRealMedia = this.hasValidMediaContent(message); - if (!hasRealMedia) { - this.logger.warn('Message detected as media but contains no valid media content'); - } else { - const media = await this.getBase64FromMediaMessage({ message }, true); + if (!hasRealMedia) { + this.logger.warn('Message detected as media but contains no valid media content'); + } else { + const media = await this.getBase64FromMediaMessage({ message }, true); - if (!media) { - this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO'); - return; - } + if (!media) { + this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO'); + return; + } - const { buffer, mediaType, fileName, size } = media; - const mimetype = mimeTypes.lookup(fileName).toString(); - const fullName = join( - `${this.instance.id}`, - received.key.remoteJid, - mediaType, - `${Date.now()}_${fileName}`, - ); - await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype }); + const { buffer, mediaType, fileName, size } = media; + const mimetype = mimeTypes.lookup(fileName).toString(); + const fullName = join( + `${this.instance.id}`, + received.key.remoteJid, + mediaType, + `${Date.now()}_${fileName}`, + ); + await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype }); + if (msg?.id) { await this.prismaRepository.media.create({ data: { messageId: msg.id, @@ -1617,16 +1620,18 @@ export class BaileysStartupService extends ChannelStartupService { mimetype, }, }); + } - const mediaUrl = await s3Service.getObjectUrl(fullName); + const mediaUrl = await s3Service.getObjectUrl(fullName); - (messageRaw.message as any).mediaUrl = mediaUrl; + (messageRaw.message as any).mediaUrl = mediaUrl; + if (msg?.id) { await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw }); } - } catch (error) { - this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); } + } catch (error) { + this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); } } } @@ -1674,7 +1679,6 @@ export class BaileysStartupService extends ChannelStartupService { messageRaw.key.addressingMode = 'pn'; } - console.log(messageRaw); this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw); From 4c9a3b3f2eb85bbcf730ee8538bdb3082e696d43 Mon Sep 17 00:00:00 2001 From: Renato Montagna Junior Date: Thu, 28 May 2026 09:32:14 +0100 Subject: [PATCH 2/2] fix(baileys): type persisted message and avoid early return on video upload --- .../whatsapp/whatsapp.baileys.service.ts | 85 +++++++++---------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 4023bcf3d..104a390a7 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1542,7 +1542,7 @@ export class BaileysStartupService extends ChannelStartupService { } } - let msg: any = null; + let msg: Message | null = null; if (this.configService.get('DATABASE').SAVE_DATA.NEW_MESSAGE) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { pollUpdates, ...messageData } = messageRaw as any; @@ -1581,53 +1581,50 @@ export class BaileysStartupService extends ChannelStartupService { try { if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { this.logger.warn('Video upload is disabled. Skipping video upload.'); - // Skip video upload by returning early from this block - return; - } - - const message: any = received; - - // Verificação adicional para garantir que há conteúdo de mídia real - const hasRealMedia = this.hasValidMediaContent(message); - - if (!hasRealMedia) { - this.logger.warn('Message detected as media but contains no valid media content'); } else { - const media = await this.getBase64FromMediaMessage({ message }, true); - - if (!media) { - this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO'); - return; - } - - const { buffer, mediaType, fileName, size } = media; - const mimetype = mimeTypes.lookup(fileName).toString(); - const fullName = join( - `${this.instance.id}`, - received.key.remoteJid, - mediaType, - `${Date.now()}_${fileName}`, - ); - await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype }); - - if (msg?.id) { - await this.prismaRepository.media.create({ - data: { - messageId: msg.id, - instanceId: this.instanceId, - type: mediaType, - fileName: fullName, - mimetype, - }, - }); - } + const message: any = received; + + // Verificação adicional para garantir que há conteúdo de mídia real + const hasRealMedia = this.hasValidMediaContent(message); + + if (!hasRealMedia) { + this.logger.warn('Message detected as media but contains no valid media content'); + } else { + const media = await this.getBase64FromMediaMessage({ message }, true); + + if (!media) { + this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO'); + } else { + const { buffer, mediaType, fileName, size } = media; + const mimetype = mimeTypes.lookup(fileName).toString(); + const fullName = join( + `${this.instance.id}`, + received.key.remoteJid, + mediaType, + `${Date.now()}_${fileName}`, + ); + await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype }); + + if (msg?.id) { + await this.prismaRepository.media.create({ + data: { + messageId: msg.id, + instanceId: this.instanceId, + type: mediaType, + fileName: fullName, + mimetype, + }, + }); + } - const mediaUrl = await s3Service.getObjectUrl(fullName); + const mediaUrl = await s3Service.getObjectUrl(fullName); - (messageRaw.message as any).mediaUrl = mediaUrl; + (messageRaw.message as any).mediaUrl = mediaUrl; - if (msg?.id) { - await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw }); + if (msg?.id) { + await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw }); + } + } } } } catch (error) {