Search code examples
pythontelegram-bottelethon

Python telethon. How can you prevent the FloodWaiTError error, or reduce its time


I was building a bot using the telethon library and ran into a problem with a FloodWaitError. I have created a function that subscribes to a list of telegram channels and while it is running I get this error very often. How can it be prevented? I tried to slow down the process of subscribing to channels, but that didn't help either.

client = TelegramClient(name, id, hash)


def is_private(url: str) -> bool:
    return "/+" in url

async def join_channel():
    for links in channels:
        await asyncio.sleep(3)
        try:
            if is_private(links):
                await client(ImportChatInviteRequest(links.split("+")[-1]))
                print(0)
            else:
                await client(functions.channels.JoinChannelRequest(channel=links))
                print(1)
        except ValueError:
            print(f"ValueError cant get entity from: {links}")
        except UserAlreadyParticipantError:
            print(f"Already a participant of {links}")
        except InviteRequestSentError:
            print(f"Successfully requested to join this chat: {links}")
        except telethon.errors.rpcerrorlist.InviteHashExpiredError:
            print("afk")


client.start()
client.loop.run_until_complete(join_channel())
print("Bot start!")
client.run_until_disconnected()

I had a delay of up to 8 hours

Traceback (most recent call last):
  File "F:\proj\PYTHON\TElethonTGbot\main.py", line 107, in <module>
    client.loop.run_until_complete(join_channel())
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
    return future.result()
  File "F:\proj\PYTHON\TElethonTGbot\main.py", line 90, in join_channel
    await client(ImportChatInviteRequest(links.split("+")[-1]))
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 83, in _call
    result = await future
telethon.errors.rpcerrorlist.FloodWaitError: A wait of 232 seconds is required (caused by ImportChatInviteRequest)

Solution

  • The only way to prevent FloodWaitError is by making less requests or slowing down. It exists to prevent API abuse. If you encounter this error often, your code is making too many requests.

    Different requests have different limits. Limits also vary under many other conditions. They're unknown, and probably vary per account too. Telegram is free to change them at any time, so there are no accurate values either.