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
- new (#15) * except and propagate TypeNotFoundError during update handling * Better document breaking ToS will lead to bans Closes #4102. * Fix KeyError when ID is in cache but queried withou... — committed to Jisan09/Telethon by Jisan09 a year ago
- Fix KeyError when ID is in cache but queried without mark Closes #4084. — committed to JackPot777/Telethon by Lonami a year ago
is a different error from
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).
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.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 orPeerChannel(ID)
and the library will try its best. I don’t think there’s more to fix here.