Skip to content

Fix channel/chat IDs being misinterpreted as user IDs#90

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

Fix channel/chat IDs being misinterpreted as user IDs#90
embogomolov wants to merge 1 commit intochigwell:mainfrom
embogomolov:fix/marked-ids

Conversation

@embogomolov
Copy link
Copy Markdown

What's broken

Telethon uses unmarked IDs internally — positive integers for everything. When the server returns these raw IDs to callers (say, from list_chats), and the caller passes them back into get_entity(int), Telethon sees a positive number and assumes it's a user. Channels and group chats get a ValueError: Could not find the input entity for PeerUser.

Any "list chats → pick one → do something" workflow breaks intermittently, especially after long sessions when entity cache expires.

The fix

get_marked_id() helper that converts entity IDs to Telethon's marked format before returning:

  • Channels: -100{id} (PeerChannel)
  • Chats: -{id} (PeerChat)
  • Users: unchanged

Applied in all places where entity IDs go out to callers: list_chats, get_chats, get_chat, format_entity, get_direct_chat_by_contact, get_contact_chats, create_group, get_bot_info, get_folder.

How to test

  • Call list_chats, grab a channel ID, pass it to search_messages or get_history — should work without errors
  • Channel IDs now look like -1001676885811 instead of 1676885811

Telethon internally uses unmarked IDs (positive integers) for channels
and chats. When these IDs are returned to callers and later passed back
to get_entity(int), Telethon interprets positive ints as PeerUser,
causing ValueError ("Could not find the input entity for PeerUser").

Add get_marked_id() helper that converts:
- Channel -> -100{id} (PeerChannel format)
- Chat -> -{id} (PeerChat format)
- User -> id (unchanged)

Apply it in all places where entity IDs are returned: list_chats,
get_chats, get_chat, format_entity, get_direct_chat_by_contact,
get_contact_chats, create_group, get_bot_info, get_folder.
@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