Skip to content

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

Merged
chigwell merged 1 commit into
chigwell:mainfrom
embogomolov:fix/resolve-unmarked-ids
May 7, 2026
Merged

Resolve unmarked channel/chat IDs passed as positive integers#92
chigwell merged 1 commit into
chigwell:mainfrom
embogomolov:fix/resolve-unmarked-ids

Conversation

@embogomolov
Copy link
Copy Markdown
Contributor

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

@embogomolov
Copy link
Copy Markdown
Contributor 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!

@embogomolov embogomolov force-pushed the fix/resolve-unmarked-ids branch from 1241cb0 to b809600 Compare May 7, 2026 10:11
@chigwell chigwell merged commit 651c189 into chigwell:main May 7, 2026
4 checks passed
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