Fix: Botones de traducción - editar mensaje en Telegram y Discord
This commit is contained in:
@@ -27,7 +27,7 @@ class TelegramSender
|
||||
return $this->request('sendMessage', $data);
|
||||
}
|
||||
|
||||
public function editMessageText(int $chatId, int $messageId, string $text, ?array $keyboard = null, ?string $parseMode = 'HTML'): array
|
||||
public function editMessageText(int $chatId, int $messageId, string $text, ?array $keyboard = null, ?string $parseMode = 'HTML', ?string $inlineMessageId = null): array
|
||||
{
|
||||
$data = [
|
||||
'chat_id' => $chatId,
|
||||
@@ -35,6 +35,12 @@ class TelegramSender
|
||||
'text' => $text,
|
||||
];
|
||||
|
||||
if ($inlineMessageId) {
|
||||
unset($data['chat_id']);
|
||||
unset($data['message_id']);
|
||||
$data['inline_message_id'] = $inlineMessageId;
|
||||
}
|
||||
|
||||
if ($parseMode) {
|
||||
$data['parse_mode'] = $parseMode;
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ if (!$update) {
|
||||
exit('No update');
|
||||
}
|
||||
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " -收到更新\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - Received update\n", FILE_APPEND);
|
||||
|
||||
if (isset($update['callback_query'])) {
|
||||
$callbackData = $update['callback_query']['data'] ?? 'no data';
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - Callback: $callbackData\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - Callback: $callbackData\n", FILE_APPEND);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -82,11 +82,12 @@ try {
|
||||
$callbackData = $callbackQuery['data'];
|
||||
$chatId = $callbackQuery['message']['chat']['id'];
|
||||
$messageId = $callbackQuery['message']['message_id'];
|
||||
$inlineMessageId = $callbackQuery['inline_message_id'] ?? null;
|
||||
$userId = $callbackQuery['from']['id'] ?? '';
|
||||
|
||||
error_log("Telegram callback - chatId: $chatId, messageId: $messageId, userId: $userId, callbackData: $callbackData");
|
||||
|
||||
handleTelegramCallback($pdo, $sender, $translator, $chatId, $messageId, $callbackQuery['id'], $callbackData);
|
||||
handleTelegramCallback($pdo, $sender, $translator, $chatId, $messageId, $inlineMessageId, $callbackQuery['id'], $callbackData);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
@@ -119,7 +120,7 @@ function handleAutoTranslation(PDO $pdo, Telegram\TelegramSender $sender, src\Tr
|
||||
}
|
||||
}
|
||||
|
||||
function getTelegramTranslationButtons(PDO $pdo, string $text, ?string $excludeLang = null): ?array
|
||||
function getTelegramTranslationButtons(PDO $pdo, string $text): ?array
|
||||
{
|
||||
try {
|
||||
$stmt = $pdo->query("SELECT language_code, flag_emoji FROM supported_languages WHERE is_active = 1");
|
||||
@@ -135,10 +136,6 @@ function getTelegramTranslationButtons(PDO $pdo, string $text, ?string $excludeL
|
||||
|
||||
$buttons = [];
|
||||
foreach ($activeLanguages as $lang) {
|
||||
if ($excludeLang && $lang['language_code'] === $excludeLang) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$callbackData = "translate:" . $lang['language_code'] . ":" . $textHash;
|
||||
|
||||
$buttons[] = [
|
||||
@@ -360,7 +357,7 @@ function sendTemplateByCommand(PDO $pdo, Telegram\TelegramSender $sender, int $c
|
||||
}
|
||||
}
|
||||
|
||||
function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\Translate $translator, int $chatId, int $messageId, string $callbackQueryId, string $callbackData): void
|
||||
function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\Translate $translator, int $chatId, int $messageId, ?string $inlineMessageId, string $callbackQueryId, string $callbackData): void
|
||||
{
|
||||
$parts = explode(':', $callbackData, 3);
|
||||
$action = $parts[0] ?? '';
|
||||
@@ -369,7 +366,7 @@ function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\T
|
||||
$targetLang = $parts[1] ?? 'es';
|
||||
$textHash = $parts[2] ?? '';
|
||||
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - targetLang: $targetLang, textHash: $textHash\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - targetLang: $targetLang, textHash: $textHash\n", FILE_APPEND);
|
||||
|
||||
// Recuperar texto de la base de datos
|
||||
$stmt = $pdo->prepare("SELECT original_text FROM translation_cache WHERE text_hash = ?");
|
||||
@@ -377,7 +374,7 @@ function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\T
|
||||
$row = $stmt->fetch();
|
||||
|
||||
if (!$row) {
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - ERROR: No text found\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - ERROR: No text found\n", FILE_APPEND);
|
||||
$sender->answerCallbackQuery($callbackQueryId, "❌ Error: Texto no encontrado");
|
||||
return;
|
||||
}
|
||||
@@ -385,7 +382,7 @@ function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\T
|
||||
$originalText = $row['original_text'];
|
||||
|
||||
if (empty($originalText)) {
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - ERROR: Empty text\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - ERROR: Empty text\n", FILE_APPEND);
|
||||
$sender->answerCallbackQuery($callbackQueryId, "❌ Error: No se pudo recuperar el texto");
|
||||
return;
|
||||
}
|
||||
@@ -394,11 +391,11 @@ function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\T
|
||||
// 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);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - sourceLang: $sourceLang, targetLang: $targetLang, originalText: " . substr($originalText, 0, 50) . "\n", FILE_APPEND);
|
||||
|
||||
// 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);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - translated: $translated\n", FILE_APPEND);
|
||||
|
||||
if ($translated) {
|
||||
// Limpiar TODAS las etiquetas HTML problemáticas (con espacios como < b > o </ b >)
|
||||
@@ -422,16 +419,16 @@ function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\T
|
||||
|
||||
$sender->answerCallbackQuery($callbackQueryId, "", false);
|
||||
|
||||
$keyboard = getTelegramTranslationButtons($pdo, $originalText, $targetLang);
|
||||
$keyboard = getTelegramTranslationButtons($pdo, $originalText);
|
||||
|
||||
$result = $sender->editMessageText($chatId, $messageId, "🌐 <b>Traducción (" . strtoupper($targetLang) . "):</b>\n\n" . $translated, $keyboard);
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - editMessageText result: " . json_encode($result) . "\n", FILE_APPEND);
|
||||
$result = $sender->editMessageText($chatId, $messageId, "🌐 <b>Traducción (" . strtoupper($targetLang) . "):</b>\n\n" . $translated, $keyboard, 'HTML', $inlineMessageId);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - editMessageText result: " . json_encode($result) . "\n", FILE_APPEND);
|
||||
} else {
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - ERROR: Translation failed\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - ERROR: Translation failed\n", FILE_APPEND);
|
||||
$sender->answerCallbackQuery($callbackQueryId, "❌ Error al traducir el mensaje", false);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - EXCEPTION: " . $e->getMessage() . "\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . '/../../logs/telegram_debug.log', date('Y-m-d H:i:s') . " - EXCEPTION: " . $e->getMessage() . "\n", FILE_APPEND);
|
||||
$sender->answerCallbackQuery($callbackQueryId, "❌ Error en la traducción", false);
|
||||
}
|
||||
} elseif ($action === 'chat_mode') {
|
||||
|
||||
Reference in New Issue
Block a user