Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all users in a telegram channel using telethon?

I'm new to telethon and python. I have installed telethon in python3 and I want to get all members of a telegram channel or a group . I was searching a lot in the internet and found below code . And I'm trying really hard to understand it .Telegram documentation is not enough to do this . Is there a better solution ?

from telethon import TelegramClient

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

from telethon.tl.types import ChannelParticipantsRecent
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 = 12345
api_hash = '8710a45f0f81d383qwertyuiop'
phone_number = '+123456789'

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('channelusername')) # Your channel username

user = client(ResolveUsernameRequest('admin')) # 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
filter = ChannelAdminLogEventsFilter(True, False, False, False, True, True, True, True, True, True, True, True, True, True)
cont = 0
list = [0,100,200,300]
for num in list:
    result = client(GetParticipantsRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), filter, num, 100))
    for _user in result.users:
        print( str(_user.id) + ';' + str(_user.username) + ';' + str(_user.first_name) + ';' + str(_user.last_name) )
with open(''.join(['users/', str(_user.id)]), 'w') as f: f.write(str(_user.id))

But I'm getting this error . What have I missed ?

Traceback (most recent call last):
  File "run.py", line 51, in <module>
    result = client(GetParticipantsRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), filter, num, 100))
TypeError: __init__() missing 1 required positional argument: 'hash'
like image 919
lasan Avatar asked Dec 07 '17 15:12

lasan


People also ask

How to create Telegram app in Python with simple steps?

Fig 1. Telegram App Step 2. Telethon installation. Now, install T elethon python package on your system using terminal command pip install telethon . step 3. Telegram client creation. Create telegram client. Step 4. Getting User's Information. Extract users information as list using client.get_participants . Participants list in below format.

How to Sync Telegram client with telethon?

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.

How to add scraped telegram members to your group?

You can use user id and user hash to Add Scraped Telegram Members to Your Group or Send a Message to Telegram Group Members Using Telethon. More on that in next tutorials. 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.

How blockchain and cryptocurrency related companies use telegram to communicate?

Most of the blockchain and cryptocurrencies related companies use telegram to communicate with their world wide customers and supporters because of its unique features. Telegram groups’ data, such as user’s informations, chats of specific channels, are analyzed to get insights of channels or to get airdrop participants info etc.


2 Answers

Sean answer won't make any difference.

Your code works for older Telethon versions. In the new versions, a new argument hash is added to GetParticipantsRequest method. Therefore, you need to pass hash as an argument too. Add hash=0 like this:

result = client(GetParticipantsRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), filter, num, 100, 0))

Note that the hash of the request is not the channel hash. It's a special hash calculated based on the participants you already know about, so Telegram can avoid resending the whole thing. You can just leave it to 0.

Here is an up-to-date example from official Telethon wiki.

like image 65
Ali Hashemi Avatar answered Sep 21 '22 15:09

Ali Hashemi


I think You can use this code in the new version of Telethon

from telethon import TelegramClient
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.types import ChannelParticipantsSearch

api_id = XXXXX
api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
phone_number = '+98XXXXXXXX'
################################################
channel_username = 'tehrandb'
################################################

client = TelegramClient('session_name',api_id,api_hash)

assert client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))

# ---------------------------------------
offset = 0
limit = 200
my_filter = ChannelParticipantsSearch('')
all_participants = []
while_condition = True
# ---------------------------------------
channel = client(GetFullChannelRequest(channel_username))
while while_condition:
    participants = client(GetParticipantsRequest(channel=channel_username, filter=my_filter, offset=offset, limit=limit, hash=0))
    all_participants.extend(participants.users)
    offset += len(participants.users)
    if len(participants.users) < limit:
         while_condition = False

I used ‍Telethon V0.19, but the previous versions are pretty much the same

like image 35
Alihossein shahabi Avatar answered Sep 19 '22 15:09

Alihossein shahabi