Search code examples
pythontelegramtelethon

Telegram through telethon timeouts on my server. FloodWaitError keeps multiplying on a script that works fine on local


The application works fine on my PC. No timeouts. When I turned it off to move it to my server, at first after logging into the session with phone number, password, and login code, this error appeared:

telethon.errors.rpcerrorlist.FloodWaitError: A wait of 31387 seconds is required (caused by ResolveUsernameRequest)

on line

client.get_messages(channel_name, ids=141)

I waited way longer than the ~8+ hours.

My PC program was working fine. I again stopped it and opened the same script on my server.

Now the wait is doubled:

telethon.errors.rpcerrorlist.FloodWaitError: A wait of 67203 seconds is required (caused by ResolveUsernameRequest)

on same line. Unusable.

This is MY OWN channel owned by the very account I sign in to that I'm getting the message from...


Solution

  • Your question title mentions you "can't login", but your question body seems to indicate otherwise. I will assume your problem is what's described in the question's body.

    Telethon will use the .session file as a cache, containing, among other things, usernames and their corresponding id and access_hash. Both of these values are required to use a chat as a parameter for the API requests.

    If the username is not in cache, the library will need to fetch it first. This fetching has a high flood wait, and there's no way to bypass that.

    The correct approach here would be to:

    print(await client.get_input_entity(channel_name))
    

    once, and then hardcode the output in your script, like so:

    from telethon import types
    
    CHANNEL = types.InputPeerChannel(channel_id=..., access_hash=...)
    

    (You can use a different mechanism to store it persistently. Hardcoding it is just the simplest way to do it.)

    And then you can use CHANNEL instead of channel_name.