-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Joining a chat or channel
Note that Chats are normal groups, and Channels are a special form of Chats, which can also be super-groups if their megagroup member is True.
Once you have the entity of the channel you want to join to, you can make use of the JoinChannelRequest to join such channel:
from telethon.tl.functions.channels import JoinChannelRequest
client(JoinChannelRequest(channel))
# In the same way, you can also leave such channel
from telethon.tl.functions.channels import LeaveChannelRequest
client(LeaveChannelRequest(input_channel))For more on channels, check the channels namespace.
If all you have is a link like this one: https://t.me/joinchat/AAAAAFFszQPyPEZ7wgxLtd, you already have enough information to join! The part after the https://t.me/joinchat/, this is, AAAAAFFszQPyPEZ7wgxLtd on this example, is the hash of the chat or channel. Now you can use ImportChatInviteRequest as follows:
from telethon.tl.functions.messages import ImportChatInviteRequest
updates = client(ImportChatInviteRequest('AAAAAEHbEkejzxUjAUCfYg'))If you don't want to add yourself, maybe because you're already in, you can always add someone else with the AddChatUserRequest, which use is very straightforward:
from telethon.tl.functions.messages import AddChatUserRequest
client(AddChatUserRequest(
chat_id,
user_to_add,
fwd_limit=10 # allow the user to see the 10 last messages
))If you don't need to join but rather check whether it's a group or a channel, you can use the CheckChatInviteRequest, which takes in the hash of said channel or group.