Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 21 additions & 5 deletions src/api/integrations/channel/meta/whatsapp.business.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>('OPENAI').ENABLED && mediaType === 'audio') {
Expand Down Expand Up @@ -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>('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,
Expand All @@ -574,7 +584,7 @@ export class BusinessStartupService extends ChannelStartupService {
openAiDefaultSettings.OpenaiCreds,
{
message: {
base64: messageRaw.message.base64,
base64: openAiBase64,
...messageRaw,
},
},
Expand Down Expand Up @@ -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'] }),
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
35 changes: 17 additions & 18 deletions src/utils/makeProxyAgent.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -46,38 +45,38 @@
}

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;

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.
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}`);
}
}