How to get a Telegram channel subscribers list in python
Why do you need that list?
If you have a Telegram channel you might want to know who follows you. Other social media platforms shows this info but not Telegram (yet).
I personally have a big channel (9k subs) and noticed that a lot of cool guys & girls follows me (sometimes they DM me). I want to know all my subs since I may find someone I'd like to meet.
The problem
If you click on any Telegram profile, you'll see the mutual chats but not if a user follows your Telegram channel.
If you click on your channel's subscribers list, Telegram will show you only your contacts and 200 latest subscribers.
The solution
There is a search field on Telegram channel subscribers list. For each search query Telegram will response with up to 200 results. We can search by random short strings to pull out everyone who follows your Telegram channel.
Run Telethon
To automate actions we will use Python's Telethon library. Let's init it:
from telethon.tl import functions
from telethon import TelegramClient
api_id, api_hash = # get yours on my.telegram.org
client = TelegramClient("channel_subs", api_id, api_hash)
await client.connect()
Search query generator:
For small channels (around 1k subs) 1-letter-search-queries should be enough. But if you want to scrape large lists I'd suggest to use 2-letter-search-queries due to the limit of search result output (up to 200 users).
import itertools
def search_queries():
alphabets = [
"qwertyuiopasdfghjklzxcvbnm",
"йцукенгшщзхъёфывапролджэячсмитьбю",
"1234567890",
]
for alphabet in alphabets:
for c in alphabet:
yield c
# if your channel is large
# let's generate pairs:
for alphabet in alphabets:
for pair in itertools.permutations(alphabet, 2):
yield "".join(pair)
Final Telethon snippet
participants = {} # store users info here
channel_url = "https://t.me/danokhlopkov" # may be chat_id
for query in search_queries():
async for res in client.iter_participants(
channel_url, search=query
):
participants[res.id] = res.to_dict()
# if you want to be gentle
await asyncio.sleep(random.random() * 3 + 2)
What's next?
Couple ideas to consider:
- Keyword cloud based on
user.about
section. You may find some interesting keywords which can be used as a filter to a large list of followers. - Find profiles on other social networks with the same usernames. E.g. match with Instagram data. Sort all subscribers by their Instagram followers amount to find out more cool people that follows you.
- Parse
user.about
to extract links to their other social profiles and their Telegram channels. - Estimate
user_subscribed_at
anduser_unsubscribed_at
to build something like a CRM for your tg channel.
And ideas for optimisation:
- Instead of iterating over thousands of combinations of 2-letter-search-queries, use the most common pairs first - maybe they would be enough.
- To update the subscriber list daily use the base method without a search query:
client.iter_participants("https://t.me/danokhlopkov")
Thanks for reading. Bookmark & share this article with friends!
More hacks from me:
- This website!
- twitter.com/danokhlopkov
- t.me/danokhlopkov (ru tg blog)