Skip to content

Commit 59af932

Browse files
author
nestordavalos
committed
fix: Corregir manejo de archivo de audio en WhatsApp
1 parent 83567d6 commit 59af932

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

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)