Skip to content

Commit db7428a

Browse files
Merge pull request #949 from nestordavalos/v2.0.0
fix: Corrected audio file handling in WhatsApp
2 parents d7ddb99 + 59af932 commit db7428a

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

src/api/controllers/sendMessage.controller.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ export class SendMessageController {
4747
}
4848

4949
public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) {
50-
if (file || isURL(data.audio) || isBase64(data.audio)) {
51-
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);
50+
if (file?.buffer || isURL(data.audio) || isBase64(data.audio)) {
51+
// Si file existe y tiene buffer, o si es una URL o Base64, continúa
52+
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);
53+
} else {
54+
console.error('El archivo no tiene buffer o el audio no es una URL o Base64 válida');
55+
throw new BadRequestException('Owned media must be a url, base64, or valid file with buffer');
5256
}
53-
throw new BadRequestException('Owned media must be a url or base64');
54-
}
57+
}
5558

5659
public async sendButtons({ instanceName }: InstanceDto, data: SendButtonDto) {
5760
return await this.waMonitor.waInstances[instanceName].buttonMessage(data);

src/api/integrations/channel/evolution/evolution.channel.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,12 @@ export class EvolutionStartupService extends ChannelStartupService {
483483
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
484484
const mediaData: SendAudioDto = { ...data };
485485

486-
if (file) mediaData.audio = file.buffer.toString('base64');
486+
if (file?.buffer) {
487+
mediaData.audio = file.buffer.toString('base64');
488+
} else {
489+
console.error("El archivo o buffer no está definido correctamente.");
490+
throw new Error("File or buffer is undefined.");
491+
}
487492

488493
const message = await this.processAudio(mediaData.audio, data.number);
489494

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,13 @@ export class BusinessStartupService extends ChannelStartupService {
10811081
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
10821082
const mediaData: SendAudioDto = { ...data };
10831083

1084-
if (file) mediaData.audio = file.buffer.toString('base64');
1084+
if (file?.buffer) {
1085+
// Asegurarse de que file y buffer existen antes de usarlos
1086+
mediaData.audio = file.buffer.toString('base64');
1087+
} else {
1088+
console.error('El archivo no tiene buffer o file es undefined');
1089+
throw new Error('File or buffer is undefined');
1090+
}
10851091

10861092
const message = await this.processAudio(mediaData.audio, data.number);
10871093

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,42 +2574,47 @@ export class BaileysStartupService extends ChannelStartupService {
25742574
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
25752575
const mediaData: SendAudioDto = { ...data };
25762576

2577-
if (file) mediaData.audio = file.buffer.toString('base64');
2577+
if (file?.buffer) {
2578+
mediaData.audio = file.buffer.toString('base64');
2579+
} else if (!isURL(data.audio) && !isBase64(data.audio)) {
2580+
console.error('Invalid file or audio source');
2581+
throw new BadRequestException('File buffer, URL, or base64 audio is required');
2582+
}
25782583

25792584
if (!data?.encoding && data?.encoding !== false) {
2580-
data.encoding = true;
2585+
data.encoding = true;
25812586
}
25822587

25832588
if (data?.encoding) {
2584-
const convert = await this.processAudio(mediaData.audio);
2585-
2586-
if (Buffer.isBuffer(convert)) {
2587-
const result = this.sendMessageWithTyping<AnyMessageContent>(
2588-
data.number,
2589-
{
2590-
audio: convert,
2591-
ptt: true,
2592-
mimetype: 'audio/ogg; codecs=opus',
2593-
},
2594-
{ presence: 'recording', delay: data?.delay },
2595-
isIntegration,
2596-
);
2589+
const convert = await this.processAudio(mediaData.audio);
2590+
2591+
if (Buffer.isBuffer(convert)) {
2592+
const result = this.sendMessageWithTyping<AnyMessageContent>(
2593+
data.number,
2594+
{
2595+
audio: convert,
2596+
ptt: true,
2597+
mimetype: 'audio/ogg; codecs=opus',
2598+
},
2599+
{ presence: 'recording', delay: data?.delay },
2600+
isIntegration,
2601+
);
25972602

2598-
return result;
2599-
} else {
2600-
throw new InternalServerErrorException('Failed to convert audio');
2601-
}
2603+
return result;
2604+
} else {
2605+
throw new InternalServerErrorException('Failed to convert audio');
2606+
}
26022607
}
26032608

26042609
return await this.sendMessageWithTyping<AnyMessageContent>(
2605-
data.number,
2606-
{
2607-
audio: isURL(data.audio) ? { url: data.audio } : Buffer.from(data.audio, 'base64'),
2608-
ptt: true,
2609-
mimetype: 'audio/ogg; codecs=opus',
2610-
},
2611-
{ presence: 'recording', delay: data?.delay },
2612-
isIntegration,
2610+
data.number,
2611+
{
2612+
audio: isURL(data.audio) ? { url: data.audio } : Buffer.from(data.audio, 'base64'),
2613+
ptt: true,
2614+
mimetype: 'audio/ogg; codecs=opus',
2615+
},
2616+
{ presence: 'recording', delay: data?.delay },
2617+
isIntegration,
26132618
);
26142619
}
26152620

0 commit comments

Comments
 (0)