Skip to content

Commit e08255b

Browse files
authored
Merge pull request #3 from saMahmoudzadeh/master
Support for chat group topics in Telegram
2 parents ca89b26 + e36045f commit e08255b

File tree

4 files changed

+216
-14
lines changed

4 files changed

+216
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/.idea
2+
vendor
3+
test

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Open config/logging.php and change the file
3535
'handler_with' => [
3636
'token' => env('LOG_TELEGRAM_BOT_TOKEN'),
3737
'chat_id' => env('LOG_TELEGRAM_CHAT_ID'),
38+
'topic_id' => env('LOG_TELEGRAM_TOPIC_ID',null),
3839
'bot_api' => env('LOG_TELEGRAM_BOT_API', 'https://api.telegram.org/bot'),
3940
'proxy' => env('LOG_TELEGRAM_BOT_PROXY', null),
4041
],
@@ -53,6 +54,12 @@ Add the following variables to your .env file.
5354
```php
5455
LOG_TELEGRAM_BOT_TOKEN=
5556
LOG_TELEGRAM_CHAT_ID=
57+
58+
# If chat groups are used instead of telegram channels,
59+
# and the ability to set topics on groups is enabled,
60+
# this configuration can be utilized.
61+
LOG_TELEGRAM_TOPIC_ID=
62+
5663
#LOG_TELEGRAM_BOT_API='https://api.telegram.org/bot'
5764
# add tor proxy for restricted country
5865
#LOG_TELEGRAM_BOT_PROXY='socks5h://localhost:9050'

composer.lock

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TelegramBotHandler.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,34 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt
3535
*/
3636
private $chatId;
3737

38+
/**
39+
* If chat groups are used instead of telegram channels,
40+
* and the ability to set topics on groups is enabled,
41+
* this configuration can be utilized.
42+
* @var string|null
43+
*/
44+
private $topicId;
45+
3846
/**
3947
* @param string $token Telegram bot access token provided by BotFather
4048
* @param string $channel Telegram channel name
4149
* @inheritDoc
4250
*/
43-
public function __construct(string $token, string $chat_id, $level = Logger::DEBUG, bool $bubble = true, $bot_api = 'https://api.telegram.org/bot', $proxy = null)
51+
public function __construct(
52+
string $token,
53+
string $chat_id,
54+
string|null $topic_id = null,
55+
$level = Logger::DEBUG,
56+
bool $bubble = true,
57+
$bot_api = 'https://api.telegram.org/bot',
58+
$proxy = null)
4459
{
4560
parent::__construct($level, $bubble);
4661

4762
$this->token = $token;
4863
$this->botApi = $bot_api;
4964
$this->chatId = $chat_id;
65+
$this->topicId = $topic_id;
5066
$this->level = $level;
5167
$this->bubble = $bubble;
5268
$this->proxy = $proxy;
@@ -63,32 +79,37 @@ protected function write($record): void
6379
/**
6480
* Send request to @link https://api.telegram.org/bot on SendMessage action.
6581
* @param string $message
82+
* @param array $option
6683
*/
6784
protected function send(string $message, $option = []): void
6885
{
69-
try {
70-
if(!isset($option['verify'])){
86+
try {
87+
88+
if (!isset($option['verify'])) {
7189
$option['verify'] = false;
7290
}
91+
7392
if (!is_null($this->proxy)) {
7493
$option['proxy'] = $this->proxy;
7594
}
95+
7696
$httpClient = new Client($option);
7797

78-
if (strpos($this->botApi, 'https://api.telegram.org') === false) {
79-
$url = $this->botApi;
80-
} else {
81-
$url = $this->botApi . $this->token . '/SendMessage';
82-
}
98+
$url = !str_contains($this->botApi, 'https://api.telegram.org')
99+
? $this->botApi
100+
: $this->botApi . $this->token . '/SendMessage';
101+
102+
$params = [
103+
'text' => $message,
104+
'chat_id' => $this->chatId,
105+
'parse_mode' => 'html',
106+
'disable_web_page_preview' => true,
107+
];
83108

84109
$options = [
85-
'form_params' => [
86-
'text' => $message,
87-
'chat_id' => $this->chatId,
88-
'parse_mode' => 'html',
89-
'disable_web_page_preview' => true,
90-
]
110+
'form_params' => $this->topicId !== null ? $params + ['message_thread_id' => $this->topicId] : $params
91111
];
112+
92113
$response = $httpClient->post($url, $options);
93114
} catch (\Exception $e) {
94115

0 commit comments

Comments
 (0)