Resolve unmarked channel/chat IDs passed as positive integers#92
Open
embogomolov wants to merge 1 commit intochigwell:mainfrom
Open
Resolve unmarked channel/chat IDs passed as positive integers#92embogomolov wants to merge 1 commit intochigwell:mainfrom
embogomolov wants to merge 1 commit intochigwell:mainfrom
Conversation
Telethon's get_entity(int) maps positive integers to PeerUser. When callers pass a bare channel ID like 1676885811 (without the -100 prefix), Telethon looks for a user with that ID and fails with ValueError. The existing fallback (get_dialogs cache warming) doesn't help because Telethon still interprets the positive int as PeerUser on retry. Add _marked_id_candidates() that generates -100X (PeerChannel) and -X (PeerChat) variants from a positive int. Both resolve_entity() and resolve_input_entity() now try these variants as a last resort before raising, so unmarked IDs from any source (LLMs, cached values, manual input) resolve correctly.
Author
|
Anyone? |
Owner
|
Hey @embogomolov, thank you for the contribution! Could I please ask you to fix the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's broken
When a caller passes a bare channel ID like
1676885811(without the-100prefix), Telethon'sget_entity(int)runs it throughresolve_id(), getsPeerUser, and looks for a user with that ID. There is no such user, so it throwsValueError.The existing fallback (
get_dialogs()cache warming) doesn't help — Telethon still reads the positive int as PeerUser on retry, so the second attempt fails the same way.This happens often with MCP callers (LLMs, automation tools) that store or copy channel IDs as plain numbers without the
-100marker.The fix
_marked_id_candidates()generates-100{id}(PeerChannel) and-{id}(PeerChat) from a positive int.Both
resolve_entity()andresolve_input_entity()now try these variants as a last resort: original ID first, then cache warming + retry, then marked candidates. If everything fails, the error message lists what was tried.How to test
resolve_usernamefor a channel, extract the rawchannel_idfrom the responsesearch_messagesorget_history— should work now instead of throwingValueError_marked_id_candidatesreturns an empty list for non-positive ints