Feature: Load balanced API keys and voice registration responses for Company 1

This commit is contained in:
Hamza-Ayed
2026-05-22 23:09:38 +03:00
parent 9a2cf94b86
commit 2aa8862f77
6 changed files with 103 additions and 13 deletions

View File

@@ -108,12 +108,12 @@ class ChatbotController extends BaseController
// 4. Retrieve the Gemini API key (custom or system default)
$rules = ChatbotRule::findAllByCompany($request->company_id);
$apiKey = null;
$configuredKey = null;
if (!empty($rules) && !empty($rules[0]['gemini_api_key'])) {
$apiKey = $rules[0]['gemini_api_key'];
$configuredKey = $rules[0]['gemini_api_key'];
}
$apiKey = $apiKey ?: getenv('GEMINI_API_KEY');
$apiKey = \App\Services\GeminiService::getGeminiApiKey($configuredKey);
if (empty($apiKey)) {
$response->status(500)->json(['status' => 'error', 'message' => 'Gemini API Key is not configured in the system']);

View File

@@ -358,7 +358,8 @@ class WhatsAppController extends BaseController
$replyText = $rule['ai_prompt']; // Under keyword rules, ai_prompt stores the predefined static reply
}
} elseif ($rule['trigger_type'] === 'gemini_ai') {
$apiKey = $rule['gemini_api_key'] ?: getenv('GEMINI_API_KEY');
$configuredGeminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : null;
$apiKey = \App\Services\GeminiService::getGeminiApiKey($configuredGeminiKey);
if (empty($apiKey)) {
error_log("[Chatbot Warning] Gemini API Key is not set globally or for company " . $session['company_id']);
return;
@@ -399,8 +400,11 @@ 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);
$configuredElKey = !empty($rule['elevenlabs_api_key']) ? $rule['elevenlabs_api_key'] : null;
$elApiKey = \App\Services\GeminiService::getElevenLabsApiKey($configuredElKey);
$configuredVoiceId = !empty($rule['elevenlabs_voice_id']) ? $rule['elevenlabs_voice_id'] : null;
$elVoiceId = \App\Services\GeminiService::getElevenLabsVoiceId($configuredVoiceId);
// Try generating native audio response first
$audioResponse = \App\Services\GeminiService::generateAudioResponseFromAudio(