Skip to content

Commit 9b0ff18

Browse files
committed
Added threshold setting
1 parent ce632c6 commit 9b0ff18

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- `command_prefixes`: Array of characters for commands prefixes. e.g. `/start`, `.info`
77
- `group_handlers`: Whether to execute all the handlers of an update in the same process (`true`), or fork a process for each handler (`false`)
88
- `wait_handlers`: Whether to wait for handlers to finish when closing script
9+
- `threshold`: Amount of max seconds the script will sleep instead of throwing a `TooManyRequestsException`. Defaults to 10 when using `getUpdates`
910
- Conversations **full getters**
1011
- `getConversationsByChat` (`User|Chat::getConversations()`)
1112
- `getConversationsByValue`

docs/construct.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ $token is the Bot's token generated by @BotFather
1414
$settings is an array which contains some NovaGram configurations.
1515
> None of these fields is required
1616
17-
| key | type | default | description |
18-
|-----------------------------|---------|--------------------------|-----------------------------------------------------------------------------------------------------------------------------|
19-
| mode | integer | auto | Mode for update handling (or no handling at all) |
20-
| json_payload | boolean | `true` | Whether or not print json payload |
21-
| log_updates | integer | `false` | Chat id where raw json updates will be sent (set to false to disable) |
22-
| debug | integer | `false` | Chat id where debug logs will be sent if an api error occurs (set to false to disable) |
23-
| async | bool | `true` | Whether or not process updates concurrently |
24-
| command_prefixes | array | `['/']` | Characters for commands prefixes. e.g. /start, .info |
25-
| bot_api_url | string | https://api.telegram.org | Url for custom bot api |
26-
| restart_on_changes | bool | `false` | Auto restart when Bot file is edited |
27-
| group_handlers | bool | `true` | Whether to execute all the handlers of an update in the same process (true), or fork a process for each handler (false) |
28-
| wait_handlers | bool | `false` | Whether to wait for handlers to finish when closing script |
29-
| logger | int | `Monolog\Logger::INFO` | `Monolog\Logger` constant for logging |
30-
| disable_ip_check | boolean | `false` | Whether or not disable telegram ip check (could be useful in case of reverse proxy, such as ngrok) |
31-
| exceptions | bool | `true` | Whether or not throw \skrtdev\Telegram\Exception(s) when API Errors occurs |
32-
| database | array | no | [Database](database.md) array connection info or instance of an existing `PDO` database (`novagram` will be used as prefix) |
33-
| parse_mode | string | no | Default `parse_mode` for methods that require it |
34-
| disable_web_page_preview | bool | no | Default `disable_web_page_preview` for methods that require it |
35-
| disable_notification | bool | no | Default `disable_notification` for methods that require it |
36-
| allow_sending_without_reply | bool | no | Default `allow_sending_without_reply` for methods that require it |
37-
17+
| key | type | default | description |
18+
|-----------------------------|---------|--------------------------|-------------------------------------------------------------------------------------------------------------------------------------|
19+
| mode | integer | auto | Mode for update handling (or no handling at all) |
20+
| json_payload | boolean | `true` | Whether or not print json payload |
21+
| log_updates | integer | `false` | Chat id where raw json updates will be sent (set to false to disable) |
22+
| debug | integer | `false` | Chat id where debug logs will be sent if an api error occurs (set to false to disable) |
23+
| async | bool | `true` | Whether or not process updates concurrently |
24+
| command_prefixes | array | `['/']` | Characters for commands prefixes. e.g. /start, .info |
25+
| bot_api_url | string | https://api.telegram.org | Url for custom bot api |
26+
| restart_on_changes | bool | `false` | Auto restart when Bot file is edited |
27+
| group_handlers | bool | `true` | Whether to execute all the handlers of an update in the same process (true), or fork a process for each handler (false) |
28+
| wait_handlers | bool | `false` | Whether to wait for handlers to finish when closing script |
29+
| threshold | int | null | Defaults to 10 when using `getUpdates`. Amount of max seconds the script will wait instead of throwing a `TooManyRequestsException` |
30+
| logger | int | `Monolog\Logger::INFO` | `Monolog\Logger` constant for logging |
31+
| disable_ip_check | boolean | `false` | Whether or not disable telegram ip check (could be useful in case of reverse proxy, such as ngrok) |
32+
| exceptions | bool | `true` | Whether or not throw \skrtdev\Telegram\Exception(s) when API Errors occurs |
33+
| database | array | null | [Database](database.md) array connection info or instance of an existing `PDO` database (`novagram` will be used as prefix) |
34+
| parse_mode | string | null | Default `parse_mode` for methods that require it |
35+
| disable_web_page_preview | bool | null | Default `disable_web_page_preview` for methods that require it |
36+
| disable_notification | bool | null | Default `disable_notification` for methods that require it |
37+
| allow_sending_without_reply | bool | null | Default `allow_sending_without_reply` for methods that require it |
3838

