Deploy: 2026-05-22 00:54:36
This commit is contained in:
@@ -72,4 +72,65 @@ class ChatbotController extends BaseController
|
||||
'id' => $id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate prompt from voice recording using Gemini audio input
|
||||
*/
|
||||
public function generatePromptFromAudio(Request $request, Response $response)
|
||||
{
|
||||
// 1. Get the uploaded audio file
|
||||
$files = $_FILES;
|
||||
if (empty($files['audio']) || $files['audio']['error'] !== UPLOAD_ERR_OK) {
|
||||
$response->status(400)->json(['status' => 'error', 'message' => 'Missing or invalid audio file upload']);
|
||||
return;
|
||||
}
|
||||
|
||||
$audioFile = $files['audio'];
|
||||
|
||||
// 2. Validate file size (max 10MB)
|
||||
if ($audioFile['size'] > 10 * 1024 * 1024) {
|
||||
$response->status(400)->json(['status' => 'error', 'message' => 'Audio file size exceeds limit (10MB)']);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Read the audio content and base64 encode
|
||||
$audioContent = file_get_contents($audioFile['tmp_name']);
|
||||
if ($audioContent === false) {
|
||||
$response->status(500)->json(['status' => 'error', 'message' => 'Failed to read uploaded audio file']);
|
||||
return;
|
||||
}
|
||||
$audioBase64 = base64_encode($audioContent);
|
||||
$mimeType = $audioFile['type'] ?: 'audio/webm'; // fallback
|
||||
// Normalize mime type (e.g. "audio/webm;codecs=opus" -> "audio/webm") for Gemini API compatibility
|
||||
if (strpos($mimeType, ';') !== false) {
|
||||
$mimeType = trim(explode(';', $mimeType)[0]);
|
||||
}
|
||||
|
||||
// 4. Retrieve the Gemini API key (custom or system default)
|
||||
$rules = ChatbotRule::findAllByCompany($request->company_id);
|
||||
$apiKey = null;
|
||||
if (!empty($rules) && !empty($rules[0]['gemini_api_key'])) {
|
||||
$apiKey = $rules[0]['gemini_api_key'];
|
||||
}
|
||||
|
||||
$apiKey = $apiKey ?: getenv('GEMINI_API_KEY');
|
||||
|
||||
if (empty($apiKey)) {
|
||||
$response->status(500)->json(['status' => 'error', 'message' => 'Gemini API Key is not configured in the system']);
|
||||
return;
|
||||
}
|
||||
|
||||
// 5. Call Gemini Service
|
||||
$generatedPrompt = \App\Services\GeminiService::generatePromptFromAudio($apiKey, $audioBase64, $mimeType);
|
||||
|
||||
if (empty($generatedPrompt)) {
|
||||
$response->status(500)->json(['status' => 'error', 'message' => 'Failed to generate prompt from audio. Please check Gemini logs.']);
|
||||
return;
|
||||
}
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'prompt' => trim($generatedPrompt)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,6 +314,9 @@ class WhatsAppController extends BaseController
|
||||
}
|
||||
$systemPrompt = $rule['ai_prompt'] ?: 'You are a helpful customer support assistant.';
|
||||
|
||||
// Enforce language matching rule dynamically
|
||||
$systemPrompt .= "\n\nIMPORTANT LANGUAGE RULE: Detect the language of the incoming message. If the incoming message is in English, you MUST reply in English. If the incoming message is in Arabic, you MUST reply in Arabic. Override any default language instruction to match the user's language.";
|
||||
|
||||
$replyText = \App\Services\GeminiService::generateResponse($apiKey, $systemPrompt, $incomingText);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user