diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index b5bcb1e85..694173444 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -1,7 +1,7 @@ import { Message, Typebot as TypebotModel, TypebotSession } from '@prisma/client'; import axios from 'axios'; -import { ConfigService, Typebot } from '../../../../config/env.config'; +import { ConfigService, S3, Typebot } from '../../../../config/env.config'; import { Logger } from '../../../../config/logger.config'; import { InstanceDto } from '../../../dto/instance.dto'; import { PrismaRepository } from '../../../repository/repository.service'; @@ -839,6 +839,11 @@ export class TypebotService { } private getTypeMessage(msg: any) { + let mediaId: string; + + if (this.configService.get('S3').ENABLE) mediaId = msg.message.mediaUrl; + else mediaId = msg.key.id; + const types = { conversation: msg?.message?.conversation, extendedTextMessage: msg?.message?.extendedTextMessage?.text, @@ -851,12 +856,12 @@ export class TypebotService { listResponseMessage: msg?.message?.listResponseMessage?.singleSelectReply?.selectedRowId, responseRowId: msg?.message?.listResponseMessage?.singleSelectReply?.selectedRowId, // Medias - audioMessage: msg?.message?.audioMessage ? `audioMessage:${msg?.key?.id}` : undefined, - imageMessage: msg?.message?.imageMessage ? `imageMessage:${msg?.key?.id}` : undefined, - videoMessage: msg?.message?.videoMessage ? `videoMessage:${msg?.key?.id}` : undefined, - documentMessage: msg?.message?.documentMessage ? `documentMessage:${msg?.key?.id}` : undefined, + audioMessage: msg?.message?.audioMessage ? `audioMessage|${mediaId}` : undefined, + imageMessage: msg?.message?.imageMessage ? `imageMessage|${mediaId}` : undefined, + videoMessage: msg?.message?.videoMessage ? `videoMessage|${mediaId}` : undefined, + documentMessage: msg?.message?.documentMessage ? `documentMessage|${mediaId}` : undefined, documentWithCaptionMessage: msg?.message?.auddocumentWithCaptionMessageioMessage - ? `documentWithCaptionMessage:${msg?.key?.id}` + ? `documentWithCaptionMessage|${mediaId}` : undefined, }; diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index b62233fb1..7c1ad4a44 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -1036,7 +1036,18 @@ export class BaileysStartupService extends ChannelStartupService { return; } - let messageRaw: any; + const contentMsg = received?.message[getContentType(received.message)] as any; + + const messageRaw: any = { + key: received.key, + pushName: received.pushName, + message: { ...received.message }, + contextInfo: contentMsg?.contextInfo, + messageType: getContentType(received.message), + messageTimestamp: received.messageTimestamp as number, + instanceId: this.instanceId, + source: getDevice(received.key.id), + }; const isMedia = received?.message?.imageMessage || @@ -1046,45 +1057,6 @@ export class BaileysStartupService extends ChannelStartupService { received?.message?.documentWithCaptionMessage || received?.message?.audioMessage; - const contentMsg = received?.message[getContentType(received.message)] as any; - - if (this.localWebhook.webhookBase64 === true && !this.configService.get('S3').ENABLE && isMedia) { - const buffer = await downloadMediaMessage( - { key: received.key, message: received?.message }, - 'buffer', - {}, - { - logger: P({ level: 'error' }) as any, - reuploadRequest: this.client.updateMediaMessage, - }, - ); - - messageRaw = { - key: received.key, - pushName: received.pushName, - message: { - ...received.message, - base64: buffer ? buffer.toString('base64') : undefined, - }, - contextInfo: contentMsg?.contextInfo, - messageType: getContentType(received.message), - messageTimestamp: received.messageTimestamp as number, - instanceId: this.instanceId, - source: getDevice(received.key.id), - }; - } else { - messageRaw = { - key: received.key, - pushName: received.pushName, - message: { ...received.message }, - contextInfo: contentMsg?.contextInfo, - messageType: getContentType(received.message), - messageTimestamp: received.messageTimestamp as number, - instanceId: this.instanceId, - source: getDevice(received.key.id), - }; - } - if (this.localSettings.readMessages && received.key.id !== 'status@broadcast') { await this.client.readMessages([received.key]); } @@ -1115,41 +1087,57 @@ export class BaileysStartupService extends ChannelStartupService { data: messageRaw, }); - if (this.configService.get('S3').ENABLE && isMedia) { - try { - const message: any = received; - const media = await this.getBase64FromMediaMessage( - { - message, - }, - true, - ); + if (isMedia) { + if (this.configService.get('S3').ENABLE) { + try { + const message: any = received; + const media = await this.getBase64FromMediaMessage( + { + message, + }, + true, + ); - const { buffer, mediaType, fileName, size } = media; + const { buffer, mediaType, fileName, size } = media; - const mimetype = mime.lookup(fileName).toString(); + const mimetype = mime.lookup(fileName).toString(); - const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName); + const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName); - await s3Service.uploadFile(fullName, buffer, size.fileLength, { - 'Content-Type': mimetype, - }); + await s3Service.uploadFile(fullName, buffer, size.fileLength, { + 'Content-Type': mimetype, + }); - await this.prismaRepository.media.create({ - data: { - messageId: msg.id, - instanceId: this.instanceId, - type: mediaType, - fileName: fullName, - mimetype, - }, - }); + 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.mediaUrl = mediaUrl; - } catch (error) { - this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); + messageRaw.message.mediaUrl = mediaUrl; + } catch (error) { + this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); + } + } else { + if (this.localWebhook.webhookBase64 === true) { + const buffer = await downloadMediaMessage( + { key: received.key, message: received?.message }, + 'buffer', + {}, + { + logger: P({ level: 'error' }) as any, + reuploadRequest: this.client.updateMediaMessage, + }, + ); + + messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined; + } } }