The term chat is extremely overloaded, so it’s no surprise many are confused by what it means. This section should hopefully clear that up.
The word chat in Telethon is used to refer a place where messages are sent to. Therefore, a Telethon chat can be another user, a bot, a group, or a broadcast channel. All of those are places where messages can be sent.
Of course, chats do more things than contain messages. They often have a name, username, photo, description, and other information.
When a parameter must be “chat-like”, it means Telethon will accept anything that can be “converted” to a chat. The following types are chat-like:
'me'literal string. This represents the account that is logged in (“yourself”).
'@username'. The at-sign
@is optional. Note that links are not supported.
'+1 23'phone number string. It must be an
strand start with the plus-sign
123integer identifier. It must be an
intand cannot be negative.
Previous versions of Telethon referred to this term as “entity” or “entities” instead.
The Telegram API is very confusing when it comes to the word “chat”. You only need to know about this if you plan to use the Raw API.
A bare chat has less features available than a bare channel
Official clients are very good at hiding this difference.
They will implicitly convert bare chat to bare channel
megagroup when doing certain operations.
Doing things like setting a username is actually a two-step process (migration followed by updating the username).
Official clients transparently merge the history of migrated channel with their old chat.
Telethon classes aim to map to similar concepts in official applications.
Bot API chat
The Bot API follows a certain convention when it comes to identifiers:
User IDs are positive.
Chat IDs are negative.
Channel IDs are also negative, but are prefixed by
Telethon encourages the use of
PackedChat instead of naked identifiers.
As a reminder, negative identifiers are not supported in Telethon’s chat-like parameters.
If you got an Bot API-style ID from somewhere else, you will need to explicitly say what type it is:
# If -1001234 is your ID...
from telethon.types import PackedChat, PackedType
chat = PackedChat(PackedType.BROADCAST, 1234, None)
# ...you need to explicitly create a PackedChat with id=1234 and set the corresponding type (a channel).
# The access hash (see below) will be ``None``, which may or may not work.
The way you encounter chats in Telethon is no different from official clients. If you:
…have joined a group or channel, or have sent private messages to some user, you can
…know the user is in your contact list, you can
…know the user has a common chat with you, you can
get_participants()of the chat in common.
…know the username of the user, group, or channel, you can
…are a bot responding to users, you will be able to access the
Chats access hash
Users, supergroups and channels all need an access hash.
In Telethon, the
PackedChat is the recommended way to deal with the identifier-hash pairs.
This compact type can be used anywhere a chat is expected.
It’s designed to be easy to store and cache in any way your application chooses.
Bot accounts can get away with an invalid access hash for certain operations under certain conditions. The same is true for user accounts, although to a lesser extent.
When using just the identifier to refer to a chat, Telethon will attempt to retrieve its hash from its in-memory cache.
If this fails, an invalid hash will be used. This may or may not make the API call succeed.
For this reason, it is recommended that you always use