feat: convert outgoing audio to MP3 inside send_media case to bypass getAudioDuration

This commit is contained in:
Hamza-Ayed
2026-05-18 17:22:04 +03:00
parent 4ccd90dad3
commit e28d985c10
2 changed files with 28 additions and 1 deletions

View File

@@ -459,10 +459,24 @@ async function handleMessage(ws, raw) {
if (!clientReady) {
return respond({ type: 'error', message: 'WhatsApp is not ready' });
}
const { chatId, base64, mimetype, filename, caption } = payload;
let { chatId, base64, mimetype, filename, caption } = payload;
if (!chatId || !base64 || !mimetype) {
return respond({ type: 'error', message: 'chatId, base64, and mimetype are required' });
}
// If it is an OGG audio file being sent, convert it to MP3 so the headless browser can decode it and calculate duration
if (mimetype && (mimetype.includes('audio/ogg') || mimetype.includes('ogg'))) {
try {
console.log(`[WS] Converting outgoing OGG audio to MP3 for Puppeteer compatibility...`);
const mp3Base64 = await convertOggToMp3(base64);
base64 = mp3Base64;
mimetype = 'audio/mp3';
filename = 'voice_note.mp3';
} catch (err) {
console.error(`[WS] Outgoing audio conversion failed (sending raw Ogg instead):`, err.message);
}
}
try {
const { MessageMedia } = require('whatsapp-web.js');
const media = new MessageMedia(mimetype, base64, filename || 'file');