From 83567d64669ac7ca9c0e9a2930511484a49356b3 Mon Sep 17 00:00:00 2001 From: nestordavalos Date: Sat, 5 Oct 2024 22:49:30 -0400 Subject: [PATCH 1/2] fix: Corrected audio file handling in WhatsApp An error in the WhatsApp audio message handler has been fixed. The system now checks if the file has a valid buffer before sending it. Additionally, validation has been added to verify if the audio is a valid URL or Base64. If these conditions are not met, an error message is shown, and a BadRequestException is thrown. --- src/api/controllers/sendMessage.controller.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/api/controllers/sendMessage.controller.ts b/src/api/controllers/sendMessage.controller.ts index 0d6ea33e..6f60ceb9 100644 --- a/src/api/controllers/sendMessage.controller.ts +++ b/src/api/controllers/sendMessage.controller.ts @@ -47,11 +47,14 @@ export class SendMessageController { } public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) { - if (file || isURL(data.audio) || isBase64(data.audio)) { - return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file); + if (file?.buffer || isURL(data.audio) || isBase64(data.audio)) { + // Si file existe y tiene buffer, o si es una URL o Base64, continĂºa + return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file); + } else { + console.error('El archivo no tiene buffer o el audio no es una URL o Base64 vĂ¡lida'); + throw new BadRequestException('Owned media must be a url, base64, or valid file with buffer'); } - throw new BadRequestException('Owned media must be a url or base64'); - } +} public async sendButtons({ instanceName }: InstanceDto, data: SendButtonDto) { return await this.waMonitor.waInstances[instanceName].buttonMessage(data); From 59af93251d6f2759a9bfadbf446a9073d0ab7907 Mon Sep 17 00:00:00 2001 From: nestordavalos Date: Sun, 6 Oct 2024 01:16:08 -0300 Subject: [PATCH 2/2] fix: Corregir manejo de archivo de audio en WhatsApp --- .../evolution/evolution.channel.service.ts | 7 ++- .../channel/meta/whatsapp.business.service.ts | 8 ++- .../whatsapp/whatsapp.baileys.service.ts | 57 ++++++++++--------- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/api/integrations/channel/evolution/evolution.channel.service.ts b/src/api/integrations/channel/evolution/evolution.channel.service.ts index 28d73d7f..b4020509 100644 --- a/src/api/integrations/channel/evolution/evolution.channel.service.ts +++ b/src/api/integrations/channel/evolution/evolution.channel.service.ts @@ -483,7 +483,12 @@ export class EvolutionStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = file.buffer.toString('base64'); + if (file?.buffer) { + mediaData.audio = file.buffer.toString('base64'); + } else { + console.error("El archivo o buffer no está definido correctamente."); + throw new Error("File or buffer is undefined."); + } const message = await this.processAudio(mediaData.audio, data.number); diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index f3bccdfd..934b59a3 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -1081,7 +1081,13 @@ export class BusinessStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = file.buffer.toString('base64'); + if (file?.buffer) { + // Asegurarse de que file y buffer existen antes de usarlos + mediaData.audio = file.buffer.toString('base64'); + } else { + console.error('El archivo no tiene buffer o file es undefined'); + throw new Error('File or buffer is undefined'); + } const message = await this.processAudio(mediaData.audio, data.number); diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 79167858..944a234f 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2574,42 +2574,47 @@ export class BaileysStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = file.buffer.toString('base64'); + if (file?.buffer) { + mediaData.audio = file.buffer.toString('base64'); + } else if (!isURL(data.audio) && !isBase64(data.audio)) { + console.error('Invalid file or audio source'); + throw new BadRequestException('File buffer, URL, or base64 audio is required'); + } if (!data?.encoding && data?.encoding !== false) { - data.encoding = true; + data.encoding = true; } if (data?.encoding) { - const convert = await this.processAudio(mediaData.audio); + const convert = await this.processAudio(mediaData.audio); - if (Buffer.isBuffer(convert)) { - const result = this.sendMessageWithTyping( - data.number, - { - audio: convert, - ptt: true, - mimetype: 'audio/ogg; codecs=opus', - }, - { presence: 'recording', delay: data?.delay }, - isIntegration, - ); + if (Buffer.isBuffer(convert)) { + const result = this.sendMessageWithTyping( + data.number, + { + audio: convert, + ptt: true, + mimetype: 'audio/ogg; codecs=opus', + }, + { presence: 'recording', delay: data?.delay }, + isIntegration, + ); - return result; - } else { - throw new InternalServerErrorException('Failed to convert audio'); - } + return result; + } else { + throw new InternalServerErrorException('Failed to convert audio'); + } } return await this.sendMessageWithTyping( - data.number, - { - audio: isURL(data.audio) ? { url: data.audio } : Buffer.from(data.audio, 'base64'), - ptt: true, - mimetype: 'audio/ogg; codecs=opus', - }, - { presence: 'recording', delay: data?.delay }, - isIntegration, + data.number, + { + audio: isURL(data.audio) ? { url: data.audio } : Buffer.from(data.audio, 'base64'), + ptt: true, + mimetype: 'audio/ogg; codecs=opus', + }, + { presence: 'recording', delay: data?.delay }, + isIntegration, ); }