3939
### Mode
4040

src/NovaGram/Bot.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
Update,
1414
ObjectsList,
1515
Exception as TelegramException,
16-
BadGatewayException
16+
BadGatewayException,
17+
TooManyRequestsException
1718
};
1819
use skrtdev\Prototypes\proto;
1920

@@ -78,6 +79,7 @@ public function __construct(string $token, array $settings = [], ?Logger $logger
7879
"command_prefixes" => [self::COMMAND_PREFIX],
7980
"group_handlers" => true,
8081
"wait_handlers" => false,
82+
"threshold" => null, // 10 is default when using getUpdates
8183
"database" => null,
8284
"parse_mode" => null,
8385
"disable_web_page_preview" => null,
@@ -180,7 +182,7 @@ public function __construct(string $token, array $settings = [], ?Logger $logger
180182

181183
public function stop(int $signo = null)
182184
{
183-
if(!$this->settings->wait_handlers || $this->settings->mode !== self::CLI || !$this->settings->async) return;
185+
if(!$this->settings->wait_handlers || $this->settings->mode !== self::CLI || !$this->settings->async) exit;
184186

185187
print("Stopping...".PHP_EOL);
186188
if($this->running){
@@ -282,6 +284,7 @@ public function idle(){
282284
$this->logger->warning("There was a set webhook. It has been deleted. (URL: {$webhook_info->url})");
283285
}
284286
$this->running = true;
287+
$this->settings->threshold ??= 10; // set 10 as default when using getUpdates
285288
self::showLicense();
286289
while ($this->running) {
287290
$offset = $this->processUpdates($offset ?? 0);
@@ -359,7 +362,7 @@ public function APICall(string $method, array $data = [], bool $payload = false,
359362
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
360363
$response = curl_exec($ch);
361364
curl_close($ch);
362-
#if(is_bool($response)) return $this->APICall(...func_get_args());
365+
if(is_bool($response)) return $this->APICall(...func_get_args());
363366
$decoded = json_decode($response, true);
364367

365368
if($decoded['ok'] !== true){
@@ -370,7 +373,13 @@ public function APICall(string $method, array $data = [], bool $payload = false,
370373
#$this->sendMessage($this->settings->debug, "<pre>".$method.PHP_EOL.PHP_EOL.print_r($data, true).PHP_EOL.PHP_EOL.print_r($decoded, true)."</pre>", ["parse_mode" => "HTML"], false, true);
371374
$this->debug( (string) $e, $previous_exception);
372375
}
373-
if($this->settings->exceptions) throw $e;
376+
if($e instanceof TooManyRequestsException && $e->response_parameters->retry_after <= ($this->settings->threshold ?? 0) ){
377+
$retry_after = $e->response_parameters->retry_after;
378+
$this->logger->info("[Floodwait] Waiting for $retry_after seconds (caused by $method)");
379+
sleep($retry_after);
380+
return $this->APICall(...func_get_args());
381+
}
382+
elseif($this->settings->exceptions) throw $e;
374383
else return (object) $decoded;
375384
}
376385

0 commit comments

Comments
 (0)