Skip to content

Commit c019c90

Browse files
committed
Added usage Monolog TelegramBotHandler for logging
1 parent d9bbbb6 commit c019c90

File tree

3 files changed

+14
-43
lines changed

3 files changed

+14
-43
lines changed

app/Services/TelegramBot.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use Illuminate\Http\Client\Response;
66
use Illuminate\Support\Facades\Cache;
77
use Illuminate\Support\Facades\Http;
8-
use Illuminate\Support\Str;
9-
use NotificationChannels\Telegram\TelegramMessage;
108

119
class TelegramBot
1210
{
@@ -98,39 +96,6 @@ public function isSpam(?string $message, $userId = null): bool
9896
return true;
9997
}
10098

101-
$detector = new SpamDetector($message);
102-
103-
return $detector->isSpam();
104-
}
105-
106-
/**
107-
* Send notification to Telegram.
108-
*/
109-
public static function notificationToTelegram(\Throwable $exception): void
110-
{
111-
if (config('app.env') !== 'production') {
112-
return;
113-
}
114-
115-
// Send notification to Telegram
116-
try {
117-
TelegramMessage::create()
118-
->to(config('services.telegram-bot-api.chat_id'))
119-
->line('*⚠️ Ой-ой-ой!* Возникла неприятность в нашем коде. Пользователь столкнулся с неожиданной ошибкой на сайте.')
120-
->line('`')
121-
->escapedLine(Str::of($exception->getMessage())->ucfirst())
122-
->line('`')
123-
->escapedLine('📄 Код ошибки: '.$exception->getCode())
124-
->escapedLine('📂 Файл: '.Str::after($exception->getFile(), base_path()).'#'.$exception->getLine())
125-
->line('')
126-
->line('*🔧 Что делать?*')
127-
->line('Давайте взглянем на этот участок кода внимательно и исправим проблему. С вашими умениями мы сможем преодолеть эту преграду!')
128-
->line('')
129-
->line('*💪 Не сдавайтесь!*')
130-
->line('Каждая ошибка - это шанс стать лучше. Давайте использовать этот момент, чтобы улучшить наш код и стать еще сильнее. Удачи!')
131-
->send();
132-
} catch (\Exception|\Throwable) {
133-
// without recursive
134-
}
99+
return (new SpamDetector($message))->isSpam();
135100
}
136101
}

bootstrap/app.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use App\Http\Middleware\RedirectToBanPage;
44
use App\Http\Middleware\SetDefaultVersionForUrl;
55
use Illuminate\Foundation\Application;
6-
use Illuminate\Foundation\Configuration\Exceptions;
76
use Illuminate\Foundation\Configuration\Middleware;
87

98
return Application::configure(basePath: dirname(__DIR__))
@@ -26,8 +25,4 @@
2625
'goronich',
2726
]);
2827
})
29-
->withExceptions(function (Exceptions $exceptions) {
30-
$exceptions->report(function (Throwable $e) {
31-
\App\Services\TelegramBot::notificationToTelegram($e);
32-
});
33-
})->create();
28+
->create();

config/logging.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Monolog\Handler\NullHandler;
44
use Monolog\Handler\StreamHandler;
55
use Monolog\Handler\SyslogUdpHandler;
6+
use Monolog\Handler\TelegramBotHandler;
67
use Monolog\Processor\PsrLogMessageProcessor;
78

89
return [
@@ -54,7 +55,7 @@
5455
'channels' => [
5556
'stack' => [
5657
'driver' => 'stack',
57-
'channels' => ['single'],
58+
'channels' => ['single', 'telegram'],
5859
'ignore_exceptions' => false,
5960
],
6061

@@ -94,6 +95,16 @@
9495
'processors' => [PsrLogMessageProcessor::class],
9596
],
9697

98+
'telegram' => [
99+
'driver' => 'monolog',
100+
'handler' => TelegramBotHandler::class,
101+
'level' => env('LOG_LEVEL', 'debug'),
102+
'with' => [
103+
'apiKey' => env('TELEGRAM_BOT_TOKEN', ''),
104+
'channel' => env('TELEGRAM_CHAT_ID', ''),
105+
],
106+
],
107+
97108
'stderr' => [
98109
'driver' => 'monolog',
99110
'level' => env('LOG_LEVEL', 'debug'),

0 commit comments

Comments
 (0)