@@ -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