Telegram allows you to create chatbots within the messenger for multiple purposes, but some developers report that they are getting the flood limit error while sending requests.
In this detailed article, we’ll explore the meaning of this limit and how you can avoid getting the error while sending requests or messages to users.
What does Telegram flood wait mean?
The Telegram flood limit is the mechanism that the platform has set to prevent spam and abuse of its service. For this purpose, it has set the maximum number of messages or requests that a chatbot can make to the Telegram API within a specific time period.
While the exact limit for sending messages/requests is not specified by Telegram, here is the general rule:
- Telegram bot API lets you send only 10 code requests per account.
- You are not allowed to send 1 message per second to a single chat.
- Telegram won’t let you send more than 30 messages per second to multiple users and over 20 requests to Telegram groups or channels.
- You can only create 50 groups or channels per account.
If you exceed these limits, you’ll get the status code 429 (for making too many requests), floodwaiterror, or the “retry-after” error. The message may look like this:
Floodwaiterror invoked while sending a message; forcing 70792 second wait interval for ….
To handle this situation, you’ll have to wait for the specified amount of time (until the limit is reset) and then try sending requests later.
NOTE: The exact duration for the limit may vary, depending on the usage and server load.
How do I avoid getting the Telegram Flood Wait limit error?
Calculate the Flood Wait Rate
The first solution to avoid getting this limit error or 429 error message is to calculate its rate.
Make N number of method calls to receive a FLOOD_WAIT_X. This will give you:
Flood Wait Rate: X+Total time to make method calls
Now, use the sleep function to perform N method calls within the Flood Rate Seconds to avoid getting the flood waited.
Use the Entity Object of the Receiver Party
Some users report that instead of using the receiver’s name as a string, using their entity object as a parameter also helped them avoid hitting this specific limit on Telegram. Here’s the code you can use in the bot API:
with TelegramClientSync(StringSession(session_id), api_id, api_hash) as client: bot_entity = client.get_input_entity(peer=”mybot”) results = await client.send_message(entity=bot_entity, message=message) |
Afterward, try sending requests to users or groups and see if this solution helps.
Perform Tests on the Bot
If you are in the testing phase of your Telegram bot, you can perform the following three tests to reduce the chances of getting the FLOOD_WAIT errors.
- Instead of the production server, connect to the test server (only if possible).
- To connect to the test server, create test accounts with phone numbers 99966XYYYY and use them (X and Y are 0 to 9 digits).
- On the test server, use the phone number that you used to create the API ID/Hash to connect with the server.
Change the Interval of Sending Requests
If you are sending too many messages or notifications to different users on Telegram, you’ll end up receiving this limit error.
Therefore, it’s advisable to change the intervals of sending requests to over a large time period of 8 – 12 hours and verify the fix.
Use Auto Retry Plugin
Once you hit the Flood Limit on Telegram, your only option is to wait for the specified time and then try again. For this, you can use the following Auto retry plugin code in the bot API to avoid getting the error again and send messages to users.
function pause(seconds: number) { return new Promise(resolve => setTimeout(resolve, 1000 * seconds)) } type AutoRetryTransformer = (…args: any[]) => any /** * Specify options when creating an auto retry transformer * function. */ export interface AutoRetryOptions { maxDelaySeconds: Infinity maxRetryAttempts: Infinity retryOnInternalServerErrors: boolean } export function autoRetry( options?: Partial<AutoRetryOptions> ): AutoRetryTransformer { const maxDelay = options?.maxDelaySeconds ?? 3600 const maxRetries = options?.maxRetryAttempts ?? 3 const retryOnInternalServerErrors = options?.retryOnInternalServerErrors ?? false return async (prev, method, payload, signal) => { let remainingAttempts = maxRetries let result = await prev(method, payload, signal) while (!result.ok && remainingAttempts– > 0) { let retry = false if ( typeof result.parameters?.retry_after === ‘number’ && result.parameters.retry_after <= maxDelay ) { await pause(result.parameters.retry_after) retry = true } else if ( result.error_code >= 500 && retryOnInternalServerErrors ) { retry = true } if (!retry) return result else result = await prev(method, payload, signal) } return result } } |
NOTE: You may need to use a queue or configure the plugin in a way that it never takes a lot of time to effectively run the bot on webhooks.
Conclusion
In this article, we’ve explored the reasons for the Telegram flood limit and explained some methods to avoid errors while sending messages or requests to users and groups.
Hopefully, you can now ensure a smoother experience with your bots on Telegram.