convSelecId = $convId; $this->replyText = ''; $this->cargarMensajes(); // Marcar mensajes entrantes como leĆ­dos ChatMessage::where('conversation_id', $convId) ->where('tipo', 'usuario') ->where('leido', false) ->update(['leido' => true]); ChatConversation::where('id', $convId) ->update(['mensajes_no_leidos' => 0]); } public function cargarMensajes(): void { if (! $this->convSelecId) { $this->mensajes = []; return; } $this->mensajes = ChatMessage::where('conversation_id', $this->convSelecId) ->orderBy('id') ->get() ->map(fn($m) => [ 'id' => $m->id, 'tipo' => $m->tipo, 'contenido' => $m->contenido, 'created_at' => $m->created_at->format('d/m H:i'), ]) ->toArray(); $this->dispatchBrowserEvent('scroll-admin'); } public function sendReply(): void { $this->validate(['replyText' => 'required|string|max:4096']); if (! $this->convSelecId) { return; } $conv = ChatConversation::with('contact')->find($this->convSelecId); if (! $conv) { return; } ChatMessage::create([ 'conversation_id' => $this->convSelecId, 'tipo' => 'agente', 'contenido' => $this->replyText, 'leido' => true, ]); $conv->update(['ultimo_mensaje_at' => now()]); // Enviar via Telegram si el canal es telegram if ($conv->canal === 'telegram') { $engine = new ChatBotEngine(); $engine->enviarTelegram($conv->contact->canal_id, $this->replyText); } $this->replyText = ''; $this->cargarMensajes(); } public function cerrarConv(): void { if (! $this->convSelecId) { return; } ChatConversation::where('id', $this->convSelecId) ->update(['estado' => 'cerrada']); $this->cargarMensajes(); } public function devolverAlBot(): void { if (! $this->convSelecId) { return; } ChatConversation::where('id', $this->convSelecId) ->update(['estado' => 'bot', 'menu_actual_id' => null]); } public function tomarControl(): void { if (! $this->convSelecId) { return; } ChatConversation::where('id', $this->convSelecId) ->update(['estado' => 'agente']); } public function render() { $conversaciones = ChatConversation::with('contact') ->when($this->filtroEstado, fn($q) => $q->where('estado', $this->filtroEstado)) ->when($this->busqueda, function ($q) { $q->whereHas('contact', function ($s) { $s->where('nombre', 'like', "%{$this->busqueda}%") ->orWhere('telefono', 'like', "%{$this->busqueda}%") ->orWhere('canal_id', 'like', "%{$this->busqueda}%"); }); }) ->orderByDesc('ultimo_mensaje_at') ->get(); $convSelec = $this->convSelecId ? ChatConversation::with('contact')->find($this->convSelecId) : null; return view('livewire.chat.show-conversaciones', compact('conversaciones', 'convSelec')); } }