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;