Skip to content

Commit 8b1be02

Browse files
committed
some changes for self host proxy in telegram
1 parent 3fe5e77 commit 8b1be02

File tree

1 file changed

+73
-70
lines changed

1 file changed

+73
-70
lines changed

src/TelegramBotHandler.php

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,86 @@
88

99
class TelegramBotHandler extends AbstractProcessingHandler
1010
{
11-
/**
12-
* bot api url
13-
* @var string
14-
*/
15-
private $botApi;
11+
/**
12+
* bot api url
13+
* @var string
14+
*/
15+
private $botApi;
1616

17-
/**
18-
* Telegram bot access token provided by BotFather.
19-
* Create telegram bot with https://telegram.me/BotFather and use access token from it.
20-
* @var string
21-
*/
22-
private $token;
17+
/**
18+
* Telegram bot access token provided by BotFather.
19+
* Create telegram bot with https://telegram.me/BotFather and use access token from it.
20+
* @var string
21+
*/
22+
private $token;
2323

24-
/**
25-
* if telegram is blocked in your region you can use proxy
26-
* @var null
27-
*/
28-
private $proxy;
24+
/**
25+
* if telegram is blocked in your region you can use proxy
26+
* @var null
27+
*/
28+
private $proxy;
2929

30-
/**
31-
* Telegram channel name.
32-
* Since to start with '@' symbol as prefix.
33-
* @var string
34-
*/
35-
private $chatId;
30+
/**
31+
* Telegram channel name.
32+
* Since to start with '@' symbol as prefix.
33+
* @var string
34+
*/
35+
private $chatId;
3636

37-
/**
38-
* @param string $token Telegram bot access token provided by BotFather
39-
* @param string $channel Telegram channel name
40-
* @inheritDoc
41-
*/
42-
public function __construct(string $token, string $chat_id, $level = Logger::DEBUG, bool $bubble = true, $bot_api = 'https://api.telegram.org/bot', $proxy = null)
43-
{
44-
parent::__construct($level, $bubble);
37+
/**
38+
* @param string $token Telegram bot access token provided by BotFather
39+
* @param string $channel Telegram channel name
40+
* @inheritDoc
41+
*/
42+
public function __construct(string $token, string $chat_id, $level = Logger::DEBUG, bool $bubble = true, $bot_api = 'https://api.telegram.org/bot', $proxy = null)
43+
{
44+
parent::__construct($level, $bubble);
4545

46-
$this->token = $token;
47-
$this->botApi = $bot_api;
48-
$this->chatId = $chat_id;
49-
$this->level = $level;
50-
$this->bubble = $bubble;
51-
$this->proxy = $proxy;
52-
}
46+
$this->token = $token;
47+
$this->botApi = $bot_api;
48+
$this->chatId = $chat_id;
49+
$this->level = $level;
50+
$this->bubble = $bubble;
51+
$this->proxy = $proxy;
52+
}
5353

54-
/**
55-
* @inheritDoc
56-
*/
57-
protected function write(array $record): void
58-
{
59-
$this->send($record['formatted']);
60-
}
54+
/**
55+
* @inheritDoc
56+
*/
57+
protected function write(array $record): void
58+
{
59+
$this->send($record['formatted']);
60+
}
6161

62-
/**
63-
* Send request to @link https://api.telegram.org/bot on SendMessage action.
64-
* @param string $message
65-
*/
66-
protected function send(string $message, $option = []): void
67-
{
68-
try {
69-
if (!is_null($this->proxy)) {
70-
$option['proxy'] = $this->proxy;
71-
}
72-
$httpClient = new Client($option);
62+
/**
63+
* Send request to @link https://api.telegram.org/bot on SendMessage action.
64+
* @param string $message
65+
*/
66+
protected function send(string $message, $option = []): void
67+
{
68+
try {
69+
if (!is_null($this->proxy)) {
70+
$option['proxy'] = $this->proxy;
71+
}
72+
$httpClient = new Client($option);
7373

74-
$url = $this->botApi . $this->token . '/SendMessage';
75-
$options = [
76-
'form_params' => [
77-
'text' => $message,
78-
'chat_id' => $this->chatId,
79-
'parse_mode' => 'html',
80-
'disable_web_page_preview' => true,
81-
]
82-
];
83-
$response = $httpClient->post($url, $options);
84-
} catch (\Exception $e) {
85-
86-
}
87-
}
88-
}
74+
if (strpos($this->botApi, 'https://api.telegram.org')) {
75+
$url = $this->botApi . $this->token . '/SendMessage';
76+
} else {
77+
$url = $this->botApi;
78+
}
8979

80+
$options = [
81+
'form_params' => [
82+
'text' => $message,
83+
'chat_id' => $this->chatId,
84+
'parse_mode' => 'html',
85+
'disable_web_page_preview' => true,
86+
]
87+
];
88+
$response = $httpClient->post($url, $options);
89+
} catch (\Exception $e) {
9090

91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)