Telethon: Cannot obtain Channel via get_entity by it's ID

Checklist

  • The error is in the library’s code, and not in my own.
  • I have searched for this issue before posting it and there isn’t a duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

Hi. Recently i bumped Telethon version from v1.25.4 to the most recent v1.28.2 and discovered that it’s now impossible to obtain Channel entity via TelegramClient.get_entity. This behaviour started from the v1.28.0.

Maybe worth noting that i use a StringSession instead of default SQLite.

Code that causes the issue

...
# This doesn't work
await client.get_entity(channel_id)

# This doesn't work too
entity = await client.get_input_entity(channel_id)
await client.get_entity(entity)

# But this is OK
peer_channel = PeerChannel(channel_id)
channel_entity = await client.get_entity(entity)

Traceback

Traceback (most recent call last):
  File "/home/non/Scripts/py_projects/tgbox-env/tgbox/a_test.py", line 648, in <module>
    loop.run_until_complete(main())
  File "uvloop/loop.pyx", line 1501, in uvloop.loop.Loop.run_until_complete
  File "/home/non/Scripts/py_projects/tgbox-env/tgbox/a_test.py", line 587, in main
    print(await drb.tc.get_entity(ie))
  File "/home/non/Scripts/py_projects/tgbox-env/lib/python3.9/site-packages/telethon/client/users.py", line 332, in get_entity
    result.append(id_entity[utils.get_peer_id(x)])
KeyError: 1610806777

Is it OK?

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 2
  • Comments: 19 (16 by maintainers)

Commits related to this issue

Most upvoted comments

ValueError: Could not find the input entity for PeerUser(user_id=1005640892) (PeerUser). Please read https://docs.telethon.dev/en/stable/concepts/entities.html to find out more details.

is a different error from

KeyError: 1610806777

which is what https://github.com/LonamiWebs/Telethon/commit/980f8b32fc16ff1ec6708b93fd1f923b8c684c3d fixed. Note how this “new” error is on line 332 whereas the old one is in 287 (i.e. it’s hitting a different error at a later point).

While this seems to make a fix to the SQLite session, the String one still doesn’t work. The problem is that for some reason StringSession cache Channel ID without mark as InputPeerUser, so we receive this exception.

That doesn’t seem to be the case. In https://github.com/LonamiWebs/Telethon/issues/4084#issuecomment-1535304234 you showed that print(self.session._entities) gave (-1001610806777, 2328109287557450445, ..., so with the -100 mark.

The first mentioned here exception (KeyError) disappears if we remove this block (isn’t good for sure 😃 with the addition of upper one (try/except) and all works just as expected (we can retrieve Channel by ID without mark).

Trying to fetch the user or channel with access_hash of 0 is more of a hacky last-resort attempt at getting something that works. It’s not something I would rely on. (I really dislike the fact that it works, as it defeats the purpose of access-hashes in the first place if the server knows what we can access on its own and the lines are blurry.)

If you know it’s a channel, use the -100ID form or PeerChannel(ID) and the library will try its best. I don’t think there’s more to fix here.