Feature: Manejo inteligente de emojis, stickers y GIFs

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
This commit is contained in:
2026-02-19 16:05:59 -06:00
parent 0c053ab036
commit aa98d9185f
3 changed files with 103 additions and 20 deletions

View File

@@ -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

50
includes/emoji_helper.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
/**
* Verifica si un texto contiene contenido real (no solo emojis, espacios, etc)
* Los emojis se preservan en el texto, solo verificamos que hay más que eso
*/
function hasRealContent(string $text): bool
{
if (empty($text)) {
return false;
}
// Hacer una copia para procesarla sin alterar el original
$clean = trim($text);
if (empty($clean)) {
return false;
}
// Remover emojis y caracteres especiales, preservar solo letras, números y puntuación básica
// Esta expresión regular mantiene letras, números y puntuación, elimina emojis
// Emojis Unicode: rangos múltiples de caracteres
$clean = preg_replace('/[\x{1F300}-\x{1F9FF}]/u', '', $clean); // Emojis de rango alto (0x1F300-0x1F9FF)
$clean = preg_replace('/[\x{2600}-\x{26FF}]/u', '', $clean); // Símbolos de ajedrez, dados, etc
$clean = preg_replace('/[\x{2B50}]/u', '', $clean); // Estrella
$clean = preg_replace('/[\x{00A0}\s]+/u', '', $clean); // Espacios en blanco (incluyendo no-breaking space)
$clean = preg_replace('/[\p{P}]/u', '', $clean); // Puntuación Unicode (incluyendo 👍)
// Si después de remover emojis y espacios no queda nada, es solo emojis
return !empty(trim($clean));
}
/**
* Obtiene el texto sin emojis para análisis de idioma
* NOTA: Los emojis se preservan en el mensaje original para mostrar al usuario
*/
function stripEmojisForDetection(string $text): string
{
$clean = $text;
// Remover emojis de rango alto
$clean = preg_replace('/[\x{1F300}-\x{1F9FF}]/u', '', $clean);
$clean = preg_replace('/[\x{2600}-\x{26FF}]/u', '', $clean);
$clean = preg_replace('/[\x{2B50}]/u', '', $clean);
// Limpiar espacios extras resultantes
$clean = preg_replace('/\s+/u', ' ', $clean);
return trim($clean);
}

View File

@@ -2,6 +2,7 @@
require_once __DIR__ . '/../../includes/db.php';
require_once __DIR__ . '/../../includes/env_loader.php';
require_once __DIR__ . '/../../includes/emoji_helper.php';
require_once __DIR__ . '/../TelegramSender.php';
require_once __DIR__ . '/../actions/TelegramActions.php';
require_once __DIR__ . '/../converters/HtmlToTelegramHtmlConverter.php';
@@ -49,8 +50,18 @@ try {
sendWelcomeMessage($pdo, $sender, $chatId, $username);
}
// Procesar texto de mensajes regulares
if (!empty($text)) {
// Ignorar stickers puros (sin caption)
if (isset($message['sticker']) && empty($caption)) {
exit('Sticker ignorado');
}
// Ignorar GIFs/animaciones puras (sin caption)
if (isset($message['animation']) && empty($caption)) {
exit('GIF ignorado');
}
// Procesar texto de mensajes regulares (solo si tiene contenido real)
if (!empty($text) && hasRealContent($text)) {
if (str_starts_with($text, '/')) {
handleTelegramCommand($sender, $pdo, $chatId, $userId, $text, $username);
} elseif (str_starts_with($text, '#')) {
@@ -60,12 +71,11 @@ try {
handleTelegramMessage($pdo, $sender, $chatId, $userId, $text);
}
}
// Procesar caption de fotos o videos
elseif (!empty($caption)) {
// Solo traducir si hay caption
// Procesar caption de fotos o videos (solo si tiene contenido real)
elseif (!empty($caption) && hasRealContent($caption)) {
handleTelegramMessage($pdo, $sender, $chatId, $userId, $caption);
}
// Si no hay texto ni caption, ignorar (foto/video sin texto)
// Si no hay contenido real (solo emojis, stickers, GIFs), ignorar
} elseif (isset($update['callback_query'])) {
$callbackQuery = $update['callback_query'];
@@ -99,7 +109,9 @@ function registerTelegramUser(PDO $pdo, array $user): void
function handleAutoTranslation(PDO $pdo, Telegram\TelegramSender $sender, src\Translate $translator, int $chatId, string $text): void
{
$keyboard = getTelegramTranslationButtons($pdo, $text);
// Usar texto sin emojis para detección de idioma, pero guardar el original para mostrar
$textForDetection = stripEmojisForDetection($text);
$keyboard = getTelegramTranslationButtons($pdo, $textForDetection ?: $text);
if (!empty($keyboard)) {
$message = "🌐 <b>Traducciones disponibles:</b>\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) {