From aa98d9185fa6d4e2f1108149f927291b735e0025 Mon Sep 17 00:00:00 2001 From: nickpons666 Date: Thu, 19 Feb 2026 16:05:59 -0600 Subject: [PATCH] Feature: Manejo inteligente de emojis, stickers y GIFs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cambios principales: - Nuevo archivo: includes/emoji_helper.php * hasRealContent(): Detecta si hay contenido real (no solo emojis) * stripEmojisForDetection(): Preserva emojis visuales pero mejora detección de idioma Telegram (telegram_bot_webhook.php): - Ignorar stickers puros sin caption - Ignorar GIFs/animaciones puras sin caption - Procesar caption si tiene contenido real - Usar stripEmojisForDetection() para mejor precisión en idioma Discord (discord_bot.php): - Ignorar mensajes con solo emojis/espacios - Usar stripEmojisForDetection() para detección más precisa - Intentar preservar emojis en la traducción Comportamiento: - Solo 👍 = Ignorado - Hola 👍 = Traducido como 'Hola' (emoji se preserva) - {sticker_sin_caption} = Ignorado - {gif_sin_caption} = Ignorado - {foto_con_caption} = Caption traducido --- discord_bot.php | 34 +++++++++++---- includes/emoji_helper.php | 50 +++++++++++++++++++++++ telegram/webhook/telegram_bot_webhook.php | 39 ++++++++++++------ 3 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 includes/emoji_helper.php diff --git a/discord_bot.php b/discord_bot.php index 15b9aba..4265496 100755 --- a/discord_bot.php +++ b/discord_bot.php @@ -3,6 +3,7 @@ require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/includes/db.php'; require_once __DIR__ . '/includes/env_loader.php'; +require_once __DIR__ . '/includes/emoji_helper.php'; use Discord\Discord; use Discord\Parts\Channel\Message; @@ -331,16 +332,17 @@ function sendToN8NIA(Message $message, string $userMessage): void function handleAutoTranslationWithButtons(PDO $pdo, Message $message, string $text): void { try { - // Si no hay texto, ignorar (ej: solo imagen/video sin caption) - if (empty(trim($text))) { + // Si no hay contenido real (solo emojis, espacios), ignorar + if (!hasRealContent($text)) { return; } require_once __DIR__ . '/src/Translate.php'; $translator = new src\Translate(); - // Detectar idioma del mensaje - $detectedLang = $translator->detectLanguage($text) ?? 'es'; + // Detectar idioma del mensaje (sin emojis para mejor precisión) + $textForDetection = stripEmojisForDetection($text); + $detectedLang = $translator->detectLanguage($textForDetection) ?? 'es'; // Obtener idiomas activos de la base de datos $stmt = $pdo->query("SELECT language_code, flag_emoji FROM supported_languages WHERE is_active = 1"); @@ -531,8 +533,10 @@ function handleTranslateInteraction($interaction, string $customId): void $interaction->respondWithMessage($loadingBuilder, true); // Ahora traducir (esto puede tardar) - $sourceLang = $translator->detectLanguage($originalText) ?? 'es'; - $translated = $translator->translate($originalText, $sourceLang, $targetLang); + // Usar texto sin emojis para detectar idioma y traducir con mayor precisión + $textForDetection = stripEmojisForDetection($originalText); + $sourceLang = $translator->detectLanguage($textForDetection) ?? 'es'; + $translated = $translator->translate($textForDetection, $sourceLang, $targetLang); if ($translated) { // Limpiar todo el HTML @@ -546,8 +550,24 @@ function handleTranslateInteraction($interaction, string $customId): void $translated = preg_replace('/\n\s*\n/', "\n", $translated); // Actualizar la respuesta con la traducción real + // Preservar emojis del original si los hay + $displayText = trim($translated); + if (strpos($originalText, '👍') !== false || preg_match('/[\x{1F300}-\x{1F9FF}]/u', $originalText)) { + // Si el original tenía emojis, intentar identificarlos + // Para mensajes simples, los emojis generalmente están al inicio o final + $emojiPrefix = ''; + $emojiSuffix = ''; + if (preg_match('/^([\x{1F300}-\x{1F9FF}]+)\s*/u', $originalText, $match)) { + $emojiPrefix = $match[1] . ' '; + } + if (preg_match('/\s*([\x{1F300}-\x{1F9FF}]+)$/u', $originalText, $match)) { + $emojiSuffix = ' ' . $match[1]; + } + $displayText = $emojiPrefix . $displayText . $emojiSuffix; + } + $finalBuilder = \Discord\Builders\MessageBuilder::new() - ->setContent("🌐 **Traducción (" . strtoupper($targetLang) . "):**\n\n" . trim($translated)); + ->setContent("🌐 **Traducción (" . strtoupper($targetLang) . "):**\n\n" . $displayText); $interaction->updateOriginalResponse($finalBuilder); } else { // Actualizar con error diff --git a/includes/emoji_helper.php b/includes/emoji_helper.php new file mode 100644 index 0000000..ef63e9e --- /dev/null +++ b/includes/emoji_helper.php @@ -0,0 +1,50 @@ +Traducciones disponibles:\nHaz clic en una bandera para ver la traducción"; @@ -256,8 +268,8 @@ function handleTelegramCommand(Telegram\TelegramSender $sender, PDO $pdo, int $c function handleTelegramMessage(PDO $pdo, Telegram\TelegramSender $sender, int $chatId, int $userId, string $text): void { - // Si no hay texto, ignorar (ej: solo imagen/video sin caption) - if (empty(trim($text))) { + // Si no hay contenido real (solo emojis, espacios), ignorar + if (!hasRealContent($text)) { return; } @@ -380,12 +392,13 @@ function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\T } try { - // Obtener el idioma original - $sourceLang = $translator->detectLanguage($originalText) ?? 'es'; + // Obtener el idioma original (usar texto sin emojis para mayor precisión) + $textForDetection = stripEmojisForDetection($originalText); + $sourceLang = $translator->detectLanguage($textForDetection) ?? 'es'; file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - sourceLang: $sourceLang, targetLang: $targetLang, originalText: " . substr($originalText, 0, 50) . "\n", FILE_APPEND); - // Traducir - $translated = $translator->translate($originalText, $sourceLang, $targetLang); + // Traducir (usar texto sin emojis para evitar interferencias) + $translated = $translator->translate($textForDetection ?: $originalText, $sourceLang, $targetLang); file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - translated: $translated\n", FILE_APPEND); if ($translated) {