chatbot nuevo
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ChatConfig extends Model
|
||||
{
|
||||
protected $table = 'chat_config';
|
||||
protected $fillable = ['config_key', 'config_value', 'description'];
|
||||
|
||||
public static function get(string $key, string $default = ''): string
|
||||
{
|
||||
return static::where('config_key', $key)->value('config_value') ?? $default;
|
||||
}
|
||||
|
||||
public static function set(string $key, string $value): void
|
||||
{
|
||||
static::updateOrCreate(
|
||||
['config_key' => $key],
|
||||
['config_value' => $value]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class ChatContact extends Model
|
||||
{
|
||||
protected $table = 'chat_contacts';
|
||||
protected $fillable = ['canal', 'canal_id', 'nombre', 'telefono', 'metadata'];
|
||||
protected $casts = ['metadata' => 'array'];
|
||||
|
||||
public function conversations(): HasMany
|
||||
{
|
||||
return $this->hasMany(ChatConversation::class, 'contact_id');
|
||||
}
|
||||
|
||||
public function activeConversation(): ?ChatConversation
|
||||
{
|
||||
return $this->conversations()
|
||||
->whereIn('estado', ['bot', 'agente'])
|
||||
->latest()
|
||||
->first();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class ChatConversation extends Model
|
||||
{
|
||||
protected $table = 'chat_conversations';
|
||||
protected $fillable = ['contact_id', 'canal', 'estado', 'menu_actual_id', 'ultimo_mensaje_at', 'mensajes_no_leidos'];
|
||||
protected $casts = ['ultimo_mensaje_at' => 'datetime'];
|
||||
|
||||
public function contact(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ChatContact::class, 'contact_id');
|
||||
}
|
||||
|
||||
public function messages(): HasMany
|
||||
{
|
||||
return $this->hasMany(ChatMessage::class, 'conversation_id');
|
||||
}
|
||||
|
||||
public function menuActual(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ChatMenu::class, 'menu_actual_id');
|
||||
}
|
||||
|
||||
public function canalBadge(): string
|
||||
{
|
||||
return match($this->canal) {
|
||||
'telegram' => '✈️ Telegram',
|
||||
default => '💬 Web',
|
||||
};
|
||||
}
|
||||
|
||||
public function canalColor(): string
|
||||
{
|
||||
return match($this->canal) {
|
||||
'telegram' => 'bg-blue-100 text-blue-700',
|
||||
default => 'bg-green-100 text-green-700',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class ChatMenu extends Model
|
||||
{
|
||||
protected $table = 'chat_menus';
|
||||
protected $fillable = ['nombre', 'mensaje', 'es_raiz', 'activo'];
|
||||
protected $casts = ['es_raiz' => 'boolean', 'activo' => 'boolean'];
|
||||
|
||||
public function options(): HasMany
|
||||
{
|
||||
return $this->hasMany(ChatMenuOption::class, 'menu_id')->orderBy('orden');
|
||||
}
|
||||
|
||||
public static function raiz(): ?self
|
||||
{
|
||||
return static::where('es_raiz', true)->where('activo', true)->first();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ChatMenuOption extends Model
|
||||
{
|
||||
protected $table = 'chat_menu_options';
|
||||
protected $fillable = ['menu_id', 'clave', 'etiqueta', 'accion', 'menu_destino_id', 'respuesta_texto', 'orden'];
|
||||
|
||||
public function menu(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ChatMenu::class, 'menu_id');
|
||||
}
|
||||
|
||||
public function menuDestino(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ChatMenu::class, 'menu_destino_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ChatMessage extends Model
|
||||
{
|
||||
protected $table = 'chat_messages';
|
||||
protected $fillable = ['conversation_id', 'tipo', 'contenido', 'leido'];
|
||||
|
||||
public function conversation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ChatConversation::class, 'conversation_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user