Telthon client Get link/url for all Groups or channel

Question:

Hello im try to find a solution to get for all groups i have in telgram app to save in one list
If is group i can generate a link
if is private channel can i generate join url

i get group id name and all info but i can get URL

Thanks

`

 async for dialog in client.iter_dialogs():
   if dialog.is_group:                   
                    try:                    

                        print('name:{0} ids:{1} is_user:{2} is_channel:{3} is_group:{4}'.format(dialog.name,dialog.id,dialog.is_user,dialog.is_channel,dialog.is_group))    
                        #print(dialog.is_banned)   
                        print(dialog.name)  
                        print(dialog.message.peer_id.channel_id)
                        print(dialog.entity)

                        exit()

                    except Exception as e:

                        print(e)
                        error(str(row[1]), str(dialog.name), str(e))
                        print('____________E_R_R_O_R___________')
                        exit()

`

i have try some other librari but i dont have find any solution

Asked By: kadriManaj

||

Answers:

"URL" is "https://t.me/username". if an entity object has username attribute; it’s public. else if you’re an owner or admin (with needed permissions) you get the invite link by making a seperate request.

import telethon.tl.functions as _fn 

async for d in client.iter_dialogs():
  if not d.is_group: continue
  try:
    print(f'Name:{d.name} ID:{d.id} is_user:{d.is_user} is_channel:{d.is_channel} is_group:{d.is_group}')

    en = d.entity
    public = hasattr(en, 'username') and en.username
    is_chat = d.is_group and not d.is_channel and not en.deactivated
    admin = en.creator or (en.admin_rights and en.admin_rights.invite_users)

    if not d.is_channel or is_chat: continue

    if public: print(f'Link: https://t.me/{en.username}')

    elif admin:
      if is_chat: r = await client(_fn.messages.GetFullChatRequest(en.id))
      else: r = await client(_fn.channels.GetFullChannelRequest(en))

      link = r.full_chat.exported_invite
      print(f'Link: {link.link}')
  except Exception as e:
    print(e)
    error(str(row[1]), d.name, str(e))
    print('____________E_R_R_O_R___________')
    exit()
Answered By: MustA
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.