how to add a userbot to a group by link? python telegram api

Question:

I get a link with which I need to add the bot to a group (private or public) I did not find anything that could help me

my code:

link = 'https://t.me/+r....'

client = TelegramClient(session='joiner', api_id=api_id, api_hash=api_hash)
#login process


result = client(functions.channels.JoinChannelRequest(channel=link)) #try to join
#error message (Cannot get entity from a channel (or group) that you are not part of. Join the group and retry)
Asked By: Artik gnom

||

Answers:

JoinChannelRequest is used to join public channels (both broadcast and megagroups are channels). You need the provide the channel itself (or use its username to let the library fetch it for you):

await client(functions.channels.JoinChannelRequest(
    channel='username'
))

ImportChatInviteRequest is used to join private chat and channels:

await client(functions.messages.ImportChatInviteRequest(
    hash='A4LmkR23G0IGxBE71zZfo1'
))

In this case you need to provide the final part of the invite link, not the entire link.

Answered By: Lonami