@@ -55,13 +55,13 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt
5555 * @inheritDoc
5656 */
5757 public function __construct (
58- string $ token ,
59- string $ chat_id ,
58+ string $ token ,
59+ string $ chat_id ,
6060 ?string $ topic_id = null ,
61- $ level = Logger::DEBUG ,
62- bool $ bubble = true ,
63- $ bot_api = 'https://api.telegram.org/bot ' ,
64- $ proxy = null )
61+ $ level = Logger::DEBUG ,
62+ bool $ bubble = true ,
63+ $ bot_api = 'https://api.telegram.org/bot ' ,
64+ $ proxy = null )
6565 {
6666 parent ::__construct ($ level , $ bubble );
6767
@@ -79,7 +79,11 @@ public function __construct(
7979 */
8080 protected function write ($ record ): void
8181 {
82- $ this ->send ($ record ['formatted ' ]);
82+ $ token = $ record ['context ' ]['token ' ] ?? null ;
83+ $ chatId = $ record ['context ' ]['chat_id ' ] ?? null ;
84+ $ topicId = $ record ['context ' ]['topic_id ' ] ?? null ;
85+
86+ $ this ->send ($ record ['formatted ' ], $ token , $ chatId , $ topicId );
8387 }
8488
8589 private function truncateTextToTelegramLimit (string $ textMessage ): string
@@ -88,18 +92,22 @@ private function truncateTextToTelegramLimit(string $textMessage): string
8892 return $ textMessage ;
8993 }
9094
91- return mb_substr ($ textMessage , 0 , self ::TELEGRAM_MESSAGE_SIZE ,'UTF-8 ' );
95+ return mb_substr ($ textMessage , 0 , self ::TELEGRAM_MESSAGE_SIZE , 'UTF-8 ' );
9296 }
9397
9498 /**
9599 * Send request to @link https://api.telegram.org/bot on SendMessage action.
96100 * @param string $message
97101 * @param array $option
98102 */
99- protected function send (string $ message , $ option = []): void
103+ protected function send (string $ message , $ token = null , $ chatId = null , $ topicId = null , $ option = []): void
100104 {
101105 try {
102106
107+ $ token = $ token ?? $ this ->token ;
108+ $ chatId = $ chatId ?? $ this ->chatId ;
109+ $ topicId = $ topicId ?? $ this ->topicId ;
110+
103111 if (!isset ($ option ['verify ' ])) {
104112 $ option ['verify ' ] = false ;
105113 }
@@ -112,24 +120,45 @@ protected function send(string $message, $option = []): void
112120
113121 $ url = !str_contains ($ this ->botApi , 'https://api.telegram.org ' )
114122 ? $ this ->botApi
115- : $ this ->botApi . $ this -> token . '/SendMessage ' ;
123+ : $ this ->botApi . $ token . '/SendMessage ' ;
116124
117125 $ message = $ this ->truncateTextToTelegramLimit ($ message );
118126
119127 $ params = [
120128 'text ' => $ message ,
121- 'chat_id ' => $ this -> chatId ,
129+ 'chat_id ' => $ chatId ,
122130 'parse_mode ' => 'html ' ,
123131 'disable_web_page_preview ' => true ,
124132 ];
125133
126134 $ options = [
127- 'form_params ' => $ this -> topicId !== null ? $ params + ['message_thread_id ' => $ this -> topicId ] : $ params
135+ 'form_params ' => $ topicId !== null ? $ params + ['message_thread_id ' => $ topicId ] : $ params
128136 ];
129137
130138 $ response = $ httpClient ->post ($ url , $ options );
131139 } catch (\Exception $ e ) {
132140
133141 }
134142 }
143+
144+ public function setToken (string $ token ): static
145+ {
146+ $ this ->token = $ token ;
147+
148+ return $ this ;
149+ }
150+
151+ public function setChatId (string $ chatId ): static
152+ {
153+ $ this ->chatId = $ chatId ;
154+
155+ return $ this ;
156+ }
157+
158+ public function setTopicId (string $ topicId ): static
159+ {
160+ $ this ->topicId = $ topicId ;
161+
162+ return $ this ;
163+ }
135164}
0 commit comments