Skip to content

Commit e9e3588

Browse files
committed
Add optional parameter 'interval' to modify check interval for request limiter
1 parent 5cb0eba commit e9e3588

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

examples/cron.php

+2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
//$telegram->setUploadPath('../Upload');
6565

6666
// Requests Limiter (tries to prevent reaching Telegram API limits)
67+
// Second argument are options
6768
$telegram->enableLimiter();
69+
//$telegram->enableLimiter(['interval' => 0.5);
6870

6971
// Run user selected commands
7072
$telegram->runCommands($commands);

examples/getUpdatesCLI.php

+2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
//$telegram->enableBotan('your_token', ['timeout' => 3]);
7171

7272
// Requests Limiter (tries to prevent reaching Telegram API limits)
73+
// Second argument are options
7374
$telegram->enableLimiter();
75+
//$telegram->enableLimiter(['interval' => 0.5);
7476

7577
// Handle telegram getUpdates request
7678
$serverResponse = $telegram->handleGetUpdates();

examples/hook.php

+2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
//$telegram->enableBotan('your_token', ['timeout' => 3]);
7070

7171
// Requests Limiter (tries to prevent reaching Telegram API limits)
72+
// Second argument are options
7273
$telegram->enableLimiter();
74+
//$telegram->enableLimiter(['interval' => 0.5);
7375

7476
// Handle telegram webhook request
7577
$telegram->handle();

src/Request.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class Request
5353
*/
5454
private static $limiter_enabled;
5555

56+
/**
57+
* Request limiter's interval between checks
58+
*
59+
* @var boolean
60+
*/
61+
private static $limiter_interval;
62+
5663
/**
5764
* Available actions to send
5865
*
@@ -990,10 +997,24 @@ public static function getWebhookInfo()
990997
* Enable request limiter
991998
*
992999
* @param boolean $value
1000+
* @param array $options
1001+
*
1002+
* @throws \Longman\TelegramBot\Exception\TelegramException
9931003
*/
994-
public static function setLimiter($value = true)
1004+
public static function setLimiter($value = true, array $options = [])
9951005
{
9961006
if (DB::isDbConnected()) {
1007+
$options_default = [
1008+
'interval' => 1,
1009+
];
1010+
1011+
$options = array_merge($options_default, $options);
1012+
1013+
if (!is_numeric($options['interval']) || $options['interval'] <= 0) {
1014+
throw new TelegramException('Interval must be a number and must be greater than zero!');
1015+
}
1016+
1017+
self::$limiter_interval = $options['interval'];
9971018
self::$limiter_enabled = $value;
9981019
}
9991020
}
@@ -1042,15 +1063,15 @@ private static function limitTelegramRequests($action, array $data = [])
10421063

10431064
$requests = DB::getTelegramRequestCount($chat_id, $inline_message_id);
10441065

1045-
if ($requests['LIMIT_PER_SEC'] == 0 // No more than one message per second inside a particular chat
1046-
&& ((($chat_id > 0 || $inline_message_id) && $requests['LIMIT_PER_SEC_ALL'] < 30) // No more than 30 messages per second globally
1047-
|| ($chat_id < 0 && $requests['LIMIT_PER_MINUTE'] < 20))
1066+
if ($requests['LIMIT_PER_SEC'] == 0 // No more than one message per second inside a particular chat
1067+
&& ((($chat_id > 0 || $inline_message_id) && $requests['LIMIT_PER_SEC_ALL'] < 30) // No more than 30 messages per second globally
1068+
|| ($chat_id < 0 && $requests['LIMIT_PER_MINUTE'] < 20)) // No more than 20 messages per minute in groups and channels
10481069
) {
10491070
break;
10501071
}
10511072

10521073
$timeout--;
1053-
sleep(1);
1074+
usleep(self::$limiter_interval * 1000000);
10541075
}
10551076

10561077
DB::insertTelegramRequest($action, $data);

src/Telegram.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,14 @@ public function enableBotan($token, array $options = [])
882882

883883
/**
884884
* Enable requests limiter
885+
*
886+
* @param array $options
887+
*
888+
* @return \Longman\TelegramBot\Telegram
885889
*/
886-
public function enableLimiter()
890+
public function enableLimiter(array $options = [])
887891
{
888-
Request::setLimiter(true);
892+
Request::setLimiter(true, $options);
889893

890894
return $this;
891895
}

0 commit comments

Comments
 (0)