@@ -35,18 +35,34 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt
35
35
*/
36
36
private $ chatId ;
37
37
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
+
38
46
/**
39
47
* @param string $token Telegram bot access token provided by BotFather
40
48
* @param string $channel Telegram channel name
41
49
* @inheritDoc
42
50
*/
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 )
44
59
{
45
60
parent ::__construct ($ level , $ bubble );
46
61
47
62
$ this ->token = $ token ;
48
63
$ this ->botApi = $ bot_api ;
49
64
$ this ->chatId = $ chat_id ;
65
+ $ this ->topicId = $ topic_id ;
50
66
$ this ->level = $ level ;
51
67
$ this ->bubble = $ bubble ;
52
68
$ this ->proxy = $ proxy ;
@@ -63,32 +79,37 @@ protected function write($record): void
63
79
/**
64
80
* Send request to @link https://api.telegram.org/bot on SendMessage action.
65
81
* @param string $message
82
+ * @param array $option
66
83
*/
67
84
protected function send (string $ message , $ option = []): void
68
85
{
69
- try {
70
- if (!isset ($ option ['verify ' ])){
86
+ try {
87
+
88
+ if (!isset ($ option ['verify ' ])) {
71
89
$ option ['verify ' ] = false ;
72
90
}
91
+
73
92
if (!is_null ($ this ->proxy )) {
74
93
$ option ['proxy ' ] = $ this ->proxy ;
75
94
}
95
+
76
96
$ httpClient = new Client ($ option );
77
97
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
+ ];
83
108
84
109
$ 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
91
111
];
112
+
92
113
$ response = $ httpClient ->post ($ url , $ options );
93
114
} catch (\Exception $ e ) {
94
115
0 commit comments