GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoiding flood limits

Avoiding flood limits

Jump to bottom Edit New page
feltroid Prime edited this page Jun 5, 2025 · 22 revisions

Flood Limits & Why to Avoid Them

Considering Telegram's Bot documentation, currently the maximum amount of messages being sent by bots is limited to ~30 messages/second for all ordinary messages and ~20 messages/minute for group messages. According to @BotSupport the limit for group also applies to channels (this is not confirmed by Telegram in their documentation however). We emphasize that Telegram does not document the precise limits, neither for sending messages nor for other kinds of API requests. Moreover, the limits may differ between bots and also over time.

When your bot hits spam limits, it starts to get RetryAfter errors from Telegram API. If you want to make sure that the message is actually sent and hence just try again on every RetryAfter error, your code would spend a significant amount resources just on those retries. Moreover, constantly retrying to send messages while ignoring API errors could result in your bot being banned for some time.

That means, if you're making a production-ready bot that is expected to have a large number of users, it's a good idea to use throughput limiting mechanism for messages being sent/requests being made to the Bot API. This way you can ensure that all messages would be delivered to end-users as soon as possible in ordered way.

Tip

With Bot API 7.1 (PTB v27.1), Telegram introduced the parameter allow_paid_broadcast. This allows bots to send up to 1000 messages per second by paying a fee in Telegram Stars.

Caution

PTBs rate limiting mechanism doesn't take allow_paid_broadcast into account automatically. If you want to use that parameter, make sure to implement the corresponding logic, so that the effect doesn't get lost.

PTBs Rate Limiting Mechanism

Since v20 (v20.0a3, to be precise), PTB comes with a built-in mechanism to throttle the number of API requests that your bot makes per time interval. This mechanism is exposed through the telegram.ext.BaseRateLimiter interface class. This class has basically one important abstract coroutine method, called BaseRateLimiter.process_request. This method is called every time when your bot makes an API request and it's purpose is to delay the request such that your bot doesn't hit the rate limits. Any implementation of BaseRateLimiter can implement its own logic on how this should be done. If you observe a particular pattern in how your bot receives RetryAfter errors (e.g. messages to one particular group trigger RetryAfter errors quickly), you can implement a logic that addresses these specific patterns.

The BaseRateLimiter setup can be used only with telegram.ext.ExtBot, not with telegram.Bot. It's easy to set up and use. Assume that MyLimiter implements BaseRateLimiter. Then we use like this:

from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler

application = ApplicationBuilder().token("TOKEN").rate_limiter(MyLimiter()).build()


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # This is automatically passed through `MyLimiter.process_request`!
    await update.message.reply_text(text="Hello World!")


application.add_handler(CommandHandler('start', start))

You may also pass additional information to MyLimiter.process_request. Imagine that you want to broadcast a message to all the users of your bot. You might want to give those messages a lower priority, answering requests of your users should be faster. This could look like this:

import asyncio
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler

application = ApplicationBuilder().token("TOKEN").rate_limiter(MyLimiter()).build()
user_ids = [1, 2, 3]

async def broadcast(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text(text="Starting the broadcast...")
    context.application.create_task(
        asyncio.gather(
            *(
                context.bot.send_message(
                    chat_id=user_id, text="Hello World!", rate_limit_args={'priority': -1}
                )
                for user_id in user_ids
            )
        )
    )


application.add_handler(CommandHandler('broadcast', broadcast))

What kind of input is allowed for the rate_limit_args argument is up to the implementation of BaseRateLimiter.

Built-in Rate Limiter

PTB comes with a built-in implementation of BaseRatelimiter: The class telegram.ext.AIORateLimiter uses the library aiolimiter. Please consult the documentation of AIORateLimiter for details on how it applies rate limits to API requests and how it should be used.

⚠️ We emphasize that AIORateLimiter 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 telegram.ext.BaseRateLimiter. Feel free to check out the source code of AIORateLimiter class for inspiration.

Wiki of python-telegram-bot © Copyright 2015-2026 – Licensed by Creative Commons

Must read

  1. Introduction to the API
  2. Tutorial: Your first bot
  3. FAQ
  4. How to ask good questions
  5. How to write an MWE

Concepts & Important Elements

  1. Architecture Overview
  2. Builder Pattern for Application
  3. Types of Handlers
  4. Working with Files and Media
  5. Exceptions, Warnings and Logging
  6. Concurrency in PTB

Notable Features

  1. Advanced Filters
  2. Storing data
  3. Making your bot persistent
  4. Adding Defaults
  5. Job Queue
  6. Arbitrary callback_data
  7. Avoiding flood limits
  8. Webhooks
  9. Bot API Forward Compatiblity

Code Resources

  1. Frequently requested design patterns
  2. Code snippets
  3. Performance Optimizations
  4. Telegram Passport
  5. Bots built with PTB
  6. Automated Bot Tests

Examples explained

  1. InlineKeyboard Example

Networking

  1. Working Behind a Proxy
  2. Handling network errors

Other resources

  1. Where to host Telegram Bots
  2. How to host your bot
  3. Local API Server
  4. Type Checking with PTB
  5. Press
  6. Notes on GAE
  7. Related Projects
  8. Emoji

Transition Guides

  • Version 4
  • Version 5
  • Version 12
  • Version 13
  • Version 20

Administration

  • Test Bots
  • How To Release
  • Bots, Groups & Channels
  • PTB tests knowledge base
  • Bot API Backward Compatibility

Clone this wiki locally

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.