Feature: Load balanced API keys and voice registration responses for Company 1
This commit is contained in:
@@ -47,7 +47,8 @@ class ConversationFlowEngine
|
||||
$isAudio = !empty($msgData['audio']) && !empty($msgData['mimeType']);
|
||||
if ($isAudio) {
|
||||
$rule = \App\Models\ChatbotRule::findActiveForRule($companyId);
|
||||
$apiKey = ($rule && !empty($rule['gemini_api_key'])) ? $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)) {
|
||||
$transcription = \App\Services\GeminiService::transcribeAudio($apiKey, $msgData['audio'], $msgData['mimeType']);
|
||||
if ($transcription) {
|
||||
@@ -123,7 +124,40 @@ class ConversationFlowEngine
|
||||
}
|
||||
|
||||
// 5. Send reply if one is provided
|
||||
if ($result->getReplyText() !== '' || $result->getMediaUrl() !== null) {
|
||||
$replySent = false;
|
||||
if ($companyId === 1 && $flowName === 'driver_registration_flow' && $result->getReplyText() !== '') {
|
||||
$rule = \App\Models\ChatbotRule::findActiveForRule($companyId);
|
||||
$configuredGeminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : null;
|
||||
$apiKey = \App\Services\GeminiService::getGeminiApiKey($configuredGeminiKey);
|
||||
|
||||
if (!empty($apiKey)) {
|
||||
$configuredElKey = ($rule && !empty($rule['elevenlabs_api_key'])) ? $rule['elevenlabs_api_key'] : null;
|
||||
$elApiKey = \App\Services\GeminiService::getElevenLabsApiKey($configuredElKey);
|
||||
|
||||
$configuredVoiceId = ($rule && !empty($rule['elevenlabs_voice_id'])) ? $rule['elevenlabs_voice_id'] : null;
|
||||
$elVoiceId = \App\Services\GeminiService::getElevenLabsVoiceId($configuredVoiceId);
|
||||
|
||||
// Generate the audio voice note
|
||||
$audioData = \App\Services\GeminiService::generateAudioResponse(
|
||||
$apiKey,
|
||||
"أنت خدمة تسجيل كباتن تطبيق انطلق، تتحدث بلهجة سورية ودودة ومرحبة ومهنية جداً كأنك إنسان حقيقي.",
|
||||
$result->getReplyText(),
|
||||
'Puck',
|
||||
$elApiKey,
|
||||
$elVoiceId
|
||||
);
|
||||
|
||||
if ($audioData && !empty($audioData['audio'])) {
|
||||
// Send the text message first
|
||||
self::sendReply($session, $phone, $result->getReplyText(), $result->getMediaUrl());
|
||||
// Then send the voice note
|
||||
self::sendReply($session, $phone, '', null, $audioData['audio'], $audioData['mimeType']);
|
||||
$replySent = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$replySent && ($result->getReplyText() !== '' || $result->getMediaUrl() !== null)) {
|
||||
self::sendReply($session, $phone, $result->getReplyText(), $result->getMediaUrl());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user