Telethon: FloodWaitError on get_entity()

Created on 16 Dec 2017  路  11Comments  路  Source: LonamiWebs/Telethon

Hi, i write a small code to retrieve only the unread messages from some channels and it works....but after some hours receives FloodWaitError of 24hours

The code runs every 4 minutes, requests "get_entity" for four channels and the GetHistoryRequest of each.
I have tried to increase the time between executions, introduce sleeps between requests, change get_entity by ResolveUsernameRequest ... but I do not know what else to try

Please, any hint or suggestion?

client = TelegramClient('username', api_id, api_hash)
client.connect()

user_id = client.get_me().id    # For use with bot

channels = ["one","two","three","four"]
channel_entities = []

for channel in channels:
    channel_entities.append(client.get_entity(channel))
    time.sleep(1)  # not works :(

for ch_ent in channel_entities:

    result = client(GetHistoryRequest(
        ch_ent,
        limit=25, 
        offset_date=None,
        offset_id=0,
        max_id=0,
        min_id=int(...), 
        add_offset=0
    ))
    time.sleep(6) 

Most helpful comment

Thanks for the reply. First try with .get_input_entity('username'), but this did not solve the problem. In the end, saving the id and the hash in a file did work.

Maybe you could add a little tip in the start guide so that new users do not have the same problem

I leave here part of my code in case it helps someone

```

We have channel name or url into channel.nick

Now, try to load id and hash from config file

( config load code.... )

There is no id and hash

if channel.id is None or channel.hash is None:
print(" -> querying server")

channel_query = None
try:
    channel_query = client.get_input_entity(channel.nick)
except FloodWaitError:
    f_flood_exit()

# Built InputPeerchannel
channel_id = channel_query.channelannel_id
channel_hash = channel_query.access_hash
channel.entity = InputPeerchannel(channel_id, channel_hash)

# Save id and hash to file
save_var(channel.nick + "_id", channel_id)
save_var(channel.nick + "_hash", channel_hash)

Channel id and hash are present

else:
channel.entity = InputPeerchannel(channel.id, channel.hash)
print(" -> loaded from file")

result = client(GetHistoryRequest(
peer=channel.entity,
limit=25,
offset_date=None,
add_offset=0,
offset_id=0,
max_id=0,
min_id=channel.last_msg # Only request new msg
))

All 11 comments

I think ResolveUsernameRequest has a high penalty. Instead call .get_input_entity('username') the first time and print them. Then hardcode and use those, so you save a few calls.

Thanks for the reply. First try with .get_input_entity('username'), but this did not solve the problem. In the end, saving the id and the hash in a file did work.

Maybe you could add a little tip in the start guide so that new users do not have the same problem

I leave here part of my code in case it helps someone

```

We have channel name or url into channel.nick

Now, try to load id and hash from config file

( config load code.... )

There is no id and hash

if channel.id is None or channel.hash is None:
print(" -> querying server")

channel_query = None
try:
    channel_query = client.get_input_entity(channel.nick)
except FloodWaitError:
    f_flood_exit()

# Built InputPeerchannel
channel_id = channel_query.channelannel_id
channel_hash = channel_query.access_hash
channel.entity = InputPeerchannel(channel_id, channel_hash)

# Save id and hash to file
save_var(channel.nick + "_id", channel_id)
save_var(channel.nick + "_hash", channel_hash)

Channel id and hash are present

else:
channel.entity = InputPeerchannel(channel.id, channel.hash)
print(" -> loaded from file")

result = client(GetHistoryRequest(
peer=channel.entity,
limit=25,
offset_date=None,
add_offset=0,
offset_id=0,
max_id=0,
min_id=channel.last_msg # Only request new msg
))

How is saving this to a file different from the .session file?

The amount of calls should be reduced once I merge the sqlite-session branch, as I'll be saving username in the .session file, so less calls need to be made for the input versions. You can use the input versions to get many entities all at once. Usernames will still be resolved one by one if using .get_entity, because they may have changed.

@darkvier So, when you received the floodwaiterror (83508 secs), did you had to wait for 24 hours before trying again?

Of course. You should be able to make other requests though. Resolving usernames has a high penalty.

Hi.
I have same problem with this log:

DEBUG:telethon.telegram_bare_client:Invoking ResolveUsernameRequest
DEBUG:telethon.network.mtproto_sender:Sending requests with IDs: ResolveUsernameRequest: 6550181159891180300
DEBUG:telethon.network.mtproto_sender:Processing Remote Procedure Call result
DEBUG:telethon.network.mtproto_sender:Received response for request with ID 6550181159891180300
DEBUG:telethon.network.mtproto_sender:Confirmed ResolveUsernameRequest through error A wait of 12167 seconds is required
WARNING:telethon.telegram_bare_client:Request invoked too often, wait 12167s
Traceback (most recent call last):
File "history_transfer.py", line 330, in
telegram_channel_entity = telegram_api_instance.get_channel_entity(telegram_channel_username)
File "/root/history_bot/telegram_api.py", line 50, in get_channel_entity
telegram_channel_entity = self.client.get_entity(telegram_channel_username)
File "/usr/local/lib/python3.6/dist-packages/telethon/telegram_client.py", line 2414, in get_entity
for x in inputs
File "/usr/local/lib/python3.6/dist-packages/telethon/telegram_client.py", line 2414, in
for x in inputs
File "/usr/local/lib/python3.6/dist-packages/telethon/telegram_client.py", line 2451, in _get_entity_from_string
result = self(ResolveUsernameRequest(username))
File "/usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py", line 459, in __call__
result = self._invoke(call_receive, *requests)
File "/usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py", line 551, in _invoke
raise next(x.rpc_error for x in requests if x.rpc_error)
telethon.errors.rpc_error_list.FloodWaitError: A wait of 12167 seconds is required

is this exact constraint of making request to telegram api that exceeding that cause this error?
or any other considerations that should i have to avoid this?

Of course. You should be able to make other requests though. Resolving usernames has a high penalty.

We have to do how many times request/minute to avoid being charged a high penalty? Any suggestions?

Of course. You should be able to make other requests though. Resolving usernames has a high penalty.

We have to do how many times request/minute to avoid being charged a high penalty? Any suggestions?

Any updates?

Of course. You should be able to make other requests though. Resolving usernames has a high penalty.

We have to do how many times request/minute to avoid being charged a high penalty? Any suggestions?

Any updates?

I'm trying only one get_entity() request per minute and seeing if Telegram allows it. I would increase the delay if there is a flood error. There seems no other way around this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

saharokk picture saharokk  路  3Comments

chri1389 picture chri1389  路  6Comments

Lerbytech picture Lerbytech  路  5Comments

amir3code picture amir3code  路  6Comments

saeedesfandi picture saeedesfandi  路  3Comments