Skip to content

Commit 7704654

Browse files
committed
Add rate limiting configuration for Telegram logger
1 parent 9e11385 commit 7704654

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

config/telegram-logger.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,29 @@
6969
'log_errors' => [
7070
'500'
7171
],
72+
73+
/*
74+
| --------------------------------------------------------------------------
75+
| Rate Limiter max attempts
76+
| --------------------------------------------------------------------------
77+
|
78+
| This option defines the maximum number of attempts to send a message
79+
| to the Telegram Bot API. The default value is 1.
80+
| Telegram log will send one exemplary message per day.
81+
|
82+
*/
83+
84+
'max_attempts' => 1,
85+
86+
/*
87+
| --------------------------------------------------------------------------
88+
| Rate Limiter decay seconds
89+
| --------------------------------------------------------------------------
90+
|
91+
| This option defines the number of seconds to wait before retrying to
92+
| send a message to the Telegram Bot API. The default value is 1 day.
93+
|
94+
*/
95+
96+
'decay_seconds' => 60 * 60 * 24,
7297
];

src/Telegram/TelegramLog.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ public function setChatId(string $chatId): self
4646
protected function rateLimiter($message): bool
4747
{
4848
$key = md5('telegram_logger' . $message);
49+
$decaySeconds = config('telegram-logger.decay_seconds', 60 * 60 * 24);
50+
$maxAttempts = config('telegram-logger.max_attempts', 1);
4951

50-
if (RateLimiter::tooManyAttempts($key, 5)) {
52+
if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
5153
info('Telegram rate limit exceeded');
54+
5255
return false;
5356
}
5457

55-
RateLimiter::hit($key);
58+
RateLimiter::hit($key, $decaySeconds);
5659

5760
return true;
5861
}

0 commit comments

Comments
 (0)