diff --git a/modules/turnero/api/chat_upload_media.php b/modules/turnero/api/chat_upload_media.php index dd53e82..890695d 100644 --- a/modules/turnero/api/chat_upload_media.php +++ b/modules/turnero/api/chat_upload_media.php @@ -28,9 +28,25 @@ $caption = trim($_POST['caption'] ?? ''); $isVoice = !empty($_POST['is_voice']); // mime_content_type() returns video/webm for browser-recorded audio (same WebM container). -// WhatsApp rejects webm; map to audio/ogg (same Opus codec, accepted format). -$mime = (str_contains($detectedMime, 'webm') || str_contains($browserMime, 'webm')) - ? 'audio/ogg' : $detectedMime; +// WhatsApp only accepts OGG Opus, not WebM. Convert with ffmpeg when available. +$isWebm = str_contains($detectedMime, 'webm') || str_contains($browserMime, 'webm'); +$mime = $isWebm ? 'audio/ogg' : $detectedMime; + +$uploadFilePath = $file['tmp_name']; +if ($isWebm && function_exists('shell_exec')) { + $ffmpeg = trim((string)@shell_exec('which ffmpeg 2>/dev/null')); + if ($ffmpeg) { + $convertedPath = sys_get_temp_dir() . '/wa_audio_' . uniqid() . '.ogg'; + $cmd = escapeshellcmd($ffmpeg) + . ' -y -i ' . escapeshellarg($file['tmp_name']) + . ' -map 0:a:0 -c:a libopus -b:a 32k -vbr on -ar 48000 -ac 1 -f ogg ' + . escapeshellarg($convertedPath) . ' 2>/dev/null'; + @shell_exec($cmd); + if (file_exists($convertedPath) && filesize($convertedPath) > 0) { + $uploadFilePath = $convertedPath; + } + } +} // Determinar tipo de media if (str_starts_with($mime, 'image/')) $mediaType = 'image'; @@ -49,7 +65,7 @@ try { $wa = new WhatsAppService('turnero'); // Subir a WhatsApp Media API - $uploadResult = $wa->uploadMedia($file['tmp_name'], $mime); + $uploadResult = $wa->uploadMedia($uploadFilePath, $mime); if (!$uploadResult || !isset($uploadResult['id'])) { tmErr('No se pudo subir el archivo a WhatsApp', 502); }