From 69f44cdc7096d656e70b47890fcc68226606b95b Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Sat, 31 Dec 2016 23:56:42 +0100 Subject: [PATCH 1/8] Add a simple request limiter to prevent bot from reaching API limits --- examples/getUpdatesCLI.php | 3 ++ examples/hook.php | 3 ++ src/DB.php | 77 ++++++++++++++++++++++++++++++++++++++ src/Request.php | 75 +++++++++++++++++++++++++++++++++++++ src/Telegram.php | 10 +++++ structure.sql | 12 ++++++ 6 files changed, 180 insertions(+) diff --git a/examples/getUpdatesCLI.php b/examples/getUpdatesCLI.php index 58b80eb8e..6d8656b1f 100644 --- a/examples/getUpdatesCLI.php +++ b/examples/getUpdatesCLI.php @@ -66,6 +66,9 @@ //$telegram->enableBotan('your_token'); //$telegram->enableBotan('your_token', ['timeout' => 3]); + // Requests Limiter (tries to prevent reaching Telegram API limits) + //$telegram->enableLimiter(); + // Handle telegram getUpdates request $serverResponse = $telegram->handleGetUpdates(); diff --git a/examples/hook.php b/examples/hook.php index 51c3e1cec..763fafa17 100644 --- a/examples/hook.php +++ b/examples/hook.php @@ -65,6 +65,9 @@ //$telegram->enableBotan('your_token'); //$telegram->enableBotan('your_token', ['timeout' => 3]); + // Requests Limiter (tries to prevent reaching Telegram API limits) + //$telegram->enableLimiter(); + // Handle telegram webhook request $telegram->handle(); } catch (Longman\TelegramBot\Exception\TelegramException $e) { diff --git a/src/DB.php b/src/DB.php index 27ee116c3..3af89cd04 100644 --- a/src/DB.php +++ b/src/DB.php @@ -136,6 +136,7 @@ protected static function defineTables() 'edited_message', 'inline_query', 'message', + 'request_limiter', 'telegram_update', 'user', 'user_chat', @@ -1052,4 +1053,80 @@ public static function selectChats( throw new TelegramException($e->getMessage()); } } + + /** + * Get Telegram API request count for current chat / message + * + * @param integer $chat_id + * @param string $inline_message_id + * + * @return array|bool (Array containing TOTAL and CURRENT fields or false on invalid arguments) + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function getTelegramRequestCount($chat_id = null, $inline_message_id = null) + { + if (!self::isDbConnected()) { + return false; + } + + try { + $sth = self::$pdo->prepare('SELECT + (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE ((`chat_id` = :chat_id AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL)) AND `created_at` >= :date) as CURRENT, + (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :date) as TOTAL + '); + + $date = self::getTimestamp(); + + $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); + $sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); + $sth->bindParam(':date', $date, \PDO::PARAM_STR); + + $sth->execute(); + + return $sth->fetch(); + } catch (\Exception $e) { + throw new TelegramException($e->getMessage()); + } + } + + /** + * Insert Telegram API request in db + * + * @param string $method + * @param array $data + * + * @return bool If the insert was successful + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + public static function insertTelegramRequest($method, $data) + { + if (!self::isDbConnected()) { + return false; + } + + $chat_id = ((isset($data['chat_id']) && $data['chat_id'] != 0) ? $data['chat_id'] : null); + $inline_message_id = (isset($data['inline_message_id']) ? $data['inline_message_id'] : null); + + try { + $sth = self::$pdo->prepare('INSERT INTO `' . TB_REQUEST_LIMITER . '` + ( + `method`, `chat_id`, `inline_message_id`, `created_at` + ) + VALUES ( + :method, :chat_id, :inline_message_id, :date + ); + '); + + $created_at = self::getTimestamp(); + + $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); + $sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); + $sth->bindParam(':method', $method, \PDO::PARAM_STR); + $sth->bindParam(':date', $created_at, \PDO::PARAM_STR); + + return $sth->execute(); + } catch (\Exception $e) { + throw new TelegramException($e->getMessage()); + } + } } diff --git a/src/Request.php b/src/Request.php index 2126c8c30..7988c5c4a 100644 --- a/src/Request.php +++ b/src/Request.php @@ -46,6 +46,13 @@ class Request */ private static $input; + /** + * Request limiter + * + * @var boolean + */ + private static $limiter_enabled; + /** * Available actions to send * @@ -314,6 +321,8 @@ public static function send($action, array $data = []) self::ensureNonEmptyData($data); + self::limitTelegramRequests($action, $data); + $response = json_decode(self::execute($action, $data), true); if (null === $response) { @@ -972,4 +981,70 @@ public static function getWebhookInfo() // Must send some arbitrary data for this to work for now... return self::send('getWebhookInfo', ['info']); } + + /** + * Enable request limiter + * + * @param boolean $value + */ + public static function setLimiter($value = true) + { + if (DB::isDbConnected()) { + self::$limiter_enabled = $value; + } + } + + /** + * This functions delays API requests to prevent reaching Telegram API limits + * Can be disabled while in execution by 'Request::setLimiter(false)' + * + * @link https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this + * + * @param string $action + * @param array $data + * + * @throws \Longman\TelegramBot\Exception\TelegramException + */ + private static function limitTelegramRequests($action, array $data = []) + { + if (self::$limiter_enabled) { + $limited_methods = [ + 'sendMessage', + 'forwardMessage', + 'sendPhoto', + 'sendAudio', + 'sendDocument', + 'sendSticker', + 'sendVideo', + 'sendVoice', + 'sendLocation', + 'sendVenue', + 'sendContact', + 'editMessageText', + 'editMessageCaption', + 'editMessageReplyMarkup', + ]; + + if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) { + $timeout = 60; + + while (true) { + $requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null)); + + if ($requests['CURRENT'] <= 0 && ((isset($data['inline_message_id']) && $requests['TOTAL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] > 0 && $requests['TOTAL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] < 0 && $requests['TOTAL'] < 20))) { + break; + } + + sleep(1); + $timeout--; + + if ($timeout <= 0) { + throw new TelegramException('Timed out while waiting for a request slot!'); + } + } + + DB::insertTelegramRequest($action, $data); + } + } + } } diff --git a/src/Telegram.php b/src/Telegram.php index 63f313113..75e64c2f8 100644 --- a/src/Telegram.php +++ b/src/Telegram.php @@ -829,4 +829,14 @@ public function enableBotan($token, array $options = []) return $this; } + + /** + * Enable requests limiter + */ + public function enableLimiter() + { + Request::setLimiter(true); + + return $this; + } } diff --git a/structure.sql b/structure.sql index f3b1bf2f6..9f405dba3 100644 --- a/structure.sql +++ b/structure.sql @@ -211,3 +211,15 @@ CREATE TABLE IF NOT EXISTS `botan_shortener` ( FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; + +CREATE TABLE IF NOT EXISTS `request_limiter` ( + `id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry', + `chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier', + `inline_message_id` char(255) NULL DEFAULT NULL COMMENT 'Identifier of the sent inline message', + `method` char(255) DEFAULT NULL COMMENT 'Request method', + `created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation', + + PRIMARY KEY (`id`), + + FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`) +) ENGINE=InnoDB DEFAULT charSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; From fe988b7f7f9c654524df3c5b539308492a8b41ab Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Sun, 1 Jan 2017 14:32:42 +0100 Subject: [PATCH 2/8] Fix for channels where chat_id can be a text (developers should use getChat() to get numeric value!) --- src/Request.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Request.php b/src/Request.php index 7988c5c4a..10042ed60 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1028,6 +1028,10 @@ private static function limitTelegramRequests($action, array $data = []) if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) { $timeout = 60; + if (!is_numeric($data['chat_id'])) { + $data['chat_id'] = 0; + } + while (true) { $requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null)); From ac29f07a39b9977c857760da8cdc6b8630b1b862 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Tue, 3 Jan 2017 22:37:45 +0100 Subject: [PATCH 3/8] Some changes... --- src/DB.php | 9 ++++++--- src/Request.php | 10 ++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/DB.php b/src/DB.php index 3af89cd04..c8d5f4b97 100644 --- a/src/DB.php +++ b/src/DB.php @@ -1071,15 +1071,18 @@ public static function getTelegramRequestCount($chat_id = null, $inline_message_ try { $sth = self::$pdo->prepare('SELECT - (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE ((`chat_id` = :chat_id AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL)) AND `created_at` >= :date) as CURRENT, - (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :date) as TOTAL + (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :date) as LIMIT_PER_SEC_ALL, + (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE ((`chat_id` = :chat_id AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL)) AND `created_at` >= :date) as LIMIT_PER_SEC, + (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `chat_id` = :chat_id AND `created_at` >= :date_minute) as LIMIT_PER_MINUTE '); - $date = self::getTimestamp(); + $date = self::getTimestamp(time()); + $date_minute = self::getTimestamp(strtotime('-1 minute')); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); $sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); $sth->bindParam(':date', $date, \PDO::PARAM_STR); + $sth->bindParam(':date_minute', $date_minute, \PDO::PARAM_STR); $sth->execute(); diff --git a/src/Request.php b/src/Request.php index 10042ed60..a405c1fc5 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1027,6 +1027,7 @@ private static function limitTelegramRequests($action, array $data = []) if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) { $timeout = 60; + $tick = 500000; //msec if (!is_numeric($data['chat_id'])) { $data['chat_id'] = 0; @@ -1035,15 +1036,16 @@ private static function limitTelegramRequests($action, array $data = []) while (true) { $requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null)); - if ($requests['CURRENT'] <= 0 && ((isset($data['inline_message_id']) && $requests['TOTAL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] > 0 && $requests['TOTAL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] < 0 && $requests['TOTAL'] < 20))) { + if ($requests['LIMIT_PER_SEC'] <= 0 && ((isset($data['inline_message_id']) && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] > 0 && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] < 0 && $requests['LIMIT_PER_MINUTE'] < 20))) { break; } - sleep(1); - $timeout--; + usleep($tick); + $timeout = $timeout - ($tick / 1000000); if ($timeout <= 0) { - throw new TelegramException('Timed out while waiting for a request slot!'); + TelegramLog::debug('Timed out while waiting for a request slot, retrying with 10 seconds delay!' . PHP_EOL . 'Request data:' . PHP_EOL . print_r($data, true) . PHP_EOL . PHP_EOL); + $timeout = 10; } } From 0dee13b0203719c42a5dd41488ceb9820b01ec07 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Mon, 9 Jan 2017 19:15:10 +0100 Subject: [PATCH 4/8] Changes... --- src/DB.php | 10 +++++----- src/Request.php | 20 +++++++++++--------- structure.sql | 6 ++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/DB.php b/src/DB.php index c8d5f4b97..947efc0dc 100644 --- a/src/DB.php +++ b/src/DB.php @@ -1079,7 +1079,7 @@ public static function getTelegramRequestCount($chat_id = null, $inline_message_ $date = self::getTimestamp(time()); $date_minute = self::getTimestamp(strtotime('-1 minute')); - $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); + $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); $sth->bindParam(':date', $date, \PDO::PARAM_STR); $sth->bindParam(':date_minute', $date_minute, \PDO::PARAM_STR); @@ -1095,8 +1095,8 @@ public static function getTelegramRequestCount($chat_id = null, $inline_message_ /** * Insert Telegram API request in db * - * @param string $method - * @param array $data + * @param string $method + * @param array $data * * @return bool If the insert was successful * @throws \Longman\TelegramBot\Exception\TelegramException @@ -1107,7 +1107,7 @@ public static function insertTelegramRequest($method, $data) return false; } - $chat_id = ((isset($data['chat_id']) && $data['chat_id'] != 0) ? $data['chat_id'] : null); + $chat_id = ((isset($data['chat_id'])) ? $data['chat_id'] : null); $inline_message_id = (isset($data['inline_message_id']) ? $data['inline_message_id'] : null); try { @@ -1122,7 +1122,7 @@ public static function insertTelegramRequest($method, $data) $created_at = self::getTimestamp(); - $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); + $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); $sth->bindParam(':method', $method, \PDO::PARAM_STR); $sth->bindParam(':date', $created_at, \PDO::PARAM_STR); diff --git a/src/Request.php b/src/Request.php index a405c1fc5..2bce8e13a 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1027,11 +1027,7 @@ private static function limitTelegramRequests($action, array $data = []) if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) { $timeout = 60; - $tick = 500000; //msec - - if (!is_numeric($data['chat_id'])) { - $data['chat_id'] = 0; - } + $retry = false; while (true) { $requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null)); @@ -1040,12 +1036,18 @@ private static function limitTelegramRequests($action, array $data = []) break; } - usleep($tick); - $timeout = $timeout - ($tick / 1000000); + sleep(1); + $timeout = $timeout - 1; if ($timeout <= 0) { - TelegramLog::debug('Timed out while waiting for a request slot, retrying with 10 seconds delay!' . PHP_EOL . 'Request data:' . PHP_EOL . print_r($data, true) . PHP_EOL . PHP_EOL); - $timeout = 10; + if ($retry) { + throw new TelegramException('Timed out while waiting for a request slot after 2 tries!'); + } else { + $timeout = 60; + $retry = true; + + TelegramLog::debug('Timed out while waiting for a request slot, retrying! Request data:' . PHP_EOL . print_r($data, true) . PHP_EOL . PHP_EOL); + } } } diff --git a/structure.sql b/structure.sql index 9f405dba3..8cfd539e2 100644 --- a/structure.sql +++ b/structure.sql @@ -214,12 +214,10 @@ CREATE TABLE IF NOT EXISTS `botan_shortener` ( CREATE TABLE IF NOT EXISTS `request_limiter` ( `id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry', - `chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier', + `chat_id` char(255) NULL DEFAULT NULL COMMENT 'Unique chat identifier', `inline_message_id` char(255) NULL DEFAULT NULL COMMENT 'Identifier of the sent inline message', `method` char(255) DEFAULT NULL COMMENT 'Request method', `created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation', - PRIMARY KEY (`id`), - - FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT charSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; From d7bf693dfc37fce6c4d86cc5291a06f8cdab89fb Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Mon, 6 Feb 2017 19:17:03 +0100 Subject: [PATCH 5/8] 'Retry' is pointless, remove it --- src/Request.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Request.php b/src/Request.php index e0ee9911b..dd3de6ccf 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1031,28 +1031,20 @@ private static function limitTelegramRequests($action, array $data = []) if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) { $timeout = 60; - $retry = false; while (true) { + if ($timeout <= 0) { + throw new TelegramException('Timed out while waiting for a request slot!'); + } + $requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null)); if ($requests['LIMIT_PER_SEC'] <= 0 && ((isset($data['inline_message_id']) && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] > 0 && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] < 0 && $requests['LIMIT_PER_MINUTE'] < 20))) { break; } - sleep(1); $timeout = $timeout - 1; - - if ($timeout <= 0) { - if ($retry) { - throw new TelegramException('Timed out while waiting for a request slot after 2 tries!'); - } else { - $timeout = 60; - $retry = true; - - TelegramLog::debug('Timed out while waiting for a request slot, retrying! Request data:' . PHP_EOL . print_r($data, true) . PHP_EOL . PHP_EOL); - } - } + sleep(1); } DB::insertTelegramRequest($action, $data); From b2e1c34c1d5bdc1757833b90e06376a47afbad4f Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Tue, 7 Feb 2017 18:14:55 +0100 Subject: [PATCH 6/8] Requests Limiter enabled by default --- examples/cron.php | 3 +++ examples/getUpdatesCLI.php | 2 +- examples/hook.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/cron.php b/examples/cron.php index 701f30010..1636b5f7f 100644 --- a/examples/cron.php +++ b/examples/cron.php @@ -60,6 +60,9 @@ //$telegram->setDownloadPath('../Download'); //$telegram->setUploadPath('../Upload'); + // Requests Limiter (tries to prevent reaching Telegram API limits) + $telegram->enableLimiter(); + // Run user selected commands $telegram->runCommands($commands); } catch (Longman\TelegramBot\Exception\TelegramException $e) { diff --git a/examples/getUpdatesCLI.php b/examples/getUpdatesCLI.php index 6d8656b1f..989bc5f03 100644 --- a/examples/getUpdatesCLI.php +++ b/examples/getUpdatesCLI.php @@ -67,7 +67,7 @@ //$telegram->enableBotan('your_token', ['timeout' => 3]); // Requests Limiter (tries to prevent reaching Telegram API limits) - //$telegram->enableLimiter(); + $telegram->enableLimiter(); // Handle telegram getUpdates request $serverResponse = $telegram->handleGetUpdates(); diff --git a/examples/hook.php b/examples/hook.php index 763fafa17..d65f9639d 100644 --- a/examples/hook.php +++ b/examples/hook.php @@ -66,7 +66,7 @@ //$telegram->enableBotan('your_token', ['timeout' => 3]); // Requests Limiter (tries to prevent reaching Telegram API limits) - //$telegram->enableLimiter(); + $telegram->enableLimiter(); // Handle telegram webhook request $telegram->handle(); From 899b79bac03487096cef3318f9c6793eb049d2a2 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Tue, 7 Feb 2017 19:21:21 +0100 Subject: [PATCH 7/8] Improve code readability --- src/Request.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Request.php b/src/Request.php index dd3de6ccf..a21a62b20 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1029,21 +1029,30 @@ private static function limitTelegramRequests($action, array $data = []) 'editMessageReplyMarkup', ]; - if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) { + $chat_id = isset($data['chat_id']) ? $data['chat_id'] : null; + $inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null; + + if (($chat_id || $inline_message_id) && in_array($action, $limited_methods)) { $timeout = 60; while (true) { if ($timeout <= 0) { - throw new TelegramException('Timed out while waiting for a request slot!'); + throw new TelegramException('Timed out while waiting for a request spot!'); } - $requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null)); + $requests = DB::getTelegramRequestCount($chat_id, $inline_message_id); - if ($requests['LIMIT_PER_SEC'] <= 0 && ((isset($data['inline_message_id']) && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] > 0 && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] < 0 && $requests['LIMIT_PER_MINUTE'] < 20))) { + if ( + $requests['LIMIT_PER_SEC'] == 0 && // No more than one message per second inside a particular chat + ( + (($chat_id > 0 || $inline_message_id) && $requests['LIMIT_PER_SEC_ALL'] < 30) || // No more than 30 messages per second globally + ($chat_id < 0 && $requests['LIMIT_PER_MINUTE'] < 20) // No more than 20 messages per minute for group chats + ) + ) { break; } - $timeout = $timeout - 1; + $timeout--; sleep(1); } From 331e1497664311200c282056e8437cb4dc8fd934 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Tue, 7 Feb 2017 19:28:08 +0100 Subject: [PATCH 8/8] Correction for travis to pass --- src/Request.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Request.php b/src/Request.php index a21a62b20..5e751723a 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1042,12 +1042,9 @@ private static function limitTelegramRequests($action, array $data = []) $requests = DB::getTelegramRequestCount($chat_id, $inline_message_id); - if ( - $requests['LIMIT_PER_SEC'] == 0 && // No more than one message per second inside a particular chat - ( - (($chat_id > 0 || $inline_message_id) && $requests['LIMIT_PER_SEC_ALL'] < 30) || // No more than 30 messages per second globally - ($chat_id < 0 && $requests['LIMIT_PER_MINUTE'] < 20) // No more than 20 messages per minute for group chats - ) + if ($requests['LIMIT_PER_SEC'] == 0 // No more than one message per second inside a particular chat + && ((($chat_id > 0 || $inline_message_id) && $requests['LIMIT_PER_SEC_ALL'] < 30) // No more than 30 messages per second globally + || ($chat_id < 0 && $requests['LIMIT_PER_MINUTE'] < 20)) ) { break; }