134 lines
5.3 KiB
PHP
134 lines
5.3 KiB
PHP
<?php
|
||
// Secure token check to prevent unauthorized execution on production
|
||
if (($_GET['token'] ?? '') !== 'nabeh_test_audio_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 Chatbot Audio Reply Diagnostics ===\n\n";
|
||
|
||
// 1. Fetch Chatbot Rules and check for Gemini Key
|
||
$rules = Database::select("SELECT * FROM chatbot_rules WHERE trigger_type = 'gemini_ai' AND is_active = 1 LIMIT 1");
|
||
if (empty($rules)) {
|
||
echo "❌ [Database] No active chatbot rule found with 'gemini_ai' trigger type.\n";
|
||
exit(1);
|
||
}
|
||
|
||
$rule = $rules[0];
|
||
$apiKey = $rule['gemini_api_key'] ?: getenv('GEMINI_API_KEY');
|
||
|
||
if (empty($apiKey)) {
|
||
echo "❌ [Config] Gemini API Key is empty. Please set gemini_api_key in the active chatbot rule or GEMINI_API_KEY in the .env file.\n";
|
||
exit(1);
|
||
}
|
||
|
||
echo "✅ [Database] Found active Gemini rule for Company ID: {$rule['company_id']}\n";
|
||
echo "ℹ️ [Config] Gemini API Key starts with: " . substr($apiKey, 0, 6) . "...\n";
|
||
|
||
// 2. Test Gemini API Audio Output (Puck Voice, gemini-flash-lite-latest)
|
||
echo "\n--- Testing Gemini Audio Response ---\n";
|
||
$systemPrompt = "You are a friendly customer service assistant. Speak in a warm, welcoming tone.";
|
||
$testMsg = "مرحبا، هل متجركم مفتوح اليوم؟";
|
||
|
||
// Test via cURL directly to dump exact error
|
||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=' . $apiKey;
|
||
$payload = json_encode([
|
||
'contents' => [[
|
||
'role' => 'user',
|
||
'parts' => [['text' => $testMsg]]
|
||
]],
|
||
'systemInstruction' => [
|
||
'parts' => [['text' => $systemPrompt]]
|
||
],
|
||
'generationConfig' => [
|
||
'responseModalities' => ['AUDIO'],
|
||
'speechConfig' => [
|
||
'voiceConfig' => [
|
||
'prebuiltVoiceConfig' => [
|
||
'voiceName' => 'Puck'
|
||
]
|
||
]
|
||
]
|
||
]
|
||
]);
|
||
|
||
$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']);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||
$response = curl_exec($ch);
|
||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
curl_close($ch);
|
||
|
||
if ($httpCode !== 200) {
|
||
echo "❌ [Gemini API Error] HTTP Status: " . $httpCode . "\n";
|
||
echo "Response Payload: " . $response . "\n";
|
||
$audioResponse = null;
|
||
} else {
|
||
$data = json_decode($response, true);
|
||
$part = $data['candidates'][0]['content']['parts'][0] ?? null;
|
||
if ($part && isset($part['inlineData'])) {
|
||
$audioResponse = [
|
||
'audio' => $part['inlineData']['data'],
|
||
'mimeType' => $part['inlineData']['mimeType'] ?? 'audio/mp4'
|
||
];
|
||
} else {
|
||
echo "❌ [Gemini API Error] Response did not contain inlineData. Response: " . $response . "\n";
|
||
$audioResponse = null;
|
||
}
|
||
}
|
||
|
||
if ($audioResponse && !empty($audioResponse['audio'])) {
|
||
echo "✅ [Gemini] Successfully generated voice note!\n";
|
||
echo "ℹ️ [Gemini] Audio MimeType: " . $audioResponse['mimeType'] . "\n";
|
||
echo "ℹ️ [Gemini] Audio Size: " . strlen($audioResponse['audio']) . " base64 chars\n";
|
||
|
||
// 3. Test Audio-to-Audio conversion
|
||
echo "\n--- Testing Audio-to-Audio (Speech-to-Speech) ---\n";
|
||
$startTime = microtime(true);
|
||
$audioResponse2 = GeminiService::generateAudioResponseFromAudio($apiKey, $systemPrompt, $audioResponse['audio'], $audioResponse['mimeType'], 'Puck');
|
||
$elapsedTime2 = round(microtime(true) - $startTime, 2);
|
||
|
||
if ($audioResponse2 && !empty($audioResponse2['audio'])) {
|
||
echo "✅ [Gemini] Successfully generated Audio-to-Audio response in {$elapsedTime2} seconds!\n";
|
||
echo "ℹ️ [Gemini] Audio MimeType: " . $audioResponse2['mimeType'] . "\n";
|
||
echo "ℹ️ [Gemini] Audio Size: " . strlen($audioResponse2['audio']) . " base64 chars\n";
|
||
} else {
|
||
echo "❌ [Gemini] Audio-to-Audio generation failed.\n";
|
||
}
|
||
} else {
|
||
echo "❌ [Gemini] Audio response generation failed.\n";
|
||
}
|
||
|
||
// 4. Check Node.js WhatsApp Gateway Status
|
||
echo "\n--- Checking Node.js Gateway Status ---\n";
|
||
$gatewayUrl = rtrim(getenv('WHATSAPP_GATEWAY_URL') ?: 'http://localhost:3722', '/');
|
||
echo "Gateway URL: {$gatewayUrl}\n";
|
||
|
||
$ch = curl_init($gatewayUrl . '/health');
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||
$healthResponse = curl_exec($ch);
|
||
$healthHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
$healthError = curl_error($ch);
|
||
curl_close($ch);
|
||
|
||
if ($healthHttpCode === 200) {
|
||
echo "✅ [Gateway] Gateway is ONLINE and healthy.\n";
|
||
echo "ℹ️ [Gateway] Response: {$healthResponse}\n";
|
||
} else {
|
||
echo "❌ [Gateway] Gateway is OFFLINE or returning error. HTTP Code: {$healthHttpCode}. Error: {$healthError}\n";
|
||
}
|
||
|
||
echo "\n=== Diagnostics Complete ===\n";
|