Fix: Botones de traducción - editar mensaje en Telegram y Discord
This commit is contained in:
@@ -104,15 +104,17 @@ function processScheduledMessages(): array
|
||||
|
||||
// Obtener botones de traducción (convertir HTML a texto plano)
|
||||
$plainText = $schedule['content'];
|
||||
// Convertir saltos de párrafo a saltos de línea
|
||||
$plainText = preg_replace('/<\/p>/i', "\n", $plainText);
|
||||
// Marcar donde hay imágenes
|
||||
$plainText = preg_replace('/<img[^>]+src=["\']([^"\']+)["\'][^>]*>/i', "\n[IMAGEN]\n", $plainText);
|
||||
// Convertir saltos de párrafo y br a saltos de línea dobles
|
||||
$plainText = preg_replace('/<\/p>/i', "\n\n", $plainText);
|
||||
$plainText = preg_replace('/<p[^>]*>/i', '', $plainText);
|
||||
$plainText = preg_replace('/<br\s*\/?>/i', "\n", $plainText);
|
||||
// Eliminar HTML
|
||||
// Eliminar HTML restante
|
||||
$plainText = html_entity_decode(strip_tags($plainText), ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
// Limpiar espacios múltiples pero preservar saltos de línea
|
||||
// Limpiar espacios múltiples pero preservar saltos de línea (máximo 2)
|
||||
$plainText = preg_replace('/[ \t]+/', ' ', $plainText);
|
||||
$plainText = preg_replace('/\n\s*\n/', "\n", $plainText);
|
||||
$plainText = preg_replace('/\n{3,}/', "\n\n", $plainText);
|
||||
$plainText = trim($plainText);
|
||||
$translationButtons = getTranslationButtons($pdo, $plainText);
|
||||
|
||||
@@ -120,24 +122,26 @@ function processScheduledMessages(): array
|
||||
$segments = $sender->parseContent($schedule['content']);
|
||||
|
||||
$messageCount = 0;
|
||||
$totalSegments = count($segments);
|
||||
$currentSegment = 0;
|
||||
|
||||
// Enviar cada segmento en el orden correcto
|
||||
foreach ($segments as $segment) {
|
||||
$currentSegment++;
|
||||
$isLastSegment = ($currentSegment === $totalSegments);
|
||||
|
||||
if ($segment['type'] === 'text') {
|
||||
// Convertir el texto al formato de la plataforma
|
||||
$textContent = ConverterFactory::convert($schedule['platform'], $segment['content']);
|
||||
|
||||
if (!empty(trim($textContent))) {
|
||||
// Agregar botones de traducción al último segmento de texto
|
||||
$buttons = null;
|
||||
if ($segment === end($segments)) {
|
||||
$buttons = $schedule['platform'] === 'telegram'
|
||||
? $translationButtons['telegram']
|
||||
: $translationButtons['discord'];
|
||||
if ($isLastSegment && $schedule['platform'] !== 'telegram') {
|
||||
$buttons = $translationButtons['discord'];
|
||||
}
|
||||
|
||||
if ($schedule['platform'] === 'telegram') {
|
||||
$sender->sendMessage($schedule['platform_id'], $textContent, $buttons);
|
||||
$sender->sendMessage($schedule['platform_id'], $textContent);
|
||||
} else {
|
||||
$sender->sendMessage($schedule['platform_id'], $textContent, null, $buttons);
|
||||
}
|
||||
@@ -146,22 +150,43 @@ function processScheduledMessages(): array
|
||||
} elseif ($segment['type'] === 'image') {
|
||||
$imagePath = $segment['src'];
|
||||
|
||||
// Quitar parámetros de URL si los hay
|
||||
$imgPath = parse_url($imagePath, PHP_URL_PATH) ?: $imagePath;
|
||||
$appUrl = $_ENV['APP_URL'] ?? getenv('APP_URL') ?? '';
|
||||
$baseUrl = rtrim($appUrl, '/');
|
||||
|
||||
if (file_exists($imgPath)) {
|
||||
// Es un archivo local
|
||||
$sender->sendMessageWithAttachments($schedule['platform_id'], '', [$imgPath]);
|
||||
$messageCount++;
|
||||
} elseif (strpos($imagePath, 'http') === 0) {
|
||||
// Es una URL remota
|
||||
$embed = ['image' => ['url' => $imagePath]];
|
||||
$sender->sendMessage($schedule['platform_id'], '', $embed);
|
||||
$buttons = null;
|
||||
if ($isLastSegment && $schedule['platform'] !== 'telegram') {
|
||||
$buttons = $translationButtons['discord'];
|
||||
}
|
||||
|
||||
if ($schedule['platform'] === 'telegram') {
|
||||
if (strpos($imagePath, 'http') !== 0) {
|
||||
$imageUrl = $baseUrl . '/' . ltrim($imagePath, '/');
|
||||
} else {
|
||||
$imageUrl = $imagePath;
|
||||
}
|
||||
$sender->sendPhoto($schedule['platform_id'], $imageUrl);
|
||||
$messageCount++;
|
||||
} else {
|
||||
$imgPath = parse_url($imagePath, PHP_URL_PATH) ?: $imagePath;
|
||||
if (file_exists($imgPath)) {
|
||||
$sender->sendMessageWithImages($schedule['platform_id'], '', [$imgPath], $buttons);
|
||||
$messageCount++;
|
||||
} elseif (strpos($imagePath, 'http') === 0) {
|
||||
$embed = ['image' => ['url' => $imagePath]];
|
||||
$sender->sendMessage($schedule['platform_id'], '', $embed, $buttons);
|
||||
$messageCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Enviar botones de traducción en mensaje separado para Telegram
|
||||
if ($schedule['platform'] === 'telegram' && !empty($translationButtons['telegram'])) {
|
||||
$translationMessage = "🌐 <b>Traducciones disponibles:</b>\nHaz clic en una bandera para ver la traducción";
|
||||
$sender->sendMessage($schedule['platform_id'], $translationMessage, $translationButtons['telegram']);
|
||||
$messageCount++;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO sent_messages (schedule_id, recipient_id, platform_message_id, message_count, sent_at)
|
||||
VALUES (?, ?, ?, ?, NOW())
|
||||
|
||||
Reference in New Issue
Block a user