diff --git a/discord/DiscordSender.php b/discord/DiscordSender.php index 36f8fe9..c083102 100755 --- a/discord/DiscordSender.php +++ b/discord/DiscordSender.php @@ -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]); } } } diff --git a/discord_bot.php b/discord_bot.php index e9db2e9..bb77363 100755 --- a/discord_bot.php +++ b/discord_bot.php @@ -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('//i', "\n", $plainText); - $plainText = preg_replace('/<\/p>/i', "\n", $plainText); - $plainText = preg_replace('/]*>/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('//i', "\n", $plainText); + $plainText = preg_replace('/<\/p>/i', "\n", $plainText); + $plainText = preg_replace('/]*>/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()); } diff --git a/includes/emoji_helper.php b/includes/emoji_helper.php index ef63e9e..d7c3deb 100644 --- a/includes/emoji_helper.php +++ b/includes/emoji_helper.php @@ -1,7 +1,7 @@ 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); } } diff --git a/telegram/webhook/telegram_bot_webhook.php b/telegram/webhook/telegram_bot_webhook.php index d62803d..aaa8d5d 100755 --- a/telegram/webhook/telegram_bot_webhook.php +++ b/telegram/webhook/telegram_bot_webhook.php @@ -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('//i', "\n", $plainText); - $plainText = preg_replace('/<\/p>/i', "\n", $plainText); - $plainText = preg_replace('/]*>/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('//i', "\n", $plainText); + $plainText = preg_replace('/<\/p>/i', "\n", $plainText); + $plainText = preg_replace('/]*>/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}");