- class telegram.ext.AIORateLimiter(overall_max_rate=30, overall_time_period=1, group_max_rate=20, group_time_period=60, max_retries=0)¶
If you want to use this class, you must install PTB with the optional requirement
pip install "python-telegram-bot[rate-limiter]"
The rate limiting is applied by combining two levels of throttling and
process_request()roughly boils down to:
async with group_limiter(group_id): async with overall_limiter: await callback(*args, **kwargs)
group_idis determined by checking if there is a
chat_idparameter in the
overall_limiteris applied only if a
chat_idargument is present at all.
Some bot methods accept a
chat_idparameter in form of a
@usernamefor supergroups and channels. As we can’t know which
@usernamecorresponds to which integer
chat_id, these will be treated as different groups, which may lead to exceeding the rate limit.
As channels can’t be differentiated from supergroups by the
chat_id, this also applies the group related rate limits to channels.
RetryAfterexception will halt all requests for
retry_after+ 0.1 seconds. This may be stricter than necessary in some cases, e.g. the bot may hit a rate limit in one group but might still be allowed to send messages in another group.
This class is to be understood as minimal effort reference implementation. If you would like to handle rate limiting in a more sophisticated, fine-tuned way, we welcome you to implement your own subclass of
BaseRateLimiter. Feel free to check out the source code of this class for inspiration.
New in version 20.0.
- async initialize()¶
- async process_request(callback, args, kwargs, endpoint, data, rate_limit_args)¶
Processes a request by applying rate limiting.
telegram.ext.BaseRateLimiter.process_request()for detailed information on the arguments.
- async shutdown()¶