Deploy ElevenLabs TTS integration and diagnostics

This commit is contained in:
Hamza-Ayed
2026-05-22 16:49:43 +03:00
parent 88c8a0987e
commit 37f99a4995
2 changed files with 96 additions and 1 deletions

View File

@@ -377,8 +377,19 @@ class WhatsAppController extends BaseController
if (strpos($mimeType, ';') !== false) {
$mimeType = trim(explode(';', $mimeType)[0]);
}
$elApiKey = !empty($rule['elevenlabs_api_key']) ? $rule['elevenlabs_api_key'] : (getenv('ELEVENLABS_API_KEY') ?: null);
$elVoiceId = !empty($rule['elevenlabs_voice_id']) ? $rule['elevenlabs_voice_id'] : (getenv('ELEVENLABS_VOICE_ID') ?: null);
// Try generating native audio response first
$audioResponse = \App\Services\GeminiService::generateAudioResponseFromAudio($apiKey, $systemPrompt, $msgData['audio'], $mimeType);
$audioResponse = \App\Services\GeminiService::generateAudioResponseFromAudio(
$apiKey,
$systemPrompt,
$msgData['audio'],
$mimeType,
'Puck',
$elApiKey,
$elVoiceId
);
if ($audioResponse && !empty($audioResponse['audio'])) {
$replyAudio = $audioResponse['audio'];
$replyText = '[صوت من الذكاء الاصطناعي]';

View File

@@ -0,0 +1,84 @@
<?php
// Secure token check to prevent unauthorized execution on production
if (($_GET['token'] ?? '') !== 'nabeh_test_elevenlabs_1298') {
http_response_code(403);
die('Unauthorized access');
}
require_once dirname(__DIR__) . '/app/bootstrap.php';
use App\Services\GeminiService;
use App\Models\ChatbotRule;
use App\Core\Database;
header('Content-Type: text/plain; charset=utf-8');
echo "=== Starting ElevenLabs Audio Response Diagnostics ===\n\n";
// 1. Load active rule and extract ElevenLabs configuration
$rules = Database::select("SELECT * FROM chatbot_rules WHERE trigger_type = 'gemini_ai' AND is_active = 1 LIMIT 1");
$rule = !empty($rules) ? $rules[0] : null;
if ($rule) {
echo "✅ [Database] Found active rule for Company ID: {$rule['company_id']}\n";
// Check if keys are defined in the database rule
$elApiKey = !empty($rule['elevenlabs_api_key']) ? \App\Core\Security::decrypt($rule['elevenlabs_api_key']) : null;
$elVoiceId = $rule['elevenlabs_voice_id'] ?? null;
} else {
echo " [Database] No active rule found. Checking environment variables.\n";
$elApiKey = null;
$elVoiceId = null;
}
// Fallback to .env configuration if database values are empty
if (empty($elApiKey)) {
$elApiKey = getenv('ELEVENLABS_API_KEY');
echo " [Config] ElevenLabs API Key loaded from environment variable (.env).\n";
} else {
echo " [Config] ElevenLabs API Key loaded from active chatbot rule.\n";
}
if (empty($elVoiceId)) {
$elVoiceId = getenv('ELEVENLABS_VOICE_ID') ?: 'pNInz6obpgDQGcFmaJgB'; // Default to Adam
echo " [Config] ElevenLabs Voice ID loaded from environment/default: {$elVoiceId}\n";
} else {
echo " [Config] ElevenLabs Voice ID loaded from active chatbot rule: {$elVoiceId}\n";
}
if (empty($elApiKey)) {
echo "❌ [Config] ElevenLabs API Key is missing. Diagnostics stopped.\n";
exit(1);
}
echo " [Config] API Key starts with: " . substr($elApiKey, 0, 6) . "...\n";
// 2. Execute Test Voice Generation using ElevenLabs
echo "\n--- Generating Speech with ElevenLabs ---\n";
$testText = "مرحباً بك! أنا مساعدك الذكي من نبيه، كيف يمكنني مساعدتك اليوم؟";
echo "Text to generate: \"{$testText}\"\n";
// We pass an empty/dummy Gemini API Key and systemPrompt because we are only testing ElevenLabs TTS path
$response = GeminiService::generateAudioResponse(
'dummy_gemini_key',
'dummy_system_prompt',
$testText,
'Puck',
$elApiKey,
$elVoiceId
);
if ($response && !empty($response['audio'])) {
echo "✅ [ElevenLabs] Speech generation successful!\n";
echo " [ElevenLabs] Audio MimeType: {$response['mimeType']}\n";
echo " [ElevenLabs] Audio size: " . strlen($response['audio']) . " base64 chars\n";
// Save to test file
$binaryAudio = base64_decode($response['audio']);
$outputPath = __DIR__ . '/test_elevenlabs_out.mp3';
file_put_contents($outputPath, $binaryAudio);
echo "✅ [ElevenLabs] Audio successfully written to: " . basename($outputPath) . "\n";
} else {
echo "❌ [ElevenLabs] Speech generation failed. Check error log or verify API Key quota/limits.\n";
}
echo "\n=== Diagnostics Complete ===\n";