85 lines
3.2 KiB
PHP
85 lines
3.2 KiB
PHP
<?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";
|