Skip to content

Commit 13d0df5

Browse files
authored
Merge pull request #60 from nutgram/rate-limit
Add "rate limiting" page
2 parents 09f1500 + 2f39c6e commit 13d0df5

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

docs/usage/rate_limiting.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Rate Limiting
2+
3+
Nutgram offers a powerful rate-limiting feature to safeguard your bot from abuse.<br/>
4+
You can set rate limits for specific handlers, groups, or the entire bot,
5+
ensuring users can only make a controlled number of requests within a defined time frame.
6+
7+
:::info
8+
The rate limiter will run only for updates with the `user.id` and `chat.id` set.<br/>
9+
This means that the rate limiter will be skipped for updates without these fields.
10+
:::
11+
12+
To apply a rate limiter, use the `throttle` method.
13+
14+
**Available Rate Limits**
15+
- **Handler Rate Limit**: restricts the execution of a specific handler (highest priority).
16+
- **Group Rate Limit**: restricts the execution of handlers within a specific group.
17+
- **Global Rate Limit**: restricts the execution of any handler in the bot (lowest priority).
18+
19+
:::caution
20+
- Each call to `throttle` overrides the previous one.
21+
- The rate limiter follows a hierarchy: **Handler > Group > Global**.<br/>
22+
This means that a rate limiter applied at the **Handler level** will override any limits set at the **Group** or **Global** level.<br/>
23+
Similarly, a **Group rate limit** will override a **Global rate limit**, but not a **Handler rate limit**.
24+
:::
25+
26+
## Rate Limiters
27+
28+
### Global Rate Limit
29+
30+
A global rate limiter can be set once and will apply to all handlers.
31+
32+
```php
33+
$bot = new Nutgram('your-token-here');
34+
$bot->throttle(10);
35+
36+
$bot->onCommand('start', StartCommand::class); // 10 messages per minute
37+
$bot->onCommand('help', HelpCommand::class); // 10 messages per minute
38+
39+
$bot->run();
40+
```
41+
42+
### Group Rate Limit
43+
44+
A group rate limiter can be set for a specific group of handlers.
45+
46+
```php
47+
$bot = new Nutgram('your-token-here');
48+
49+
$bot->group(function(Nutgram $bot){
50+
$bot->onCommand('start', StartCommand::class); // 3 messages per minute
51+
$bot->onCommand('help', HelpCommand::class); // 3 messages per minute
52+
})->throttle(3);
53+
54+
$bot->run();
55+
```
56+
57+
58+
### Handler Rate Limit
59+
60+
A handler rate limiter can be set for a specific handler.
61+
62+
```php
63+
$bot = new Nutgram('your-token-here');
64+
65+
$bot->onCommand('start', StartCommand::class)->throttle(5); // 5 messages per minute
66+
$bot->onCommand('help', HelpCommand::class)->throttle(5); // 5 messages per minute
67+
68+
$bot->run();
69+
```
70+
71+
## Throttle Parameters
72+
73+
The `throttle` method accepts the following parameters:
74+
75+
| Parameter | Type | Default | Description |
76+
|:----------------|:----------|:--------|:----------------------------------------------------------|
77+
| maxAttempts | int | | The number of messages allowed in the time frame |
78+
| decaySeconds | int | 60 | The time frame in seconds |
79+
| key | ?string | null | A unique key to identify the group for the rate limiter |
80+
| warningCallback | ?callable | null | A callback to be executed when the rate limit is exceeded |
81+
82+
## Skip Rate Limiter
83+
84+
You can skip the rate limiter for a specific handler by calling the `withoutThrottle` method.
85+
86+
```php
87+
$bot = new Nutgram('your-token-here');
88+
$bot->throttle(5);
89+
90+
$bot->onCommand('start', StartCommand::class); // 5 messages per minute
91+
$bot->onCommand('help', HelpCommand::class)->withoutThrottle(); // No rate limiter
92+
93+
$bot->run();
94+
```
95+
96+
## Rate Limit Exceeded
97+
98+
When the rate limit is exceeded, the bot will send a message (**once**) to the user with the following text:
99+
100+
> Too many messages, please wait a bit. This message will only be sent once until the rate limit is reset.
101+
102+
If you want to customize this message, you can set your logic in the throttle method:
103+
104+
```php
105+
$bot = new Nutgram('your-token-here');
106+
107+
$bot->onCommand('start', StartCommand::class)
108+
->throttle(5, warningCallback: function (Nutgram $bot, int $availableIn) {
109+
$bot->sendMessage("You're sending too many messages. Please wait $availableIn seconds.");
110+
});
111+
```
112+
113+
## Advanced example
114+
115+
```php
116+
$bot = new Nutgram('your-token-here');
117+
$bot->throttle(4);
118+
119+
$bot->onCommand('start', StartCommand::class); // 4 messages per minute
120+
$bot->onCommand('help', HelpCommand::class)->throttle(2); // 2 messages per minute
121+
122+
$bot->group(function (Nutgram $bot) {
123+
124+
$bot->onCommand('feedback', FeedbackCommand::class); // 3 messages per minute
125+
$bot->onCommand('stats', StatsCommand::class)->throttle(2); // 2 messages per minute
126+
$bot->onCommand('what', WhatCommand::class)->throttle(5); // 5 messages per minute
127+
128+
$bot->group(function (Nutgram $bot) {
129+
130+
$bot->onCommand('settings', SettingsCommand::class); // 2 messages per minute
131+
$bot->onCommand('donate', DonateCommand::class)->throttle(1); // 1 message per minute
132+
$bot->onCommand('refund', RefundCommand::class)->throttle(3); // 3 messages per minute
133+
134+
})->throttle(2);
135+
136+
})->throttle(3);
137+
138+
```

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const sidebars = {
4747
'usage/sending_requests',
4848
'usage/handlers',
4949
'usage/middleware',
50+
'usage/rate_limiting',
5051
'usage/keyboards',
5152
'usage/conversations',
5253
'usage/inline_menu',

0 commit comments

Comments
 (0)