Deploy: 2026-05-22 23:55:19

This commit is contained in:
Hamza-Ayed
2026-05-22 23:55:19 +03:00
parent 7bf0933efb
commit 4860519f39
15 changed files with 1280 additions and 102 deletions

View File

@@ -43,9 +43,25 @@ class ConversationFlowEngine
$companyId = $session['company_id'];
$text = isset($msgData['body']) ? trim($msgData['body']) : '';
// If incoming message is audio, transcribe it via Gemini
// If incoming message is audio, transcribe it via Gemini (if limits permit)
$isAudio = !empty($msgData['audio']) && !empty($msgData['mimeType']);
if ($isAudio) {
if ($companyId !== 1) {
$activeSub = \App\Models\CompanySubscription::findActiveByCompany($companyId);
if (!$activeSub) {
error_log("[Flow Engine Warning] Company {$companyId} has no active subscription for audio transcription.");
return false;
}
if (!\App\Models\CompanySubscriptionUsage::hasRemainingLimit($companyId, 'request')) {
error_log("[Flow Engine Warning] Company {$companyId} has exceeded its request limit for audio transcription.");
return false;
}
if (!\App\Models\CompanySubscriptionUsage::hasRemainingLimit($companyId, 'voice')) {
error_log("[Flow Engine Warning] Company {$companyId} has exceeded its voice limit for audio transcription.");
return false;
}
}
$rule = \App\Models\ChatbotRule::findActiveForRule($companyId);
$configuredGeminiKey = ($rule && !empty($rule['gemini_api_key'])) ? $rule['gemini_api_key'] : null;
$apiKey = \App\Services\GeminiService::getGeminiApiKey($configuredGeminiKey);
@@ -54,6 +70,11 @@ class ConversationFlowEngine
if ($transcription) {
$text = $transcription;
$msgData['body'] = $transcription;
// Increment usage stats for successful transcription
if ($companyId !== 1) {
\App\Models\CompanySubscriptionUsage::incrementUsage($companyId, 'voice');
\App\Models\CompanySubscriptionUsage::incrementUsage($companyId, 'request');
}
}
}
}
@@ -84,6 +105,21 @@ class ConversationFlowEngine
return false;
}
// Check subscription limits for active/starting flow
if ($companyId !== 1) {
$activeSub = \App\Models\CompanySubscription::findActiveByCompany($companyId);
if (!$activeSub) {
error_log("[Flow Engine Warning] Company {$companyId} has no active subscription.");
self::sendReply($session, $phone, "⚠️ عذراً، لا يوجد اشتراك نشط لهذا المتجر حالياً.");
return true;
}
if (!\App\Models\CompanySubscriptionUsage::hasRemainingLimit($companyId, 'request')) {
error_log("[Flow Engine Warning] Company {$companyId} has exceeded its general request limit.");
self::sendReply($session, $phone, "⚠️ عذراً، لقد استهلك هذا المتجر كامل الحد المسموح له من الرسائل والطلبات لهذا الشهر.");
return true;
}
}
// 3. User cancel flow option
$normalizedCancel = strtolower(trim($text));
if (in_array($normalizedCancel, ['إلغاء', 'خروج', 'cancel', 'exit'])) {