Files
nabeh/backend/public/test_elevenlabs.php
2026-05-22 16:52:15 +03:00

107 lines
3.7 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 length: " . strlen($elApiKey) . ", starts with: " . substr($elApiKey, 0, 6) . "..., ends with: " . substr($elApiKey, -4) . "\n";
// 2. Execute Test Voice Generation using ElevenLabs
echo "\n--- Generating Speech with ElevenLabs ---\n";
$testText = "مرحباً بك! أنا مساعدك الذكي من نبيه، كيف يمكنني مساعدتك اليوم؟";
echo "Text to generate: \"{$testText}\"\n";
// Execute ElevenLabs TTS directly in this test script to get detailed error logs
$url = 'https://api.elevenlabs.io/v1/text-to-speech/' . $elVoiceId;
$payload = json_encode([
'text' => $testText,
'model_id' => 'eleven_multilingual_v2',
'voice_settings' => [
'stability' => 0.5,
'similarity_boost' => 0.8
]
]);
echo "Sending API request to: {$url}...\n";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'xi-api-key: ' . $elApiKey
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
echo "HTTP Status Code: {$httpCode}\n";
if ($curlError) {
echo "cURL Error: {$curlError}\n";
}
if ($httpCode === 200) {
echo "✅ [ElevenLabs] Speech generation successful!\n";
echo " [ElevenLabs] Audio size: " . strlen($response) . " bytes\n";
// Save to test file
$outputPath = __DIR__ . '/test_elevenlabs_out.mp3';
file_put_contents($outputPath, $response);
echo "✅ [ElevenLabs] Audio successfully written to: " . basename($outputPath) . "\n";
} else {
echo "❌ [ElevenLabs] Speech generation failed.\n";
echo "Response: " . $response . "\n";
}
echo "\n=== Diagnostics Complete ===\n";