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
+25 -10
View File
@@ -100,31 +100,46 @@ class BotRouter
return AiBot::process($company, $context, $input);
}
private static function isMediaType(string $inputType): bool
{
return in_array($inputType, ['image', 'audio', 'video', 'document', 'sticker'], true);
}
private static function runHybridBot(array $company, array $context, string $input, string $inputType): ?array
{
$response = self::runNormalBot($company, $context, $input, $inputType);
$config = self::getConfig($company);
// Media (image/audio/video/document) → AI handles it if enabled
if (self::isMediaType($inputType)) {
$aiEnabled = (bool)($config['ai_for_media'] ?? true);
if (!$aiEnabled) {
return self::categoryMenuFallback($company, $context, $config);
}
return AiBot::processMedia($company, $context, $inputType, $input);
}
// Text / interactive / button → NormalBot only, never AI
$response = self::runNormalBot($company, $context, $input, $inputType);
if ($response !== null) {
return $response;
}
// Only escalate to AI if bot type truly needs it; avoid AI for menu-driven bots
$config = self::getConfig($company);
$permType = (string)($context['permission_type'] ?? 1);
$perType = $config['per_type'][$permType] ?? [];
// NormalBot returned null → show the category greeting menu as fallback
return self::categoryMenuFallback($company, $context, $config);
}
// If a greeting_menu is configured for this category, use it as fallback instead of AI
private static function categoryMenuFallback(array $company, array $context, array $config): ?array
{
$permType = (string)($context['permission_type'] ?? 1);
$perType = $config['per_type'][$permType] ?? [];
$greetingMenuKey = $perType['greeting_menu'] ?? null;
if ($greetingMenuKey !== null) {
$menus = array_merge($config['menus'] ?? [], $perType['menus'] ?? []);
if (isset($menus[$greetingMenuKey])) {
self::log("NormalBot no manejó '{$input}' — mostrando menú de categoría en lugar de IA");
return NormalBot::buildGreetingMenu($menus[$greetingMenuKey], $context['from'], $company);
}
}
self::log("NormalBot no manejó '{$input}', escalando a IA");
return self::runAiBot($company, $context, $input);
return null;
}
private static function shouldSkipBot(array $company, array $context): bool