Skip to content

Resolve unmarked channel/chat IDs passed as positive integers#92

Open
embogomolov wants to merge 1 commit intochigwell:mainfrom
embogomolov:fix/resolve-unmarked-ids
Open

Resolve unmarked channel/chat IDs passed as positive integers#92
embogomolov wants to merge 1 commit intochigwell:mainfrom
embogomolov:fix/resolve-unmarked-ids

Conversation

@embogomolov
Copy link
Copy Markdown

What's broken

When a caller passes a bare channel ID like 1676885811 (without the -100 prefix), Telethon's get_entity(int) runs it through resolve_id(), gets PeerUser, and looks for a user with that ID. There is no such user, so it throws ValueError.

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 -100 marker.

The fix

_marked_id_candidates() generates -100{id} (PeerChannel) and -{id} (PeerChat) from a positive int.

Both resolve_entity() and resolve_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

  • Call resolve_username for a channel, extract the raw channel_id from the response
  • Pass that bare positive int to search_messages or get_history — should work now instead of throwing ValueError
  • Negative (already marked) IDs still work as before — _marked_id_candidates returns an empty list for non-positive ints

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.
@embogomolov
Copy link
Copy Markdown
Author

Anyone?

@chigwell
Copy link
Copy Markdown
Owner

chigwell commented Apr 8, 2026

Hey @embogomolov, thank you for the contribution! Could I please ask you to fix the black formatting so the CI tests will pass? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants