From ec03f11b931808ae9e5af388201ad56209f0fc82 Mon Sep 17 00:00:00 2001 From: TangMo Date: Fri, 4 Apr 2025 01:02:00 +0000 Subject: [PATCH] Parse markdown --- composer.json | 2 +- composer.lock | 262 ++++++++++++------------- src/Commands/GenericmessageCommand.php | 23 ++- 3 files changed, 146 insertions(+), 141 deletions(-) diff --git a/composer.json b/composer.json index 979c14d..200f0ea 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "php": ">=8.1", "deeplcom/deepl-php": "^1.1.0", "longman/telegram-bot": "*", - "steelywing/chinese": "^0.5.3" + "lucadevelop/telegram-entities-decoder": "^1.3" }, "config": { "allow-plugins": { diff --git a/composer.lock b/composer.lock index b0a9741..f1a5257 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a0a65830cd3d7e541e4610730263798f", + "content-hash": "be8152ca483086150648bd025aef879e", "packages": [ { "name": "deeplcom/deepl-php", @@ -71,22 +71,22 @@ }, { "name": "guzzlehttp/guzzle", - "version": "7.5.0", + "version": "7.9.3", "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", - "reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba" + "reference": "7b2f29fe81dc4da0ca0ea7d42107a0845946ea77" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/b50a2a1251152e43f6a37f0fa053e730a67d25ba", - "reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/7b2f29fe81dc4da0ca0ea7d42107a0845946ea77", + "reference": "7b2f29fe81dc4da0ca0ea7d42107a0845946ea77", "shasum": "" }, "require": { "ext-json": "*", - "guzzlehttp/promises": "^1.5", - "guzzlehttp/psr7": "^1.9 || ^2.4", + "guzzlehttp/promises": "^1.5.3 || ^2.0.3", + "guzzlehttp/psr7": "^2.7.0", "php": "^7.2.5 || ^8.0", "psr/http-client": "^1.0", "symfony/deprecation-contracts": "^2.2 || ^3.0" @@ -95,10 +95,11 @@ "psr/http-client-implementation": "1.0" }, "require-dev": { - "bamarni/composer-bin-plugin": "^1.8.1", + "bamarni/composer-bin-plugin": "^1.8.2", "ext-curl": "*", - "php-http/client-integration-tests": "^3.0", - "phpunit/phpunit": "^8.5.29 || ^9.5.23", + "guzzle/client-integration-tests": "3.0.2", + "php-http/message-factory": "^1.1", + "phpunit/phpunit": "^8.5.39 || ^9.6.20", "psr/log": "^1.1 || ^2.0 || ^3.0" }, "suggest": { @@ -111,9 +112,6 @@ "bamarni-bin": { "bin-links": true, "forward-command": false - }, - "branch-alias": { - "dev-master": "7.5-dev" } }, "autoload": { @@ -179,7 +177,7 @@ ], "support": { "issues": "https://github.com/guzzle/guzzle/issues", - "source": "https://github.com/guzzle/guzzle/tree/7.5.0" + "source": "https://github.com/guzzle/guzzle/tree/7.9.3" }, "funding": [ { @@ -195,38 +193,37 @@ "type": "tidelift" } ], - "time": "2022-08-28T15:39:27+00:00" + "time": "2025-03-27T13:37:11+00:00" }, { "name": "guzzlehttp/promises", - "version": "1.5.2", + "version": "2.2.0", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", - "reference": "b94b2807d85443f9719887892882d0329d1e2598" + "reference": "7c69f28996b0a6920945dd20b3857e499d9ca96c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/b94b2807d85443f9719887892882d0329d1e2598", - "reference": "b94b2807d85443f9719887892882d0329d1e2598", + "url": "https://api.github.com/repos/guzzle/promises/zipball/7c69f28996b0a6920945dd20b3857e499d9ca96c", + "reference": "7c69f28996b0a6920945dd20b3857e499d9ca96c", "shasum": "" }, "require": { - "php": ">=5.5" + "php": "^7.2.5 || ^8.0" }, "require-dev": { - "symfony/phpunit-bridge": "^4.4 || ^5.1" + "bamarni/composer-bin-plugin": "^1.8.2", + "phpunit/phpunit": "^8.5.39 || ^9.6.20" }, "type": "library", "extra": { - "branch-alias": { - "dev-master": "1.5-dev" + "bamarni-bin": { + "bin-links": true, + "forward-command": false } }, "autoload": { - "files": [ - "src/functions_include.php" - ], "psr-4": { "GuzzleHttp\\Promise\\": "src/" } @@ -263,7 +260,7 @@ ], "support": { "issues": "https://github.com/guzzle/promises/issues", - "source": "https://github.com/guzzle/promises/tree/1.5.2" + "source": "https://github.com/guzzle/promises/tree/2.2.0" }, "funding": [ { @@ -279,26 +276,26 @@ "type": "tidelift" } ], - "time": "2022-08-28T14:55:35+00:00" + "time": "2025-03-27T13:27:01+00:00" }, { "name": "guzzlehttp/psr7", - "version": "2.4.1", + "version": "2.7.1", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", - "reference": "69568e4293f4fa993f3b0e51c9723e1e17c41379" + "reference": "c2270caaabe631b3b44c85f99e5a04bbb8060d16" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/psr7/zipball/69568e4293f4fa993f3b0e51c9723e1e17c41379", - "reference": "69568e4293f4fa993f3b0e51c9723e1e17c41379", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/c2270caaabe631b3b44c85f99e5a04bbb8060d16", + "reference": "c2270caaabe631b3b44c85f99e5a04bbb8060d16", "shasum": "" }, "require": { "php": "^7.2.5 || ^8.0", "psr/http-factory": "^1.0", - "psr/http-message": "^1.0", + "psr/http-message": "^1.1 || ^2.0", "ralouphie/getallheaders": "^3.0" }, "provide": { @@ -306,9 +303,9 @@ "psr/http-message-implementation": "1.0" }, "require-dev": { - "bamarni/composer-bin-plugin": "^1.8.1", - "http-interop/http-factory-tests": "^0.9", - "phpunit/phpunit": "^8.5.29 || ^9.5.23" + "bamarni/composer-bin-plugin": "^1.8.2", + "http-interop/http-factory-tests": "0.9.0", + "phpunit/phpunit": "^8.5.39 || ^9.6.20" }, "suggest": { "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" @@ -318,9 +315,6 @@ "bamarni-bin": { "bin-links": true, "forward-command": false - }, - "branch-alias": { - "dev-master": "2.4-dev" } }, "autoload": { @@ -382,7 +376,7 @@ ], "support": { "issues": "https://github.com/guzzle/psr7/issues", - "source": "https://github.com/guzzle/psr7/tree/2.4.1" + "source": "https://github.com/guzzle/psr7/tree/2.7.1" }, "funding": [ { @@ -398,20 +392,20 @@ "type": "tidelift" } ], - "time": "2022-08-28T14:45:39+00:00" + "time": "2025-03-27T12:30:47+00:00" }, { "name": "longman/telegram-bot", - "version": "0.79.0", + "version": "0.83.1", "source": { "type": "git", "url": "https://github.com/php-telegram-bot/core.git", - "reference": "097177ca85627bfa5d0d9e679714a0b1353d49de" + "reference": "ca9eeb1c9e51c39a33070efeba01aac105ed9aa7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-telegram-bot/core/zipball/097177ca85627bfa5d0d9e679714a0b1353d49de", - "reference": "097177ca85627bfa5d0d9e679714a0b1353d49de", + "url": "https://api.github.com/repos/php-telegram-bot/core/zipball/ca9eeb1c9e51c39a33070efeba01aac105ed9aa7", + "reference": "ca9eeb1c9e51c39a33070efeba01aac105ed9aa7", "shasum": "" }, "require": { @@ -420,7 +414,7 @@ "ext-mbstring": "*", "ext-pdo": "*", "guzzlehttp/guzzle": "^6.0|^7.0", - "php": "^7.3|^8.0", + "php": "^8.1", "psr/log": "^1.1|^2.0|^3.0" }, "require-dev": { @@ -430,6 +424,11 @@ "squizlabs/php_codesniffer": "^3.6" }, "type": "library", + "extra": { + "branch-alias": { + "dev-develop": "0.83.x-dev" + } + }, "autoload": { "psr-4": { "Longman\\TelegramBot\\": "src" @@ -499,7 +498,48 @@ "type": "tidelift" } ], - "time": "2022-09-04T13:22:41+00:00" + "time": "2024-05-30T21:05:25+00:00" + }, + { + "name": "lucadevelop/telegram-entities-decoder", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/LucaDevelop/telegram-entities-decoder.git", + "reference": "32cbdce21d02306c75708d6fbb1b3241069b018e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/LucaDevelop/telegram-entities-decoder/zipball/32cbdce21d02306c75708d6fbb1b3241069b018e", + "reference": "32cbdce21d02306c75708d6fbb1b3241069b018e", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": ">=7.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "lucadevelop\\TelegramEntitiesDecoder\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "LucaDevelop", + "email": "lsirri.gm@gmail.com" + } + ], + "description": "Telegram entities decoder in PHP", + "support": { + "issues": "https://github.com/LucaDevelop/telegram-entities-decoder/issues", + "source": "https://github.com/LucaDevelop/telegram-entities-decoder/tree/v1.3.1" + }, + "time": "2024-02-01T17:06:13+00:00" }, { "name": "php-http/discovery", @@ -638,21 +678,21 @@ }, { "name": "psr/http-client", - "version": "1.0.1", + "version": "1.0.3", "source": { "type": "git", "url": "https://github.com/php-fig/http-client.git", - "reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621" + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-client/zipball/2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", - "reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", + "url": "https://api.github.com/repos/php-fig/http-client/zipball/bb5906edc1c324c9a05aa0873d40117941e5fa90", + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90", "shasum": "" }, "require": { "php": "^7.0 || ^8.0", - "psr/http-message": "^1.0" + "psr/http-message": "^1.0 || ^2.0" }, "type": "library", "extra": { @@ -672,7 +712,7 @@ "authors": [ { "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "homepage": "https://www.php-fig.org/" } ], "description": "Common interface for HTTP clients", @@ -684,27 +724,27 @@ "psr-18" ], "support": { - "source": "https://github.com/php-fig/http-client/tree/master" + "source": "https://github.com/php-fig/http-client" }, - "time": "2020-06-29T06:28:15+00:00" + "time": "2023-09-23T14:17:50+00:00" }, { "name": "psr/http-factory", - "version": "1.0.1", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/php-fig/http-factory.git", - "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be" + "reference": "2b4765fddfe3b508ac62f829e852b1501d3f6e8a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-factory/zipball/12ac7fcd07e5b077433f5f2bee95b3a771bf61be", - "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be", + "url": "https://api.github.com/repos/php-fig/http-factory/zipball/2b4765fddfe3b508ac62f829e852b1501d3f6e8a", + "reference": "2b4765fddfe3b508ac62f829e852b1501d3f6e8a", "shasum": "" }, "require": { - "php": ">=7.0.0", - "psr/http-message": "^1.0" + "php": ">=7.1", + "psr/http-message": "^1.0 || ^2.0" }, "type": "library", "extra": { @@ -724,10 +764,10 @@ "authors": [ { "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "homepage": "https://www.php-fig.org/" } ], - "description": "Common interfaces for PSR-7 HTTP message factories", + "description": "PSR-17: Common interfaces for PSR-7 HTTP message factories", "keywords": [ "factory", "http", @@ -739,31 +779,31 @@ "response" ], "support": { - "source": "https://github.com/php-fig/http-factory/tree/master" + "source": "https://github.com/php-fig/http-factory" }, - "time": "2019-04-30T12:38:16+00:00" + "time": "2024-04-15T12:06:14+00:00" }, { "name": "psr/http-message", - "version": "1.0.1", + "version": "2.0", "source": { "type": "git", "url": "https://github.com/php-fig/http-message.git", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/402d35bcb92c70c026d1a6a9883f06b2ead23d71", + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71", "shasum": "" }, "require": { - "php": ">=5.3.0" + "php": "^7.2 || ^8.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -778,7 +818,7 @@ "authors": [ { "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "homepage": "https://www.php-fig.org/" } ], "description": "Common interface for HTTP messages", @@ -792,22 +832,22 @@ "response" ], "support": { - "source": "https://github.com/php-fig/http-message/tree/master" + "source": "https://github.com/php-fig/http-message/tree/2.0" }, - "time": "2016-08-06T14:39:51+00:00" + "time": "2023-04-04T09:54:51+00:00" }, { "name": "psr/log", - "version": "3.0.0", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", - "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", "shasum": "" }, "require": { @@ -842,9 +882,9 @@ "psr-3" ], "support": { - "source": "https://github.com/php-fig/log/tree/3.0.0" + "source": "https://github.com/php-fig/log/tree/3.0.2" }, - "time": "2021-07-14T16:46:02+00:00" + "time": "2024-09-11T13:17:53+00:00" }, { "name": "ralouphie/getallheaders", @@ -890,58 +930,18 @@ }, "time": "2019-03-08T08:55:37+00:00" }, - { - "name": "steelywing/chinese", - "version": "0.5.3", - "source": { - "type": "git", - "url": "https://github.com/steelywing/PHP-Chinese.git", - "reference": "8ff0b4c1275718222f5ffd3991dddab1af06be1d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/steelywing/PHP-Chinese/zipball/8ff0b4c1275718222f5ffd3991dddab1af06be1d", - "reference": "8ff0b4c1275718222f5ffd3991dddab1af06be1d", - "shasum": "" - }, - "type": "library", - "autoload": { - "psr-4": { - "SteelyWing\\Chinese\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Steely Wing", - "email": "steely.wing@gmail.com" - } - ], - "description": "中文繁簡轉換 Convert Traditional and Simplified Chinese", - "support": { - "issues": "https://github.com/steelywing/PHP-Chinese/issues", - "source": "https://github.com/steelywing/PHP-Chinese/tree/0.5.3" - }, - "time": "2019-06-10T01:58:44+00:00" - }, { "name": "symfony/deprecation-contracts", - "version": "v3.1.1", + "version": "v3.5.1", "source": { "type": "git", "url": "https://github.com/symfony/deprecation-contracts.git", - "reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918" + "reference": "74c71c939a79f7d5bf3c1ce9f5ea37ba0114c6f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918", - "reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/74c71c939a79f7d5bf3c1ce9f5ea37ba0114c6f6", + "reference": "74c71c939a79f7d5bf3c1ce9f5ea37ba0114c6f6", "shasum": "" }, "require": { @@ -949,12 +949,12 @@ }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "3.1-dev" - }, "thanks": { - "name": "symfony/contracts", - "url": "https://github.com/symfony/contracts" + "url": "https://github.com/symfony/contracts", + "name": "symfony/contracts" + }, + "branch-alias": { + "dev-main": "3.5-dev" } }, "autoload": { @@ -979,7 +979,7 @@ "description": "A generic function and convention to trigger deprecation notices", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v3.1.1" + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.5.1" }, "funding": [ { @@ -995,7 +995,7 @@ "type": "tidelift" } ], - "time": "2022-02-25T11:15:52+00:00" + "time": "2024-09-25T14:20:29+00:00" } ], "packages-dev": [], diff --git a/src/Commands/GenericmessageCommand.php b/src/Commands/GenericmessageCommand.php index cd4e2d6..13986a0 100644 --- a/src/Commands/GenericmessageCommand.php +++ b/src/Commands/GenericmessageCommand.php @@ -6,7 +6,8 @@ use Longman\TelegramBot\Commands\SystemCommand; use Longman\TelegramBot\Entities\ServerResponse; use Longman\TelegramBot\Request; use DeepL\Translator; -use SteelyWing\Chinese\Chinese; +use DeepL\TranslateTextOptions; +use lucadevelop\TelegramEntitiesDecoder\EntityDecoder; // This class name is magic class GenericmessageCommand extends SystemCommand @@ -39,26 +40,30 @@ class GenericmessageCommand extends SystemCommand $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)) { $sourceLang = 'en'; - $targetLang = 'zh'; + $targetLang = 'zh-hans'; } else { $sourceLang = 'zh'; $targetLang = 'en-US'; - $text = (new Chinese())->to(Chinese::ZH_HANS, $text); } $deeplOptions = [ - 'formality' => 'prefer_less', - 'model_type' => 'prefer_quality_optimized', + TranslateTextOptions::FORMALITY => 'prefer_less', + TranslateTextOptions::MODEL_TYPE => 'prefer_quality_optimized', + TranslateTextOptions::TAG_HANDLING => 'html', ]; try { $glosasaryConfig = json_decode(file_get_contents(__DIR__ . '/../../glossary.json'), true); - if (isset($glosasaryConfig[$sourceLang][$targetLang])) { - $deeplOptions['glossary'] = $glosasaryConfig[$sourceLang][$targetLang]; + $targetLangKey = explode('-', $targetLang)[0]; + if (isset($glosasaryConfig[$sourceLang][$targetLangKey])) { + $deeplOptions[TranslateTextOptions::GLOSSARY] = $glosasaryConfig[$sourceLang][$targetLangKey]; + } } catch(\Exception $e) {} $this->translator = new Translator($deepl_api_key); try { - $translated = $this->translator->translateText($text, $sourceLang, $targetLang, $deeplOptions); + $decoded_text = (new EntityDecoder('HTML'))->decode(json_decode($message->toJson())); + $translated = $this->translator->translateText($decoded_text, $sourceLang, $targetLang, $deeplOptions); + $translated = $translated->text; } catch (\Exception $e) { return $this->replyToChat(get_class($e) . ': ' . $e->getMessage()); // ?? var_export($e, true) } @@ -68,6 +73,6 @@ class GenericmessageCommand extends SystemCommand $translated = $message_forwarded_from . ': ' . $translated; } - return $this->replyToChat($translated, ['disable_notification' => true]); + return $this->replyToChat($translated, ['disable_notification' => true, 'parse_mode' => 'HTML']); } } \ No newline at end of file