如何使用 telethon 让所有用户进入电报频道?
How to get all users in a telegram channel using telethon?
我是电视节目和 python 的新手。我在 python3 中安装了 telethon,我想获得电报频道或群组的所有成员。我在互联网上搜索了很多,发现下面的代码。我真的很努力去理解它。Telegram 文档不足以做到这一点。有更好的解决方案吗?
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))
但是我遇到了这个错误。我错过了什么?
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'
使用 client.invoke()
而不是 client()
。
可以参考official guide.
肖恩的回答不会有任何区别。
您的代码适用于较旧的 Telethon 版本。在新版本中,新参数 hash
被添加到 GetParticipantsRequest
方法。因此,您也需要将 hash
作为参数传递。像这样添加 hash=0
:
result = client(GetParticipantsRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), filter, num, 100, 0))
请注意,请求的 hash
不是频道哈希。它是根据您已知的参与者计算的特殊哈希值,因此 Telegram 可以避免重新发送整个内容。您可以将其保留为 0。
Here 是来自官方 Telethon wiki 的最新示例。
我想你可以在新版本中使用这段代码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
我用的是Telethon V0.19
,但是之前的版本都差不多
channel = client(ResolveUsernameRequest('channel_name'))
user_list = client.iter_participants(entity=channel)
for _user in user_list:
print(_user)
或
user_list = client.get_participants(entity=channel)
for _user in user_list:
print(_user)
你可以试试下面的代码,它有效,我测试过。但我确实有一个问题,因为 Telethon 库并没有增加所有用户,它只增加了 90% 的用户。我认为它以某种方式跳过了一些...不知道为什么。
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import csv
api_id = 123456
api_hash = 'YOUR_API_HASH'
phone = '+111111111111'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
chats = []
last_date = None
chunk_size = 200
groups=[]
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
chats.extend(result.chats)
for chat in chats:
try:
if chat.megagroup== True:
groups.append(chat)
except:
continue
print('Choose a group to scrape members from:')
i=0
for g in groups:
print(str(i) + '- ' + g.title)
i+=1
g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]
print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=True)
print('Saving In file...')
with open("members.csv","w",encoding='UTF-8') as f:
writer = csv.writer(f,delimiter=",",lineterminator="\n")
writer.writerow(['username','user id', 'access hash','name','group', 'group id'])
for user in all_participants:
if user.username:
username= user.username
else:
username= ""
if user.first_name:
first_name= user.first_name
else:
first_name= ""
if user.last_name:
last_name= user.last_name
else:
last_name= ""
name= (first_name + ' ' + last_name).strip()
writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id])
print('Members scraped successfully.')
使用 telethon 获取电报频道所有用户的简单方法。并确保您拥有用于频道或组的有效权限(由 GetParticipantsRequest 引起)。
from telethon.tl.functions.contacts import ResolveUsernameRequest
channel = await client(ResolveUsernameRequest('channel_name'))
async for _user in client.iter_participants(entity=channel):
print(_user)
我是电视节目和 python 的新手。我在 python3 中安装了 telethon,我想获得电报频道或群组的所有成员。我在互联网上搜索了很多,发现下面的代码。我真的很努力去理解它。Telegram 文档不足以做到这一点。有更好的解决方案吗?
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))
但是我遇到了这个错误。我错过了什么?
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'
使用 client.invoke()
而不是 client()
。
可以参考official guide.
肖恩的回答不会有任何区别。
您的代码适用于较旧的 Telethon 版本。在新版本中,新参数 hash
被添加到 GetParticipantsRequest
方法。因此,您也需要将 hash
作为参数传递。像这样添加 hash=0
:
result = client(GetParticipantsRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), filter, num, 100, 0))
请注意,请求的 hash
不是频道哈希。它是根据您已知的参与者计算的特殊哈希值,因此 Telegram 可以避免重新发送整个内容。您可以将其保留为 0。
Here 是来自官方 Telethon wiki 的最新示例。
我想你可以在新版本中使用这段代码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
我用的是Telethon V0.19
,但是之前的版本都差不多
channel = client(ResolveUsernameRequest('channel_name'))
user_list = client.iter_participants(entity=channel)
for _user in user_list:
print(_user)
或
user_list = client.get_participants(entity=channel)
for _user in user_list:
print(_user)
你可以试试下面的代码,它有效,我测试过。但我确实有一个问题,因为 Telethon 库并没有增加所有用户,它只增加了 90% 的用户。我认为它以某种方式跳过了一些...不知道为什么。
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import csv
api_id = 123456
api_hash = 'YOUR_API_HASH'
phone = '+111111111111'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
chats = []
last_date = None
chunk_size = 200
groups=[]
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
chats.extend(result.chats)
for chat in chats:
try:
if chat.megagroup== True:
groups.append(chat)
except:
continue
print('Choose a group to scrape members from:')
i=0
for g in groups:
print(str(i) + '- ' + g.title)
i+=1
g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]
print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=True)
print('Saving In file...')
with open("members.csv","w",encoding='UTF-8') as f:
writer = csv.writer(f,delimiter=",",lineterminator="\n")
writer.writerow(['username','user id', 'access hash','name','group', 'group id'])
for user in all_participants:
if user.username:
username= user.username
else:
username= ""
if user.first_name:
first_name= user.first_name
else:
first_name= ""
if user.last_name:
last_name= user.last_name
else:
last_name= ""
name= (first_name + ' ' + last_name).strip()
writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id])
print('Members scraped successfully.')
使用 telethon 获取电报频道所有用户的简单方法。并确保您拥有用于频道或组的有效权限(由 GetParticipantsRequest 引起)。
from telethon.tl.functions.contacts import ResolveUsernameRequest
channel = await client(ResolveUsernameRequest('channel_name'))
async for _user in client.iter_participants(entity=channel):
print(_user)