First iteration

This commit is contained in:
2022-09-30 01:46:02 +00:00
parent b290f82dc5
commit e3cf8ddca4
7 changed files with 1021 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/vendor/
composer
env.php

10
composer.json Normal file
View File

@@ -0,0 +1,10 @@
{
"name": "tangmo/trans-bot",
"type": "project",
"require": {
"php": ">=8.1",
"deeplcom/deepl-php": "^1.1.0",
"longman/telegram-bot": "*",
"steelywing/chinese": "^0.5.3"
}
}

869
composer.lock generated Normal file
View File

@@ -0,0 +1,869 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "a0a65830cd3d7e541e4610730263798f",
"packages": [
{
"name": "deeplcom/deepl-php",
"version": "v1.1.0",
"source": {
"type": "git",
"url": "https://github.com/DeepLcom/deepl-php.git",
"reference": "bb6ab74e3d90a543d7effd5ee2578d450a744c12"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/DeepLcom/deepl-php/zipball/bb6ab74e3d90a543d7effd5ee2578d450a744c12",
"reference": "bb6ab74e3d90a543d7effd5ee2578d450a744c12",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"php": ">=7.3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"phpunit/phpunit": "^9",
"ramsey/uuid": "^4.2",
"squizlabs/php_codesniffer": "^3.3"
},
"type": "library",
"autoload": {
"psr-4": {
"DeepL\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "DeepL SE",
"email": "open-source@deepl.com"
}
],
"description": "Official DeepL API Client Library",
"keywords": [
"api",
"deepl",
"translation",
"translator"
],
"support": {
"issues": "https://github.com/DeepLcom/deepl-php/issues",
"source": "https://github.com/DeepLcom/deepl-php/tree/v1.1.0"
},
"time": "2022-09-28T08:44:58+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "7.5.0",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/b50a2a1251152e43f6a37f0fa053e730a67d25ba",
"reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba",
"shasum": ""
},
"require": {
"ext-json": "*",
"guzzlehttp/promises": "^1.5",
"guzzlehttp/psr7": "^1.9 || ^2.4",
"php": "^7.2.5 || ^8.0",
"psr/http-client": "^1.0",
"symfony/deprecation-contracts": "^2.2 || ^3.0"
},
"provide": {
"psr/http-client-implementation": "1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.1",
"ext-curl": "*",
"php-http/client-integration-tests": "^3.0",
"phpunit/phpunit": "^8.5.29 || ^9.5.23",
"psr/log": "^1.1 || ^2.0 || ^3.0"
},
"suggest": {
"ext-curl": "Required for CURL handler support",
"ext-intl": "Required for Internationalized Domain Name (IDN) support",
"psr/log": "Required for using the Log middleware"
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
},
"branch-alias": {
"dev-master": "7.5-dev"
}
},
"autoload": {
"files": [
"src/functions_include.php"
],
"psr-4": {
"GuzzleHttp\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Jeremy Lindblom",
"email": "jeremeamia@gmail.com",
"homepage": "https://github.com/jeremeamia"
},
{
"name": "George Mponos",
"email": "gmponos@gmail.com",
"homepage": "https://github.com/gmponos"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/Nyholm"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://github.com/sagikazarmark"
},
{
"name": "Tobias Schultze",
"email": "webmaster@tubo-world.de",
"homepage": "https://github.com/Tobion"
}
],
"description": "Guzzle is a PHP HTTP client library",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"psr-18",
"psr-7",
"rest",
"web service"
],
"support": {
"issues": "https://github.com/guzzle/guzzle/issues",
"source": "https://github.com/guzzle/guzzle/tree/7.5.0"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://github.com/Nyholm",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/guzzlehttp/guzzle",
"type": "tidelift"
}
],
"time": "2022-08-28T15:39:27+00:00"
},
{
"name": "guzzlehttp/promises",
"version": "1.5.2",
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "b94b2807d85443f9719887892882d0329d1e2598"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/b94b2807d85443f9719887892882d0329d1e2598",
"reference": "b94b2807d85443f9719887892882d0329d1e2598",
"shasum": ""
},
"require": {
"php": ">=5.5"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4 || ^5.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.5-dev"
}
},
"autoload": {
"files": [
"src/functions_include.php"
],
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/Nyholm"
},
{
"name": "Tobias Schultze",
"email": "webmaster@tubo-world.de",
"homepage": "https://github.com/Tobion"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"support": {
"issues": "https://github.com/guzzle/promises/issues",
"source": "https://github.com/guzzle/promises/tree/1.5.2"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://github.com/Nyholm",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises",
"type": "tidelift"
}
],
"time": "2022-08-28T14:55:35+00:00"
},
{
"name": "guzzlehttp/psr7",
"version": "2.4.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "69568e4293f4fa993f3b0e51c9723e1e17c41379"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/69568e4293f4fa993f3b0e51c9723e1e17c41379",
"reference": "69568e4293f4fa993f3b0e51c9723e1e17c41379",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"ralouphie/getallheaders": "^3.0"
},
"provide": {
"psr/http-factory-implementation": "1.0",
"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"
},
"suggest": {
"laminas/laminas-httphandlerrunner": "Emit PSR-7 responses"
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
},
"branch-alias": {
"dev-master": "2.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "George Mponos",
"email": "gmponos@gmail.com",
"homepage": "https://github.com/gmponos"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/Nyholm"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://github.com/sagikazarmark"
},
{
"name": "Tobias Schultze",
"email": "webmaster@tubo-world.de",
"homepage": "https://github.com/Tobion"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://sagikazarmark.hu"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"psr-7",
"request",
"response",
"stream",
"uri",
"url"
],
"support": {
"issues": "https://github.com/guzzle/psr7/issues",
"source": "https://github.com/guzzle/psr7/tree/2.4.1"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://github.com/Nyholm",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7",
"type": "tidelift"
}
],
"time": "2022-08-28T14:45:39+00:00"
},
{
"name": "longman/telegram-bot",
"version": "0.79.0",
"source": {
"type": "git",
"url": "https://github.com/php-telegram-bot/core.git",
"reference": "097177ca85627bfa5d0d9e679714a0b1353d49de"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-telegram-bot/core/zipball/097177ca85627bfa5d0d9e679714a0b1353d49de",
"reference": "097177ca85627bfa5d0d9e679714a0b1353d49de",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-pdo": "*",
"guzzlehttp/guzzle": "^6.0|^7.0",
"php": "^7.3|^8.0",
"psr/log": "^1.1|^2.0|^3.0"
},
"require-dev": {
"dms/phpunit-arraysubset-asserts": "^0.2",
"monolog/monolog": "^2.1",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6"
},
"type": "library",
"autoload": {
"psr-4": {
"Longman\\TelegramBot\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Avtandil Kikabidze aka LONGMAN",
"email": "akalongman@gmail.com",
"homepage": "http://longman.me",
"role": "Developer"
},
{
"name": "Armando Lüscher",
"email": "armando@noplanman.ch",
"homepage": "https://noplanman.ch",
"role": "Developer"
},
{
"name": "PHP Telegram Bot Team",
"homepage": "https://github.com/php-telegram-bot/core/graphs/contributors",
"role": "Developer"
}
],
"description": "PHP Telegram bot",
"homepage": "https://github.com/php-telegram-bot/core",
"keywords": [
"api",
"bot",
"telegram"
],
"support": {
"issues": "https://github.com/php-telegram-bot/core/issues",
"source": "https://github.com/php-telegram-bot/core"
},
"funding": [
{
"url": "https://github.com/php-telegram-bot/core#donate",
"type": "custom"
},
{
"url": "https://github.com/noplanman",
"type": "github"
},
{
"url": "https://ko-fi.com/phptelegrambot",
"type": "ko_fi"
},
{
"url": "https://liberapay.com/PHP-Telegram-Bot",
"type": "liberapay"
},
{
"url": "https://opencollective.com/php-telegram-bot",
"type": "open_collective"
},
{
"url": "https://www.patreon.com/phptelegrambot",
"type": "patreon"
},
{
"url": "https://tidelift.com/funding/github/packagist/longman/telegram-bot",
"type": "tidelift"
}
],
"time": "2022-09-04T13:22:41+00:00"
},
{
"name": "psr/http-client",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-client.git",
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-client/zipball/2dfb5f6c5eff0e91e20e913f8c5452ed95b86621",
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621",
"shasum": ""
},
"require": {
"php": "^7.0 || ^8.0",
"psr/http-message": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Client\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP clients",
"homepage": "https://github.com/php-fig/http-client",
"keywords": [
"http",
"http-client",
"psr",
"psr-18"
],
"support": {
"source": "https://github.com/php-fig/http-client/tree/master"
},
"time": "2020-06-29T06:28:15+00:00"
},
{
"name": "psr/http-factory",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-factory.git",
"reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-factory/zipball/12ac7fcd07e5b077433f5f2bee95b3a771bf61be",
"reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be",
"shasum": ""
},
"require": {
"php": ">=7.0.0",
"psr/http-message": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interfaces for PSR-7 HTTP message factories",
"keywords": [
"factory",
"http",
"message",
"psr",
"psr-17",
"psr-7",
"request",
"response"
],
"support": {
"source": "https://github.com/php-fig/http-factory/tree/master"
},
"time": "2019-04-30T12:38:16+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"support": {
"source": "https://github.com/php-fig/http-message/tree/master"
},
"time": "2016-08-06T14:39:51+00:00"
},
{
"name": "psr/log",
"version": "3.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001",
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001",
"shasum": ""
},
"require": {
"php": ">=8.0.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Log\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"homepage": "https://github.com/php-fig/log",
"keywords": [
"log",
"psr",
"psr-3"
],
"support": {
"source": "https://github.com/php-fig/log/tree/3.0.0"
},
"time": "2021-07-14T16:46:02+00:00"
},
{
"name": "ralouphie/getallheaders",
"version": "3.0.3",
"source": {
"type": "git",
"url": "https://github.com/ralouphie/getallheaders.git",
"reference": "120b605dfeb996808c31b6477290a714d356e822"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822",
"reference": "120b605dfeb996808c31b6477290a714d356e822",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^5 || ^6.5"
},
"type": "library",
"autoload": {
"files": [
"src/getallheaders.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ralph Khattar",
"email": "ralph.khattar@gmail.com"
}
],
"description": "A polyfill for getallheaders.",
"support": {
"issues": "https://github.com/ralouphie/getallheaders/issues",
"source": "https://github.com/ralouphie/getallheaders/tree/develop"
},
"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",
"source": {
"type": "git",
"url": "https://github.com/symfony/deprecation-contracts.git",
"reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918",
"reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918",
"shasum": ""
},
"require": {
"php": ">=8.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "3.1-dev"
},
"thanks": {
"name": "symfony/contracts",
"url": "https://github.com/symfony/contracts"
}
},
"autoload": {
"files": [
"function.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"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"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2022-02-25T11:15:52+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=8.1"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

8
env.sample.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
// Rename me to env.php
$bot_api_key = '1234567890:Telegram_bot_api_key';
$bot_username = 'telegram_bot_username';
$hook_url = 'https://my.server.com/path/to/hook.php';
putenv('DEEPL_API_KEY=some-uuid-12345-abcde:fx');

13
public_html/hook.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../env.php';
try {
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
$telegram->addCommandsPath(realpath(__DIR__ . '/../src/Commands'));
// Handle telegram webhook request
$telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// file_put_contents(__DIR__ . '/../out.txt', $e->getMessage());
}

13
set_web_hook.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/env.php';
try {
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
$result = $telegram->setWebhook($hook_url);
if ($result->isOk()) {
echo $result->getDescription();
}
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e->getMessage();
}

View File

@@ -0,0 +1,105 @@
<?php
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request;
use DeepL\Translator;
use SteelyWing\Chinese\Chinese;
// This class name is magic
class GenericmessageCommand extends SystemCommand
{
/** @var string */
protected $name = 'genericmessage';
/** @var string */
protected $description = 'Handle generic message';
/** @var string */
protected $version = '1.0.0';
private Translator $translator;
private const ALLOWED_CHAT_ID = [
123, 456, 789
];
private const ZH_TO_EN = [
'兽设' => 'fursona',
'兽人' => 'furry character',
'兽控' => 'furry',
'毛毛' => 'fursuit',
'兽装' => 'fursuit',
'大佬' => 'da lao',
'丸吞' => 'vore',
'哦不' => 'oh no',
'哦哇塞' => 'o wa sai'
];
private const EN_TO_ZH = [
'fursona' => '兽设',
'furry character' => '兽人',
'furry' => '兽控',
'fursuit' => '兽装',
'sese' => '色色',
'sheshe' => '射射',
'wun tun' => '丸吞',
'vore' => '丸吞',
'money power' => '钞能力',
'oh no' => '哦不',
];
public function execute(): ServerResponse
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
if(!in_array($chat_id, self::ALLOWED_CHAT_ID)) {
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => "Chat ID $chat_id not allowed",
]);
}
$text = $message->getText(true) ?? $message->getCaption();
if (empty($text)) {
return Request::emptyResponse();
}
$text_chara_count = count(preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY));
$text_english_count = count(preg_split('~[^a-z]*~i', $text, 0, PREG_SPLIT_NO_EMPTY));
if ($text_english_count >= ($text_chara_count / 2)) {
$sourceLang = 'en';
$targetLang = 'zh';
$glossary = self::EN_TO_ZH;
} else {
$sourceLang = 'zh';
$targetLang = 'en-US';
$glossary = self::ZH_TO_EN;
$text = (new Chinese())->to(Chinese::ZH_HANS, $text);
}
foreach ($glossary as $search => $replace) {
$text = str_replace($search, $replace, $text);
}
$this->translator = new Translator(getenv('DEEPL_API_KEY'));
try {
$translated = $this->translator->translateText($text, $sourceLang, $targetLang);
} catch (\Exception $e) {
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => get_class($e) . ': ' . $e->getMessage(), // ?? var_export($e, true),
]);
}
$message_forwarded_from = $message->getForwardFrom()?->getFirstName();
if ($message_forwarded_from !== null) {
$translated = $message_forwarded_from . ': ' . $translated;
}
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => $translated,
]);
//return Request::emptyResponse();
}
}