Scraping Telegram Group Members with Python and Telethon

Telegram is one of the best communications apps around the world. People usually use Telegram for managing their communities and promotions.

Startup companies or ongoing projects use Telegram for bringing audience attention to their products and services. Telegram Members are engaging with the community! This is what we all want. Engaged members will help to grow the community.

In this tutorial, you will learn how to use Telegram API extract group members.

So why scraping members from Telegram Groups?

It’s a nice opportunity to get attentions from related Telegram groups. You may want to scrape members from other related groups and add them to yours. Also, you can send messages to them and start engaging (Without Spamming!)

Enough Talking. Let’s get our hands dirty with the code.

Create a Telegram App and Get Your Credentials

Go to my.telegram.org  and log in.

Click on API development tools and fill the required fields.

You can choose any name for your app. After submitting, you will receive api_id and api_hash. Save them somewhere. You will use these credentials to login to Telegram API.

 

Install Telethon

Telethon is a  great MTProto API Telegram client library written by LunamiWebs, you can check the Github page here. You can install telethon using pip:

 

Note: If you are on Linux or Mac, you might need to use sudo before pip to avoid permissions issues.

 

Create Client Object and Login

Latest version of telethon has two sync and async modules. The async module is using asyncio which is out of the scope of this article. Although you can get the same functionality using both modules but for the sake of simplicity we will use the sync module  in this tutorial.

So as the first step you need to import the sync module from Telethon library.

Then, instantiate your client object using the credentials you got before.

Next step would be connecting to telegram and checking if you are already authorized. Otherwise send an OTP code request and ask user to enter the code they received on their telegram account.

After logging in, a .session file will be created. This is a database file which makes your session persistent.

 

Listing All Telegram Groups

Create an empty list for chats and populate with the results which you get from GetDialogsRequest. You need to import two more functions:  GetDialogsRequest and  InputPeerEmpty

Note: offset_date and  offset_peer are used for filtering the chats. We are sending empty values to these parameters so API returns all chats. offset_id and limit are used for pagination. Here we are getting last 200 chats of the user.

In this tutorial, we assume that we are only interested in mega groups; so check if the megagroup attribute of the chat is True and add it to your list.

This is warped inside a try, except block avoid getting an error, because some chats do not have megagroup attribute at all. So basically we skip those using continue statement.

 

Ask User to Select a Group to Scrape Members

After listing the groups, prompt the user to input a number and select the group they want. When this code is executed it loops through every group that you stored in previous step and print it’s name starting with a number. This number is the index of that is your group list.

Ask user to enter a number associated with a group.  Then use this number as index to get the target group.

 

Get All Telegram Group Members

The last but not least step is to export all members (participants) of the Telegram group. Fortunately, there is a function for this in Telethon library which makes our job really simple.

Create an empty list of users and get members using the  get_participants  function and populate the list.

Important: Set the aggressive parameter to True otherwise you will not get more than 10k members. When aggressive is set to true, Telethon will perform an a-z search in the group’s participants and it usually extracts more than 90% of the members.

 

Store Scraped Telegram Members in a CSV File

Now use Python’s csv module to store the scraped data in a CSV file. First open a csv file in the write mode with an UTF-8 encoding. This is important because users might have non ASCII names which is very common in Telegram groups. Then create a CSV writer object and write the first row (header) in the CSV file. Finally, loop through every item in the all_participants list and write them to the CSV file.

 

Note 1: Not every user has a username. If the user has no user name the API will return None. To avoid writing None and instead of writing an empty row, check if the user has a user name; otherwise, create an empty string as the username.

Note 2: Similar to the username, some user might don’t have a first name or last name, so we are doing the same thing for name as well.

For some large groups it might take a few minutes to get the members. But finally you should see this message  Members scraped successfully.  which shows that everything worked perfectly.

So we stored username, name, user id, user hash and the group details for every user in the CSV file. You can use user id and user hash to add them to a group or Send a Message to Telegram Group Members Using Telethon. More on that in next tutorials.

 

Complete Code of Telegram Group Members Extraction Tutorial

Here is the complete executable code for this tutorial.

 

 

I speak Python!

Majid Alizadeh is a freelance developer specialized in web development, web scraping and automation. He provides high quality and sophisticated software for his clients. Beside Python he works with other languages like Ruby, PHP and JS as well.

Rating: 5.0/5. From 3 votes.
Please wait...

Leave a Reply