mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 20:12:02 -06:00
Merge pull request #949 from nestordavalos/v2.0.0
fix: Corrected audio file handling in WhatsApp
This commit is contained in:
commit
db7428abd9
@ -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);
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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<AnyMessageContent>(
|
||||
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<AnyMessageContent>(
|
||||
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<AnyMessageContent>(
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user