Skip to content

Commit 029eb08

Browse files
refactor(TelegramBotHandler): Support for chat group topics in Telegram
1 parent 74d261a commit 029eb08

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/TelegramBotHandler.php

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

38+
/**
39+
*
40+
* @var string|null
41+
*/
42+
private $topicId;
43+
3844
/**
3945
* @param string $token Telegram bot access token provided by BotFather
4046
* @param string $channel Telegram channel name
4147
* @inheritDoc
4248
*/
43-
public function __construct(string $token, string $chat_id, $level = Logger::DEBUG, bool $bubble = true, $bot_api = 'https://api.telegram.org/bot', $proxy = null)
49+
public function __construct(
50+
string $token,
51+
string $chat_id,
52+
string|null $topic_id = null,
53+
$level = Logger::DEBUG,
54+
bool $bubble = true,
55+
$bot_api = 'https://api.telegram.org/bot',
56+
$proxy = null)
4457
{
4558
parent::__construct($level, $bubble);
4659

4760
$this->token = $token;
4861
$this->botApi = $bot_api;
4962
$this->chatId = $chat_id;
63+
$this->topicId = $topic_id;
5064
$this->level = $level;
5165
$this->bubble = $bubble;
5266
$this->proxy = $proxy;
@@ -63,16 +77,20 @@ protected function write($record): void
6377
/**
6478
* Send request to @link https://api.telegram.org/bot on SendMessage action.
6579
* @param string $message
80+
* @param array $option
6681
*/
6782
protected function send(string $message, $option = []): void
6883
{
69-
try {
70-
if(!isset($option['verify'])){
84+
try {
85+
86+
if (!isset($option['verify'])) {
7187
$option['verify'] = false;
7288
}
89+
7390
if (!is_null($this->proxy)) {
7491
$option['proxy'] = $this->proxy;
7592
}
93+
7694
$httpClient = new Client($option);
7795

7896
if (strpos($this->botApi, 'https://api.telegram.org') === false) {
@@ -81,14 +99,17 @@ protected function send(string $message, $option = []): void
8199
$url = $this->botApi . $this->token . '/SendMessage';
82100
}
83101

102+
$params = [
103+
'text' => $message,
104+
'chat_id' => $this->chatId,
105+
'parse_mode' => 'html',
106+
'disable_web_page_preview' => true,
107+
];
108+
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)