Telegram – How to Export Member List

exporttelegram

I'd like to export a list of current members of the channel or group. Is it possible through the UI, alternative client (such as Bettergram), Telegram API or Bots API?

I've seen this feature on some marketing apps (such as Telegram Auto), but I prefer to do it on my own.

One of the use cases is when moving members from channel to the group (without manually clicking all of the members), so the question assumes I've admin rights, but if the method will work for other channels/groups, that's even better.

How such an export can be done?

Best Answer

You can try Telethon which can extract all the members of a given group and saves it in an SQLite database. You need to be an admin for the channel of course.

You can get it with a method like:

from telethon import TelegramClient

from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.functions.channels import GetAdminLogRequest

from telethon.tl.types import InputChannel
from telethon.tl.types import ChannelAdminLogEventsFilter
from telethon.tl.types import InputUserSelf
from telethon.tl.types import InputUser

# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = ****** # Your api_id
api_hash = '********************************' # Your api_hash
phone_number = '+989122594574' # Your phone number

client = TelegramClient(phone_number, api_id, api_hash)
client.session.report_errors = False
client.connect()

if not client.is_user_authorized():
    client.send_code_request(phone_number)
    client.sign_in(phone_number, input('Enter the code: '))


channel = client(ResolveUsernameRequest('tabe_eshgh')) # Your channel username

user = client(ResolveUsernameRequest('amir2b')) # Your channel admin username
admins = [InputUserSelf(), InputUser(user.users[0].id, user.users[0].access_hash)] # admins
admins = [] # No need admins for join and leave and invite filters

filter = None # All events
# param: (join, leave, invite, ban, unban, kick, unkick, promote, demote, info, 
#         settings, pinned, edit, delete)
filter = ChannelAdminLogEventsFilter(True, True, True, False, False, False, False, 
         False, False, False, False, False, False, False)

result = client(GetAdminLogRequest(InputChannel(channel.chats[0].id,
         channel.chats[0].access_hash), '', 0, 0, 10, filter, admins))
##print(result)

for _user in result.users:
    ##print(_user.id)
    with open(''.join(['users/', str(_user.id)]), 'w') as f:
        f.write(str(_user.id))

or method like this:

from telethon import TelegramClient, sync

api_id = 'FILL REAL VALUES HERE'
api_hash = 'FILL REAL VALUES HERE'

client = TelegramClient('xxx', api_id, api_hash).start()

# get all the channels that I can access
channels = {d.entity.username: d.entity
            for d in client.get_dialogs()
            if d.is_channel}

# choose the one that I want to list users from
channel = channels[channel_name]

# get all the users and print them
for u in client.get_participants(channel):
    print(u.id, u.first_name, u.last_name, u.username)

However, first, you need to obtain your api_id and api_hash using MTProto by logging to https://my.telegram.org/ with your existing Telegram account and getting credentials.


Resources: