From 74d261a8eda0c8d5d43e8b6d63dc588848ad3da5 Mon Sep 17 00:00:00 2001 From: Mahmoudzadeh Date: Fri, 16 Jun 2023 03:07:30 +0330 Subject: [PATCH 1/6] refactor: update gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index a09c56d..06a55f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /.idea +vendor +test \ No newline at end of file From 029eb08a4634a0a6794be893015a57515f867663 Mon Sep 17 00:00:00 2001 From: Mahmoudzadeh Date: Fri, 16 Jun 2023 03:11:31 +0330 Subject: [PATCH 2/6] refactor(TelegramBotHandler): Support for chat group topics in Telegram --- src/TelegramBotHandler.php | 39 +++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/TelegramBotHandler.php b/src/TelegramBotHandler.php index ae5655b..4328547 100644 --- a/src/TelegramBotHandler.php +++ b/src/TelegramBotHandler.php @@ -35,18 +35,32 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt */ private $chatId; + /** + * + * @var string|null + */ + private $topicId; + /** * @param string $token Telegram bot access token provided by BotFather * @param string $channel Telegram channel name * @inheritDoc */ - public function __construct(string $token, string $chat_id, $level = Logger::DEBUG, bool $bubble = true, $bot_api = 'https://api.telegram.org/bot', $proxy = null) + public function __construct( + string $token, + string $chat_id, + string|null $topic_id = null, + $level = Logger::DEBUG, + bool $bubble = true, + $bot_api = 'https://api.telegram.org/bot', + $proxy = null) { parent::__construct($level, $bubble); $this->token = $token; $this->botApi = $bot_api; $this->chatId = $chat_id; + $this->topicId = $topic_id; $this->level = $level; $this->bubble = $bubble; $this->proxy = $proxy; @@ -63,16 +77,20 @@ protected function write($record): void /** * Send request to @link https://api.telegram.org/bot on SendMessage action. * @param string $message + * @param array $option */ protected function send(string $message, $option = []): void { - try { - if(!isset($option['verify'])){ + try { + + if (!isset($option['verify'])) { $option['verify'] = false; } + if (!is_null($this->proxy)) { $option['proxy'] = $this->proxy; } + $httpClient = new Client($option); if (strpos($this->botApi, 'https://api.telegram.org') === false) { @@ -81,14 +99,17 @@ protected function send(string $message, $option = []): void $url = $this->botApi . $this->token . '/SendMessage'; } + $params = [ + 'text' => $message, + 'chat_id' => $this->chatId, + 'parse_mode' => 'html', + 'disable_web_page_preview' => true, + ]; + $options = [ - 'form_params' => [ - 'text' => $message, - 'chat_id' => $this->chatId, - 'parse_mode' => 'html', - 'disable_web_page_preview' => true, - ] + 'form_params' => $this->topicId === null ? $params + ['message_thread_id' => $this->topicId] : $params ]; + $response = $httpClient->post($url, $options); } catch (\Exception $e) { From d44c274c96670dc0104e8b21351c0ed658301cae Mon Sep 17 00:00:00 2001 From: Mahmoudzadeh Date: Fri, 16 Jun 2023 03:12:43 +0330 Subject: [PATCH 3/6] refactor(TelegramBotHandler): switch from if else structure to ternary operator --- src/TelegramBotHandler.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/TelegramBotHandler.php b/src/TelegramBotHandler.php index 4328547..5d2780d 100644 --- a/src/TelegramBotHandler.php +++ b/src/TelegramBotHandler.php @@ -93,11 +93,9 @@ protected function send(string $message, $option = []): void $httpClient = new Client($option); - if (strpos($this->botApi, 'https://api.telegram.org') === false) { - $url = $this->botApi; - } else { - $url = $this->botApi . $this->token . '/SendMessage'; - } + $url = !str_contains($this->botApi, 'https://api.telegram.org') + ? $this->botApi + : $this->botApi . $this->token . '/SendMessage'; $params = [ 'text' => $message, From 38032a1a51f3f8a11b5c578773b0005e304574b7 Mon Sep 17 00:00:00 2001 From: Mahmoudzadeh Date: Fri, 16 Jun 2023 03:14:20 +0330 Subject: [PATCH 4/6] refactor(ReadMe): add topic id to config channel logs --- README.md | 1 + composer.lock | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 composer.lock diff --git a/README.md b/README.md index 473fae3..b3e5116 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Open config/logging.php and change the file 'handler_with' => [ 'token' => env('LOG_TELEGRAM_BOT_TOKEN'), 'chat_id' => env('LOG_TELEGRAM_CHAT_ID'), + 'topic_id' => env('LOG_TELEGRAM_TOPIC_ID',null), 'bot_api' => env('LOG_TELEGRAM_BOT_API', 'https://api.telegram.org/bot'), 'proxy' => env('LOG_TELEGRAM_BOT_PROXY', null), ], diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..7c8f013 --- /dev/null +++ b/composer.lock @@ -0,0 +1,172 @@ +{ + "_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": "2d9e03b1d86c084a93ac550f7917f439", + "packages": [ + { + "name": "monolog/monolog", + "version": "3.3.1", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "9b5daeaffce5b926cac47923798bba91059e60e2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/9b5daeaffce5b926cac47923798bba91059e60e2", + "reference": "9b5daeaffce5b926cac47923798bba91059e60e2", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "psr/log": "^2.0 || ^3.0" + }, + "provide": { + "psr/log-implementation": "3.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^3.0", + "doctrine/couchdb": "~1.0@dev", + "elasticsearch/elasticsearch": "^7 || ^8", + "ext-json": "*", + "graylog2/gelf-php": "^1.4.2 || ^2@dev", + "guzzlehttp/guzzle": "^7.4.5", + "guzzlehttp/psr7": "^2.2", + "mongodb/mongodb": "^1.8", + "php-amqplib/php-amqplib": "~2.4 || ^3", + "phpstan/phpstan": "^1.9", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-strict-rules": "^1.4", + "phpunit/phpunit": "^9.5.26", + "predis/predis": "^1.1 || ^2", + "ruflin/elastica": "^7", + "symfony/mailer": "^5.4 || ^6", + "symfony/mime": "^5.4 || ^6" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-curl": "Required to send log messages using the IFTTTHandler, the LogglyHandler, the SendGridHandler, the SlackWebhookHandler or the TelegramBotHandler", + "ext-mbstring": "Allow to work properly with unicode symbols", + "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", + "ext-openssl": "Required to send log messages using SSL", + "ext-sockets": "Allow sending log messages to a Syslog server (via UDP driver)", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "https://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "https://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "support": { + "issues": "https://github.com/Seldaek/monolog/issues", + "source": "https://github.com/Seldaek/monolog/tree/3.3.1" + }, + "funding": [ + { + "url": "https://github.com/Seldaek", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/monolog/monolog", + "type": "tidelift" + } + ], + "time": "2023-02-06T13:46:10+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" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "ext-curl": "*" + }, + "platform-dev": [], + "plugin-api-version": "2.3.0" +} From d73d2e6a0c5939a654e91fcd870cce88f1c5cdd0 Mon Sep 17 00:00:00 2001 From: Mahmoudzadeh Date: Fri, 16 Jun 2023 10:19:21 +0330 Subject: [PATCH 5/6] refactor(ReadMe): update document --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index b3e5116..1927e82 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,12 @@ Add the following variables to your .env file. ```php LOG_TELEGRAM_BOT_TOKEN= LOG_TELEGRAM_CHAT_ID= + +# If chat groups are used instead of telegram channels, +# and the ability to set topics on groups is enabled, +# this configuration can be utilized. +LOG_TELEGRAM_TOPIC_ID= + #LOG_TELEGRAM_BOT_API='https://api.telegram.org/bot' # add tor proxy for restricted country #LOG_TELEGRAM_BOT_PROXY='socks5h://localhost:9050' From e36045fece46586a6640d44c69e248190e2b221a Mon Sep 17 00:00:00 2001 From: Mahmoudzadeh Date: Fri, 16 Jun 2023 10:20:27 +0330 Subject: [PATCH 6/6] refactor(TelegramBotHandler): add doc block and set form_params with topic id --- src/TelegramBotHandler.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/TelegramBotHandler.php b/src/TelegramBotHandler.php index 5d2780d..2465993 100644 --- a/src/TelegramBotHandler.php +++ b/src/TelegramBotHandler.php @@ -36,7 +36,9 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt private $chatId; /** - * + * If chat groups are used instead of telegram channels, + * and the ability to set topics on groups is enabled, + * this configuration can be utilized. * @var string|null */ private $topicId; @@ -105,7 +107,7 @@ protected function send(string $message, $option = []): void ]; $options = [ - 'form_params' => $this->topicId === null ? $params + ['message_thread_id' => $this->topicId] : $params + 'form_params' => $this->topicId !== null ? $params + ['message_thread_id' => $this->topicId] : $params ]; $response = $httpClient->post($url, $options);