feat: media via AI, text via NormalBot only, ai_for_media toggle

- Text/interactive/button messages → NormalBot only, never escalates to AI
- Image/audio/video/document → AiBot.processMedia() if ai_for_media enabled,
  else falls back to category greeting menu
- AiBot.processMedia() uses permission_type context so AI acknowledges uploads
  for perm 3 and redirects others to their menu
- WpWebhook.handleMedia now calls BotRouter so the bot responds to media
- Admin UI: checkbox toggle "Procesar imágenes y archivos con IA" in AI tab
- categoryMenuFallback() extracted in BotRouter as shared helper

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-28 13:28:56 -05:00
co-authored by Claude Sonnet 4.6
parent 4d7dd0f306
commit 6f799c3829
5 changed files with 95 additions and 10 deletions
+55
View File
@@ -3,6 +3,54 @@ declare(strict_types=1);
class AiBot
{
public static function processMedia(array $company, array $context, string $mediaType, string $caption): ?array
{
$permType = (int)($context['permission_type'] ?? 1);
$config = self::getConfig($company);
$mediaLabel = match ($mediaType) {
'image' => 'una imagen',
'audio' => 'un mensaje de audio',
'video' => 'un video',
'document' => 'un documento',
'sticker' => 'un sticker',
default => 'un archivo',
};
$canUpload = $permType === 3;
$permDesc = $canUpload
? 'El usuario tiene permisos para subir información y reportes.'
: 'El usuario solo puede recibir información o descargar informes, NO puede subir archivos.';
$captionNote = $caption !== '' && !str_starts_with($caption, '[') ? " con el texto: \"{$caption}\"" : '';
$basePrompt = $config['ai_prompt'] ?? self::defaultMediaPrompt($company);
$systemPrompt = $basePrompt . "\n\n{$permDesc}\n\nReglas:\n"
. "- Si el usuario puede subir: confirma recepción de {$mediaLabel} e indica que será procesado.\n"
. "- Si NO puede subir: explica brevemente y dile que use el menú de opciones.\n"
. "- Respuesta máx 2 oraciones. Sin saludo largo.";
$userMessage = "El usuario envió {$mediaLabel}{$captionNote}.";
$botCtx = ConversationContext::getOrCreate((int)$company['id'], $context['from'], 'ai');
$ctxId = (int)$botCtx['id'];
ConversationContext::addAiMessage($ctxId, ['role' => 'user', 'content' => $userMessage]);
$result = self::callLlm($systemPrompt, $ctxId, $company);
if ($result === null) {
return null;
}
ConversationContext::addAiMessage($ctxId, ['role' => 'assistant', 'content' => $result['content'] ?? '']);
return [
'action' => 'send',
'type' => 'text',
'to' => $context['from'],
'payload' => json_encode(['text' => $result['content'] ?? '']),
];
}
public static function process(array $company, array $context, string $input): ?array
{
$botCtx = ConversationContext::getOrCreate((int)$company['id'], $context['from'], 'ai');
@@ -189,6 +237,13 @@ Mantén las respuestas concisas (máximo 3 párrafos).
PROMPT;
}
private static function defaultMediaPrompt(array $company): string
{
$name = $company['display_name'] ?? $company['name'] ?? 'la empresa';
return "Eres el asistente virtual de {$name}. El usuario te acaba de enviar un archivo multimedia. "
. "Responde de forma breve y apropiada según los permisos del usuario. Sin saludos largos.";
}
private static function getConfig(array $company): array
{
$json = $company['config_json'] ?? '';