diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 459ab18f9..1e4808c15 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -516,7 +516,9 @@ export class BusinessStartupService extends ChannelStartupService { const mediaUrl = await s3Service.getObjectUrl(fullName); messageRaw.message.mediaUrl = mediaUrl; - messageRaw.message.base64 = buffer.data.toString('base64'); + if (this.localWebhook.enabled && this.localWebhook.webhookBase64) { + messageRaw.message.base64 = buffer.data.toString('base64'); + } // Processar OpenAI speech-to-text para áudio após o mediaUrl estar disponível if (this.configService.get('OPENAI').ENABLED && mediaType === 'audio') { @@ -554,11 +556,19 @@ export class BusinessStartupService extends ChannelStartupService { this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); } } else { - const buffer = await this.downloadMediaMessage(received?.messages[0]); - messageRaw.message.base64 = buffer.toString('base64'); + if (this.localWebhook.enabled && this.localWebhook.webhookBase64) { + const buffer = await this.downloadMediaMessage(received?.messages[0]); + messageRaw.message.base64 = buffer.toString('base64'); + } // Processar OpenAI speech-to-text para áudio mesmo sem S3 if (this.configService.get('OPENAI').ENABLED && message.type === 'audio') { + let openAiBase64 = messageRaw.message.base64; + if (!openAiBase64) { + const buffer = await this.downloadMediaMessage(received?.messages[0]); + openAiBase64 = buffer.toString('base64'); + } + const openAiDefaultSettings = await this.prismaRepository.openaiSetting.findFirst({ where: { instanceId: this.instanceId, @@ -574,7 +584,7 @@ export class BusinessStartupService extends ChannelStartupService { openAiDefaultSettings.OpenaiCreds, { message: { - base64: messageRaw.message.base64, + base64: openAiBase64, ...messageRaw, }, }, @@ -1016,6 +1026,7 @@ export class BusinessStartupService extends ChannelStartupService { [message['mediaType']]: { [message['type']]: message['id'], ...(message['mediaType'] !== 'audio' && + message['mediaType'] !== 'video' && message['fileName'] && !isImage && { filename: message['fileName'] }), ...(message['mediaType'] !== 'audio' && message['caption'] && { caption: message['caption'] }), @@ -1606,9 +1617,14 @@ export class BusinessStartupService extends ChannelStartupService { const messageType = msg.messageType.includes('Message') ? msg.messageType : msg.messageType + 'Message'; const mediaMessage = msg.message[messageType]; + if (!msg.message?.base64) { + const buffer = await this.downloadMediaMessage({ type: messageType, ...msg.message }); + msg.message.base64 = buffer.toString('base64'); + } + return { mediaType: msg.messageType, - fileName: mediaMessage?.fileName, + fileName: mediaMessage?.fileName || mediaMessage?.filename, caption: mediaMessage?.caption, size: { fileLength: mediaMessage?.fileLength, diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 2636adbda..46e144512 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -82,7 +82,7 @@ import { createId as cuid } from '@paralleldrive/cuid2'; import { Instance, Message } from '@prisma/client'; import { createJid } from '@utils/createJid'; import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion'; -import {makeProxyAgent, makeProxyAgentUndici} from '@utils/makeProxyAgent'; +import { makeProxyAgent, makeProxyAgentUndici } from '@utils/makeProxyAgent'; import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache'; import { status } from '@utils/renderStatus'; import { sendTelemetry } from '@utils/sendTelemetry'; @@ -1349,6 +1349,10 @@ export class BaileysStartupService extends ChannelStartupService { this.logger.verbose(messageRaw); sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`); + if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) { + messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt; + } + console.log(messageRaw); this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw); diff --git a/src/utils/makeProxyAgent.ts b/src/utils/makeProxyAgent.ts index e882876ef..ac64b9dec 100644 --- a/src/utils/makeProxyAgent.ts +++ b/src/utils/makeProxyAgent.ts @@ -1,7 +1,6 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; import { SocksProxyAgent } from 'socks-proxy-agent'; - -import { ProxyAgent } from 'undici' +import { ProxyAgent } from 'undici'; type Proxy = { host: string; @@ -46,38 +45,38 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent | } export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent { - let proxyUrl: string - let protocol: string + let proxyUrl: string; + let protocol: string; if (typeof proxy === 'string') { - const url = new URL(proxy) - protocol = url.protocol.replace(':', '') - proxyUrl = proxy + const url = new URL(proxy); + protocol = url.protocol.replace(':', ''); + proxyUrl = proxy; } else { - const { host, password, port, protocol: proto, username } = proxy - protocol = (proto || 'http').replace(':', '') + const { host, password, port, protocol: proto, username } = proxy; + protocol = (proto || 'http').replace(':', ''); if (protocol === 'socks') { - protocol = 'socks5' + protocol = 'socks5'; } - const auth = username && password ? `${username}:${password}@` : '' - proxyUrl = `${protocol}://${auth}${host}:${port}` + const auth = username && password ? `${username}:${password}@` : ''; + proxyUrl = `${protocol}://${auth}${host}:${port}`; } - const PROXY_HTTP_PROTOCOL = 'http' - const PROXY_HTTPS_PROTOCOL = 'https' - const PROXY_SOCKS4_PROTOCOL = 'socks4' - const PROXY_SOCKS5_PROTOCOL = 'socks5' + const PROXY_HTTP_PROTOCOL = 'http'; + const PROXY_HTTPS_PROTOCOL = 'https'; + const PROXY_SOCKS4_PROTOCOL = 'socks4'; + const PROXY_SOCKS5_PROTOCOL = 'socks5'; switch (protocol) { case PROXY_HTTP_PROTOCOL: case PROXY_HTTPS_PROTOCOL: case PROXY_SOCKS4_PROTOCOL: case PROXY_SOCKS5_PROTOCOL: - return new ProxyAgent(proxyUrl) + return new ProxyAgent(proxyUrl); default: - throw new Error(`Unsupported proxy protocol: ${protocol}`) + throw new Error(`Unsupported proxy protocol: ${protocol}`); } }