- Migration + AiUsageLog model with tokens, duration, time, cost fields - GeminiIntentService and GeminiVisionService log every API call with input/output token counts from usageMetadata and response time - TelegramBotService logs Whisper calls with audio duration (from Telegram) and calculates cost at $1/second - Whisper voice duration now passed from TelegramWebhookController - Free text in Telegram now tries Gemini intent detection before showing menu - /chat/ia-logs: Livewire component with summary cards + filterable table - Whisper connection test button in config panel - "Logs IA" link added to Chat/Bot nav section Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
153 lines
5.9 KiB
PHP
Executable File
153 lines
5.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Livewire\Chat;
|
|
|
|
use App\Models\ChatConfig;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
use Livewire\Component;
|
|
|
|
class ShowConfiguracionChat extends Component
|
|
{
|
|
use LivewireAlert;
|
|
|
|
// ── Chat general ──────────────────────────────────────────
|
|
public string $telegram_token = '';
|
|
public string $mensaje_bienvenida = '';
|
|
public string $mensaje_transferencia = '';
|
|
public string $webhookResult = '';
|
|
public string $whisperResult = '';
|
|
|
|
// ── Gemini IA ─────────────────────────────────────────────
|
|
public string $gemini_api_key = '';
|
|
public string $gemini_habilitado = '0';
|
|
public string $gemini_prompt_extra = '';
|
|
|
|
// ── Validación de pago por foto + correo ──────────────────
|
|
public string $validacion_foto_habilitada = '0';
|
|
public string $validacion_accion_auto = 'solo_notificar';
|
|
public string $validacion_monto_tolerancia = '0';
|
|
|
|
// ── Whisper STT ───────────────────────────────────────────
|
|
public string $whisper_url = '';
|
|
public string $whisper_token = '';
|
|
public string $whisper_habilitado = '0';
|
|
|
|
// ── Bre-B ─────────────────────────────────────────────────
|
|
public string $recarga_banco_nombre = '';
|
|
public string $recarga_banco_llave = '';
|
|
public string $recarga_banco_titular = '';
|
|
|
|
public function mount(): void
|
|
{
|
|
$keys = [
|
|
'telegram_token', 'mensaje_bienvenida', 'mensaje_transferencia',
|
|
'gemini_api_key', 'gemini_habilitado', 'gemini_prompt_extra',
|
|
'validacion_foto_habilitada', 'validacion_accion_auto', 'validacion_monto_tolerancia',
|
|
'whisper_url', 'whisper_token', 'whisper_habilitado',
|
|
'recarga_banco_nombre', 'recarga_banco_llave', 'recarga_banco_titular',
|
|
];
|
|
|
|
$defaults = [
|
|
'mensaje_bienvenida' => 'Hola! Bienvenido. Escribe tu consulta.',
|
|
'mensaje_transferencia' => 'Un agente se comunicara contigo en breve. Por favor espera.',
|
|
'validacion_accion_auto' => 'solo_notificar',
|
|
'validacion_monto_tolerancia' => '0',
|
|
];
|
|
|
|
foreach ($keys as $key) {
|
|
$this->{$key} = ChatConfig::get($key, $defaults[$key] ?? '');
|
|
}
|
|
}
|
|
|
|
public function guardar(): void
|
|
{
|
|
$this->validate([
|
|
'mensaje_bienvenida' => 'required|string|max:500',
|
|
'mensaje_transferencia' => 'required|string|max:500',
|
|
'validacion_monto_tolerancia' => 'nullable|integer|min:0',
|
|
'recarga_banco_nombre' => 'nullable|max:60',
|
|
'recarga_banco_llave' => 'nullable|max:60',
|
|
'recarga_banco_titular' => 'nullable|max:100',
|
|
]);
|
|
|
|
$keys = [
|
|
'telegram_token', 'mensaje_bienvenida', 'mensaje_transferencia',
|
|
'gemini_api_key', 'gemini_habilitado', 'gemini_prompt_extra',
|
|
'validacion_foto_habilitada', 'validacion_accion_auto', 'validacion_monto_tolerancia',
|
|
'whisper_url', 'whisper_token', 'whisper_habilitado',
|
|
'recarga_banco_nombre', 'recarga_banco_llave', 'recarga_banco_titular',
|
|
];
|
|
|
|
foreach ($keys as $key) {
|
|
ChatConfig::set($key, $this->{$key});
|
|
}
|
|
|
|
$this->alert('success', 'Configuracion guardada correctamente.');
|
|
}
|
|
|
|
public function resetearWhisper(): void
|
|
{
|
|
$url = trim($this->whisper_url ?: ChatConfig::get('whisper_url'));
|
|
$token = trim($this->whisper_token ?: ChatConfig::get('whisper_token'));
|
|
|
|
if (! $url) {
|
|
$this->whisperResult = '⚠️ La URL de Whisper está vacía.';
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$response = Http::timeout(10)
|
|
->withBasicAuth('whisper', $token)
|
|
->get(rtrim($url, '/asr')); // ping al servidor
|
|
|
|
$this->whisperResult = $response->successful()
|
|
? '✅ Conexión con Whisper establecida correctamente.'
|
|
: '❌ Whisper respondió con error ' . $response->status() . '.';
|
|
} catch (\Throwable $e) {
|
|
$this->whisperResult = '❌ No se pudo conectar con Whisper: ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function registrarWebhook(): void
|
|
{
|
|
$token = trim($this->telegram_token);
|
|
|
|
if (! $token) {
|
|
$this->webhookResult = '⚠️ El token de Telegram está vacío.';
|
|
return;
|
|
}
|
|
|
|
ChatConfig::set('telegram_token', $token);
|
|
|
|
$webhookUrl = url('/chat/webhook/telegram');
|
|
|
|
try {
|
|
$response = Http::timeout(15)
|
|
->post("https://api.telegram.org/bot{$token}/setWebhook", [
|
|
'url' => $webhookUrl,
|
|
]);
|
|
|
|
$data = $response->json();
|
|
|
|
if (! ($data['ok'] ?? false)) {
|
|
$this->webhookResult = '❌ Error de Telegram: ' . ($data['description'] ?? 'respuesta desconocida');
|
|
return;
|
|
}
|
|
|
|
// Registrar comandos del bot (/menu, /salir, /cancelar)
|
|
app(\App\Services\TelegramBotService::class)->registrarComandos();
|
|
|
|
$this->webhookResult = '✅ Webhook registrado y comandos configurados correctamente.';
|
|
|
|
} catch (\Throwable $e) {
|
|
$this->webhookResult = '❌ No se pudo conectar con Telegram: ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.chat.show-configuracion-chat');
|
|
}
|
|
}
|