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 diff --git a/README.md b/README.md index 473fae3..1927e82 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), ], @@ -53,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' 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" +} diff --git a/src/TelegramBotHandler.php b/src/TelegramBotHandler.php index ae5655b..2465993 100644 --- a/src/TelegramBotHandler.php +++ b/src/TelegramBotHandler.php @@ -35,18 +35,34 @@ 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; + /** * @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,32 +79,37 @@ 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) { - $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, + '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) {