LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.77k stars 1.39k forks source link

I can get entity by ID, if I will get entity by username at first #1191

Closed ghost closed 5 years ago

ghost commented 5 years ago

Checklist check

Code that causes the issue

entity = client.get_entity(1151511560) #Error
entity_v2 = client.get_entity("ekat01") #Success
entity_v2.id == 1151511560 #True

Video proof: https://youtu.be/mnDNZZir5PY

Traceback

Traceback (most recent call last):
  File "<pyshell#95>", line 1, in <module>
    print(client.get_entity(283241839))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telethon/client/users.py", line 250, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/telethon/client/users.py", line 420, in get_input_entity
    .format(peer)
ValueError: Could not find the input entity for <telethon.tl.types.PeerUser object at 0x116b05f60>. Please read https://telethon.readthedocs.io/en/latest/concepts/entities.html to find out more details.
ghost commented 5 years ago

If I am not wrong, that is explained in the docs "Users, chat and channel, Important section": https://telethon.readthedocs.io/en/stable/extra/basic/entities.html

Once the library has “seen” the entity, you can use their integer ID. You can’t use entities from IDs the library hasn’t seen. You must make the library see them at least once and disconnect properly. You know where the entities are and you must tell the library. It won’t guess for you.

Lonami commented 5 years ago

On a clean session,

with client:
    try:
        client.get_entity(1151511560)
    except ValueError:
        print('Error as expected')

    client.get_entity("ekat01")
    client.get_entity(1151511560)
    print('Works as expected')

prints:

Error as expected
Works as expected

On a second run,

with client:
    client.get_entity(1151511560)
    print('Works as expected')

prints:

Works as expected
ghost commented 5 years ago

Thanks a lot!

David-Shambik commented 2 years ago

what if a user has no username? how can i get the entitiy?

Morrigan-Ship commented 1 year ago

same problem

wseidel commented 10 months ago

Hey everyone, About the doubt of @ShambikDavid:

what if a user has no username? how can i get the entitiy?

I was able to capture the user (Entity) in the following way:

user = client.get_entity( PeerUser( user_id ) )

Look at here:

https://docs.telethon.dev/en/stable/concepts/entities.html

Hugs,