Scrape Telegram Members

If you need to get a list of all the members from a Telegram group then this is the right article for you. In this article, I will be showing you steps to successfully scrape Telegram members from any public Telegram group.

Installing Telethon

We need to install Telethon Library in order to scrape members from any group. You can install Telethon by using pip and running the below commands.

pip install Telethon

Installing Pandas

We also need to install the Pandas library as we will be saving all the scraped members in a CSV or Excel File. You can install Pandas by running the below command.

pip install pandas

Getting Telegram API ID and API Hash

You will need to get a Telegram API ID and API Hash in order to programmatically access your Telegram account. You will need to register to My Telegram Org in order to get these details.

Python Program to Scrape Telegram Members

After following the above steps, now you are ready to start developing the Python program to scrape Telegram Members. Below is the complete Python code, I will be explaining the important part of the code.

import pandas as pd
from telethon.sync import TelegramClient
from telethon.tl.functions.channels import InviteToChannelRequest
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.functions.messages import GetDialogsRequest, AddChatUserRequest
from telethon.tl.types import InputPeerEmpty
from telethon.tl.types import InputPeerChannel
from telethon.tl.types import InputPeerUser


api_id = "GIVE YOUR API ID"
api_hash = "GIVE YOUR API HASH"

client = TelegramClient('test', api_id, api_hash)
client.connect()

# Provide the name of the Telegram group.
# In below example we are scraping members from vikasjhahelloworld001 Group

target_group = client.get_entity("vikasjhahelloworld001")

print("Group ID: {}, Group Hash: {}".format(target_group.id, target_group.access_hash))

target_group_entity = InputPeerChannel(target_group.id,target_group.access_hash)


# Change the location of the file as per your need

df = pd.read_excel("C:\\crypto\\members.xls")
print(df.columns)
users = df.to_records(index=False)
print(users)

for i in range(6):
    try:
        print("Adding: {}".format(users[i]['username']))
        print("Userid: {} , user_access_hash: {}".format(users[i]['id'], users[i]['access_hash']))
        user_to_add =InputPeerUser(users[i]['id'], users[i]['access_hash'])
        print("user to add: {}".format(user_to_add))
        client(AddChatUserRequest("vikasjhahelloworld001",users[i]['username'],fwd_limit=1))
    except Exception as e:
        print(e)

Learn to Scrape Telegram Group Messages

Comments are closed.

Scroll to Top