Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/.idea
vendor
test
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Open config/logging.php and change the file
'handler_with' => [
'token' => env('LOG_TELEGRAM_BOT_TOKEN'),
'chat_id' => env('LOG_TELEGRAM_CHAT_ID'),
'topic_id' => env('LOG_TELEGRAM_TOPIC_ID',null),
'bot_api' => env('LOG_TELEGRAM_BOT_API', 'https://api.telegram.org/bot'),
'proxy' => env('LOG_TELEGRAM_BOT_PROXY', null),
],
Expand All @@ -53,6 +54,12 @@ Add the following variables to your .env file.
```php
LOG_TELEGRAM_BOT_TOKEN=
LOG_TELEGRAM_CHAT_ID=

# If chat groups are used instead of telegram channels,
# and the ability to set topics on groups is enabled,
# this configuration can be utilized.
LOG_TELEGRAM_TOPIC_ID=

#LOG_TELEGRAM_BOT_API='https://api.telegram.org/bot'
# add tor proxy for restricted country
#LOG_TELEGRAM_BOT_PROXY='socks5h://localhost:9050'
Expand Down
172 changes: 172 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 35 additions & 14 deletions src/TelegramBotHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,34 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt
*/
private $chatId;

/**
* If chat groups are used instead of telegram channels,
* and the ability to set topics on groups is enabled,
* this configuration can be utilized.
* @var string|null
*/
private $topicId;

/**
* @param string $token Telegram bot access token provided by BotFather
* @param string $channel Telegram channel name
* @inheritDoc
*/
public function __construct(string $token, string $chat_id, $level = Logger::DEBUG, bool $bubble = true, $bot_api = 'https://api.telegram.org/bot', $proxy = null)
public function __construct(
string $token,
string $chat_id,
string|null $topic_id = null,
$level = Logger::DEBUG,
bool $bubble = true,
$bot_api = 'https://api.telegram.org/bot',
$proxy = null)
{
parent::__construct($level, $bubble);

$this->token = $token;
$this->botApi = $bot_api;
$this->chatId = $chat_id;
$this->topicId = $topic_id;
$this->level = $level;
$this->bubble = $bubble;
$this->proxy = $proxy;
Expand All @@ -63,32 +79,37 @@ protected function write($record): void
/**
* Send request to @link https://api.telegram.org/bot on SendMessage action.
* @param string $message
* @param array $option
*/
protected function send(string $message, $option = []): void
{
try {
if(!isset($option['verify'])){
try {

if (!isset($option['verify'])) {
$option['verify'] = false;
}

if (!is_null($this->proxy)) {
$option['proxy'] = $this->proxy;
}

$httpClient = new Client($option);

if (strpos($this->botApi, 'https://api.telegram.org') === false) {
$url = $this->botApi;
} else {
$url = $this->botApi . $this->token . '/SendMessage';
}
$url = !str_contains($this->botApi, 'https://api.telegram.org')
? $this->botApi
: $this->botApi . $this->token . '/SendMessage';

$params = [
'text' => $message,
'chat_id' => $this->chatId,
'parse_mode' => 'html',
'disable_web_page_preview' => true,
];

$options = [
'form_params' => [
'text' => $message,
'chat_id' => $this->chatId,
'parse_mode' => 'html',
'disable_web_page_preview' => true,
]
'form_params' => $this->topicId !== null ? $params + ['message_thread_id' => $this->topicId] : $params
];

$response = $httpClient->post($url, $options);
} catch (\Exception $e) {

Expand Down