Mejoras en el envío de plantillas con imágenes
- Agregar detección de URLs de Discord gifts para evitar botones de traducción - Enviar imágenes en orden correcto (texto-imagen-texto-imagen) en Discord y Telegram - Usar APP_URL del .env para las URLs de imágenes - Agregar funciones sendContentWithOrderedImagesAndButtons en ambos bots
This commit is contained in:
@@ -95,34 +95,29 @@ class DiscordSender
|
||||
$result = null;
|
||||
|
||||
if (!empty($images)) {
|
||||
// Verificar si las imágenes son locales o URLs
|
||||
$localImages = [];
|
||||
$remoteImages = [];
|
||||
|
||||
$appUrl = $_ENV['APP_URL'] ?? getenv('APP_URL') ?? '';
|
||||
$baseUrl = rtrim($appUrl, '/');
|
||||
|
||||
foreach ($images as $imageUrl) {
|
||||
if (strpos($imageUrl, 'http') === 0) {
|
||||
// Es una URL remota
|
||||
$remoteImages[] = $imageUrl;
|
||||
} elseif (file_exists($imageUrl)) {
|
||||
// Es un archivo local
|
||||
$localImages[] = $imageUrl;
|
||||
} else {
|
||||
// Convertir a URL usando APP_URL
|
||||
$remoteImages[] = $baseUrl . '/' . ltrim($imageUrl, '/');
|
||||
}
|
||||
}
|
||||
|
||||
// Enviar imágenes locales como adjuntos
|
||||
if (!empty($localImages)) {
|
||||
$result = $this->sendMessageWithAttachments($channelId, $content, $localImages);
|
||||
} else {
|
||||
$result = $this->sendMessage($channelId, $content, null, $buttons);
|
||||
}
|
||||
// Enviar todas las imágenes como embeds
|
||||
$result = $this->sendMessage($channelId, $content, null, $buttons);
|
||||
|
||||
// Enviar imágenes remotas como embeds
|
||||
foreach ($remoteImages as $imageUrl) {
|
||||
$embed = [
|
||||
'image' => ['url' => $imageUrl]
|
||||
];
|
||||
$result = $this->sendMessage($channelId, '', $embed, $buttons);
|
||||
$buttons = null; // Solo enviar botones en el primer mensaje
|
||||
$result = $this->sendMessage($channelId, '', $embed);
|
||||
$buttons = null;
|
||||
}
|
||||
} else {
|
||||
$result = $this->sendMessage($channelId, $content, null, $buttons);
|
||||
@@ -136,26 +131,48 @@ class DiscordSender
|
||||
* Divide el contenido en segmentos y los envía manteniendo el orden
|
||||
*/
|
||||
public function sendContentWithOrderedImages(string $channelId, array $segments): void
|
||||
{
|
||||
$this->sendContentWithOrderedImagesAndButtons($channelId, $segments, null);
|
||||
}
|
||||
|
||||
public function sendContentWithOrderedImagesAndButtons(string $channelId, array $segments, ?array $buttons = null): void
|
||||
{
|
||||
$channelId = $this->resolveUserToDmChannel($channelId);
|
||||
|
||||
$appUrl = $_ENV['APP_URL'] ?? getenv('APP_URL') ?? '';
|
||||
$baseUrl = rtrim($appUrl, '/');
|
||||
|
||||
$totalSegments = count($segments);
|
||||
$currentSegment = 0;
|
||||
|
||||
foreach ($segments as $segment) {
|
||||
$currentSegment++;
|
||||
$isLastSegment = ($currentSegment === $totalSegments);
|
||||
|
||||
if ($segment['type'] === 'text') {
|
||||
// Enviar texto
|
||||
if (!empty(trim($segment['content']))) {
|
||||
$this->sendMessage($channelId, $segment['content']);
|
||||
// Solo enviar botones en el último segmento de texto
|
||||
if ($isLastSegment && $buttons) {
|
||||
$this->sendMessage($channelId, $segment['content'], null, $buttons);
|
||||
} else {
|
||||
$this->sendMessage($channelId, $segment['content']);
|
||||
}
|
||||
}
|
||||
} elseif ($segment['type'] === 'image') {
|
||||
// Enviar imagen
|
||||
$imagePath = $segment['src'];
|
||||
|
||||
if (strpos($imagePath, 'http') === 0) {
|
||||
// URL remota - enviar como embed
|
||||
$embed = ['image' => ['url' => $imagePath]];
|
||||
} else {
|
||||
$imageUrl = $baseUrl . '/' . ltrim($imagePath, '/');
|
||||
$embed = ['image' => ['url' => $imageUrl]];
|
||||
}
|
||||
|
||||
// Solo enviar botones en el último segmento
|
||||
if ($isLastSegment && $buttons) {
|
||||
$this->sendMessage($channelId, '', $embed, $buttons);
|
||||
} else {
|
||||
$this->sendMessage($channelId, '', $embed);
|
||||
} elseif (file_exists($imagePath)) {
|
||||
// Archivo local - enviar como adjunto
|
||||
$this->sendMessageWithAttachments($channelId, '', [$imagePath]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,40 +113,31 @@ function handleTemplateCommand(PDO $pdo, Message $message, string $command): voi
|
||||
$template = $stmt->fetch();
|
||||
|
||||
if ($template) {
|
||||
require_once __DIR__ . '/discord/converters/HtmlToDiscordMarkdownConverter.php';
|
||||
require_once __DIR__ . '/discord/DiscordSender.php';
|
||||
require_once __DIR__ . '/src/Translate.php';
|
||||
|
||||
$converter = new \Discord\Converters\HtmlToDiscordMarkdownConverter();
|
||||
|
||||
$images = $converter->extractImages($template['message_content']);
|
||||
$contentWithoutImages = $converter->removeImages($template['message_content']);
|
||||
$content = $converter->convert($contentWithoutImages);
|
||||
|
||||
require_once __DIR__ . '/discord/DiscordSender.php';
|
||||
require_once __DIR__ . '/src/Translate.php';
|
||||
|
||||
$sender = new \Discord\DiscordSender();
|
||||
|
||||
$plainText = $template['message_content'];
|
||||
$plainText = preg_replace('/<br\s*\/?>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<\/p>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<p[^>]*>/i', '', $plainText);
|
||||
$plainText = strip_tags($plainText);
|
||||
$plainText = html_entity_decode($plainText, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$plainText = trim($plainText);
|
||||
|
||||
$translationButtons = getDiscordTranslationButtons($pdo, $plainText);
|
||||
|
||||
if (!empty($images)) {
|
||||
$sender->sendMessageWithImages((string)$message->channel_id, $content, $images, $translationButtons);
|
||||
$sender = new \Discord\DiscordSender();
|
||||
|
||||
// Parsear el contenido manteniendo el orden texto-imagen
|
||||
$segments = $sender->parseContent($template['message_content']);
|
||||
|
||||
if (!empty($segments)) {
|
||||
// Obtener texto completo para botones de traducción
|
||||
$plainText = $template['message_content'];
|
||||
$plainText = preg_replace('/<br\s*\/?>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<\/p>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<p[^>]*>/i', '', $plainText);
|
||||
$plainText = strip_tags($plainText);
|
||||
$plainText = html_entity_decode($plainText, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$plainText = trim($plainText);
|
||||
|
||||
$translationButtons = getDiscordTranslationButtons($pdo, $plainText);
|
||||
|
||||
// Enviar contenido ordenado con botones
|
||||
$sender->sendContentWithOrderedImagesAndButtons((string)$message->channel_id, $segments, $translationButtons);
|
||||
}
|
||||
} else {
|
||||
$sender->sendMessage((string)$message->channel_id, $content, null, $translationButtons);
|
||||
$message->channel->sendMessage("❌ Plantilla no encontrada: #{$command}");
|
||||
}
|
||||
|
||||
} else {
|
||||
$message->channel->sendMessage("❌ Plantilla no encontrada: #{$command}");
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$message->channel->sendMessage("❌ Error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Verifica si un texto contiene contenido real (no solo emojis, espacios, etc)
|
||||
* Verifica si un texto contiene contenido real (no solo emojis, URLs de GIFs, espacios, etc)
|
||||
* Los emojis se preservan en el texto, solo verificamos que hay más que eso
|
||||
*/
|
||||
function hasRealContent(string $text): bool
|
||||
@@ -10,23 +10,34 @@ function hasRealContent(string $text): bool
|
||||
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 👍)
|
||||
// Remover URLs de GIFs y medias (tenor, giphy, media.tenor, cdn.discordapp, etc)
|
||||
$clean = preg_replace('/https?:\/\/(www\.)?(tenor\.com|giphy\.com|media\.tenor\.com|cdn\.discordapp\.com|media\.discord|gifcdn|gfycat\.com|reddit\.com\/r\/[^\/]+\/comments\/)[^\s]*/i', '', $clean);
|
||||
|
||||
// Remover URLs de Discord gifts
|
||||
$clean = preg_replace('/https?:\/\/(www\.)?(discord\.gift|discord\.com\/gifts)[^\s]*/i', '', $clean);
|
||||
|
||||
// Remover otros URLs comunes (http://..., https://...)
|
||||
$clean = preg_replace('/https?:\/\/[^\s]+/i', '', $clean);
|
||||
|
||||
$clean = trim($clean);
|
||||
|
||||
if (empty($clean)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remover emojis y caracteres especiales
|
||||
$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);
|
||||
$clean = preg_replace('/[\x{00A0}\s]+/u', '', $clean);
|
||||
$clean = preg_replace('/[\p{P}]/u', '', $clean);
|
||||
|
||||
// Si después de remover emojis y espacios no queda nada, es solo emojis
|
||||
return !empty(trim($clean));
|
||||
}
|
||||
|
||||
|
||||
@@ -247,20 +247,43 @@ class TelegramSender
|
||||
*/
|
||||
public function sendContentWithOrderedImages(int $chatId, array $segments): void
|
||||
{
|
||||
$this->sendContentWithOrderedImagesAndButtons($chatId, $segments, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviar contenido con texto e imágenes en el orden correcto con botones
|
||||
*/
|
||||
public function sendContentWithOrderedImagesAndButtons(int $chatId, array $segments, ?array $buttons = null): void
|
||||
{
|
||||
$appUrl = $_ENV['APP_URL'] ?? getenv('APP_URL') ?? '';
|
||||
$baseUrl = rtrim($appUrl, '/');
|
||||
|
||||
$totalSegments = count($segments);
|
||||
$currentSegment = 0;
|
||||
|
||||
foreach ($segments as $segment) {
|
||||
$currentSegment++;
|
||||
$isLastSegment = ($currentSegment === $totalSegments);
|
||||
|
||||
if ($segment['type'] === 'text') {
|
||||
// Enviar texto
|
||||
if (!empty(trim($segment['content']))) {
|
||||
$this->sendMessage($chatId, $segment['content']);
|
||||
if ($isLastSegment && $buttons) {
|
||||
$this->sendMessage($chatId, $segment['content'], $buttons);
|
||||
} else {
|
||||
$this->sendMessage($chatId, $segment['content']);
|
||||
}
|
||||
}
|
||||
} elseif ($segment['type'] === 'image') {
|
||||
$imagePath = $segment['src'];
|
||||
|
||||
if (file_exists($imagePath)) {
|
||||
// Es un archivo local
|
||||
$this->sendPhoto($chatId, $imagePath);
|
||||
} elseif (strpos($imagePath, 'http') === 0) {
|
||||
// Es una URL remota
|
||||
// Convertir ruta local a URL usando APP_URL
|
||||
if (strpos($imagePath, 'http') !== 0) {
|
||||
$imagePath = $baseUrl . '/' . ltrim($imagePath, '/');
|
||||
}
|
||||
|
||||
if ($isLastSegment && $buttons) {
|
||||
$this->sendPhoto($chatId, $imagePath, null, $buttons);
|
||||
} else {
|
||||
$this->sendPhoto($chatId, $imagePath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,26 +335,25 @@ function sendTemplateByCommand(PDO $pdo, Telegram\TelegramSender $sender, int $c
|
||||
$template = $stmt->fetch();
|
||||
|
||||
if ($template) {
|
||||
$converter = new Telegram\Converters\HtmlToTelegramHtmlConverter();
|
||||
$content = $converter->convert($template['message_content']);
|
||||
// Parsear el contenido manteniendo el orden texto-imagen
|
||||
$segments = $sender->parseContent($template['message_content']);
|
||||
|
||||
$plainText = $template['message_content'];
|
||||
$plainText = preg_replace('/<br\s*\/?>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<\/p>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<p[^>]*>/i', '', $plainText);
|
||||
$plainText = strip_tags($plainText);
|
||||
$plainText = html_entity_decode($plainText, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
// Limpiar espacios múltiples pero preservar saltos de línea
|
||||
$plainText = preg_replace('/[ \t]+/', ' ', $plainText);
|
||||
$plainText = preg_replace('/\n\s*\n/', "\n", $plainText);
|
||||
$plainText = trim($plainText);
|
||||
|
||||
$translationButtons = getTelegramTranslationButtons($pdo, $plainText);
|
||||
|
||||
if ($translationButtons) {
|
||||
$sender->sendMessage($chatId, $content, $translationButtons);
|
||||
} else {
|
||||
$sender->sendMessage($chatId, $content);
|
||||
if (!empty($segments)) {
|
||||
// Obtener texto completo para botones de traducción
|
||||
$plainText = $template['message_content'];
|
||||
$plainText = preg_replace('/<br\s*\/?>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<\/p>/i', "\n", $plainText);
|
||||
$plainText = preg_replace('/<p[^>]*>/i', '', $plainText);
|
||||
$plainText = strip_tags($plainText);
|
||||
$plainText = html_entity_decode($plainText, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$plainText = preg_replace('/[ \t]+/', ' ', $plainText);
|
||||
$plainText = preg_replace('/\n\s*\n/', "\n", $plainText);
|
||||
$plainText = trim($plainText);
|
||||
|
||||
$translationButtons = getTelegramTranslationButtons($pdo, $plainText);
|
||||
|
||||
// Enviar contenido ordenado con botones
|
||||
$sender->sendContentWithOrderedImagesAndButtons($chatId, $segments, $translationButtons);
|
||||
}
|
||||
} else {
|
||||
$sender->sendMessage($chatId, "❌ Plantilla no encontrada: #{$command}");
|
||||
|
||||
Reference in New Issue
Block a user