Deploy: 2026-05-23 01:28:41
This commit is contained in:
@@ -471,8 +471,10 @@ class WhatsAppController extends BaseController
|
||||
$replyText = null;
|
||||
$replyAudio = null;
|
||||
$replyAudioMimeType = null;
|
||||
$usedElevenLabs = false;
|
||||
|
||||
$companyId = $session['company_id'];
|
||||
$useElevenLabs = true;
|
||||
|
||||
// Limit enforcement for non-admin companies (company 1 is admin/demo)
|
||||
if ($companyId !== 1) {
|
||||
@@ -488,10 +490,15 @@ class WhatsAppController extends BaseController
|
||||
return;
|
||||
}
|
||||
|
||||
// Check voice limit if input is audio
|
||||
// Check voice limit if input is audio to determine if we can use premium ElevenLabs
|
||||
if ($hasAudio && !\App\Models\CompanySubscriptionUsage::hasRemainingLimit($companyId, 'voice')) {
|
||||
error_log("[Chatbot Warning] Company {$companyId} has exceeded its voice request limit.");
|
||||
$replyText = "⚠️ عذراً، لقد استهلك هذا المتجر الحد المسموح له من الرسائل الصوتية لهذا الشهر. يرجى إرسال استفسارك نصياً لكي نتمكن من مساعدتك.";
|
||||
$useElevenLabs = false;
|
||||
}
|
||||
|
||||
// Check if voice feature is enabled in subscription
|
||||
$features = json_decode($activeSub['features'] ?: '{}', true);
|
||||
if (isset($features['voice']) && !$features['voice']) {
|
||||
$useElevenLabs = false;
|
||||
}
|
||||
|
||||
// Check OCR limit if input is image
|
||||
@@ -570,10 +577,10 @@ class WhatsAppController extends BaseController
|
||||
$mimeType = trim(explode(';', $mimeType)[0]);
|
||||
}
|
||||
$configuredElKey = !empty($rule['elevenlabs_api_key']) ? $rule['elevenlabs_api_key'] : null;
|
||||
$elApiKey = \App\Services\GeminiService::getElevenLabsApiKey($configuredElKey);
|
||||
$elApiKey = $useElevenLabs ? \App\Services\GeminiService::getElevenLabsApiKey($configuredElKey) : null;
|
||||
|
||||
$configuredVoiceId = !empty($rule['elevenlabs_voice_id']) ? $rule['elevenlabs_voice_id'] : null;
|
||||
$elVoiceId = \App\Services\GeminiService::getElevenLabsVoiceId($configuredVoiceId);
|
||||
$elVoiceId = $useElevenLabs ? \App\Services\GeminiService::getElevenLabsVoiceId($configuredVoiceId) : null;
|
||||
|
||||
// Try generating native audio response first
|
||||
$audioResponse = \App\Services\GeminiService::generateAudioResponseFromAudio(
|
||||
@@ -589,6 +596,9 @@ class WhatsAppController extends BaseController
|
||||
$replyAudio = $audioResponse['audio'];
|
||||
$replyAudioMimeType = $audioResponse['mimeType'] ?? 'audio/mp4';
|
||||
$replyText = '[صوت من الذكاء الاصطناعي]';
|
||||
if (!empty($elApiKey) && $replyAudioMimeType !== 'audio/mp3') {
|
||||
$usedElevenLabs = true;
|
||||
}
|
||||
} else {
|
||||
// Fallback to text output from audio
|
||||
$replyText = \App\Services\GeminiService::generateResponseFromAudio($apiKey, $systemPrompt, $msgData['audio'], $mimeType);
|
||||
@@ -677,7 +687,7 @@ class WhatsAppController extends BaseController
|
||||
// Increment SaaS usage stats if successfully sent
|
||||
if ($status === 'sent' && $companyId !== 1) {
|
||||
\App\Models\CompanySubscriptionUsage::incrementUsage($companyId, 'request');
|
||||
if ($hasAudio || !empty($replyAudio)) {
|
||||
if ($usedElevenLabs) {
|
||||
\App\Models\CompanySubscriptionUsage::incrementUsage($companyId, 'voice');
|
||||
}
|
||||
if ($hasImage) {
|
||||
|
||||
Reference in New Issue
Block a user