From c613af2b38ac117f84a3c3f46c26a3a91fe9cb3b Mon Sep 17 00:00:00 2001 From: TangMo Date: Fri, 29 Aug 2025 01:16:42 +0000 Subject: [PATCH] Only use HTML mode when necessary --- src/Commands/GenericmessageCommand.php | 30 ++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Commands/GenericmessageCommand.php b/src/Commands/GenericmessageCommand.php index 9922983..272ec55 100644 --- a/src/Commands/GenericmessageCommand.php +++ b/src/Commands/GenericmessageCommand.php @@ -27,7 +27,7 @@ class GenericmessageCommand extends SystemCommand $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); - if(!in_array($chat_id, $allowed_chat_ids)) { + if (!in_array($chat_id, $allowed_chat_ids)) { return $this->replyToChat("Chat ID `$chat_id` not allowed"); } @@ -38,37 +38,45 @@ class GenericmessageCommand extends SystemCommand $text_chara_count = count(preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY)); $text_english_count = count(preg_split('~[^a-z 0-9\'\",.]*~i', $text, 0, PREG_SPLIT_NO_EMPTY)); - if ($text_english_count >= ($text_chara_count*2 /3)) { + if ($text_english_count >= ($text_chara_count * 2 / 3)) { $sourceLang = 'en'; $targetLang = 'zh-hans'; } else { $sourceLang = 'zh'; $targetLang = 'en-US'; } + $htmlMode = $message->getEntities() && count($message->getEntities()) > 0; $deeplOptions = [ TranslateTextOptions::FORMALITY => 'prefer_less', TranslateTextOptions::MODEL_TYPE => 'prefer_quality_optimized', - TranslateTextOptions::TAG_HANDLING => 'html', TranslateTextOptions::SPLIT_SENTENCES => 'on', ]; + if ($htmlMode) { + $deeplOptions[TranslateTextOptions::TAG_HANDLING] = 'html'; + } if (isset($deepl_glossary_id)) { $deeplOptions[TranslateTextOptions::GLOSSARY] = $deepl_glossary_id; } - + $this->translator = new Translator($deepl_api_key); try { - $decoded_text = (new EntityDecoder('HTML'))->decode(json_decode($message->toJson())); - $translated = $this->translator->translateText($decoded_text, $sourceLang, $targetLang, $deeplOptions); + if ($htmlMode) { + $text = (new EntityDecoder('HTML'))->decode(json_decode($message->toJson())); + // $text = str_replace("\n", '
', $text); + } + $translated = $this->translator->translateText($text, $sourceLang, $targetLang, $deeplOptions); $translated = $translated->text; } catch (\Exception $e) { return $this->replyToChat(get_class($e) . ': ' . $e->getMessage()); // ?? var_export($e, true) } + $translated = str_replace('<BR />', "\n", $translated); + // $translated = str_replace('
', "\n", $translated); - $message_forwarded_from = $message->getForwardFrom()?->getFirstName() ?? $message->getForwardSenderName(); - if ($message_forwarded_from !== null) { - $translated = $message_forwarded_from . ': ' . $translated; + $forwardedFrom = $message->getForwardFrom(); + if ($forwardedFrom) { + $translated = $forwardedFrom['first_name'] . ': ' . $translated; } - return $this->replyToChat($translated, ['disable_notification' => true, 'parse_mode' => 'HTML']); + return $this->replyToChat($translated, ['disable_notification' => true, 'parse_mode' => $htmlMode ? 'HTML' : null]); } -} \ No newline at end of file +}