[英]How to get all users in a telegram private channel using telethon?
我正在尝试在我是管理员的私人电报频道中获取使用 Telethon 的用户列表,并将其打印到控制台。 我已经尝试使用非私人频道使用相同的代码,但我遇到了同样的问题。
我想使用的用户名参考是什么? 我不明白那是我的电报用户名,我的频道名称,我好像我要在频道里说话,还是什么。 有人可以帮忙吗?
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 = XXXX# Use your own values here. https://my.telegram.org
api_hash = 'XXX'
phone_number = 'XXXX'
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('Jimtest')) # Your channel username --is this the username of my channel? how do I get the right value for this? It isays username, but seems to reference the channel name?
user = client(ResolveUsernameRequest('@jim')) # what is this value? my username? my "name" in the chat? the username that outputs when I type as admin in the chat?
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, 0))
for _user in result.users:
print(_user.id)
我收到以下看起来是频道或用户名问题的错误? 这些值应该是什么?
Traceback (most recent call last):
File "./maybework.py", line 33, in <module>
user = client(ResolveUsernameRequest('Jimtest')) # Your channel admin
username
File "/usr/local/lib/python3.6/dist-
packages/telethon/telegram_bare_client.py", line 459, in __call__
result = self._invoke(call_receive, *requests)
File "/usr/local/lib/python3.6/dist-
packages/telethon/telegram_bare_client.py", line 551, in _invoke
raise next(x.rpc_error for x in requests if x.rpc_error)
telethon.errors.rpc_error_list.UsernameNotOccupiedError: The username is not in use by anyone else yet
一旦我修复了那个,或者至少没有返回错误,我在下一个收到这个错误。
Traceback (most recent call last):
File "./maybework.py", line 33, in <module>
user = client(ResolveUsernameRequest('jimtest')) # Your channel admin
username
File "/usr/local/lib/python3.6/dist-
packages/telethon/telegram_bare_client.py", line 459, in __call__
result = self._invoke(call_receive, *requests)
File "/usr/local/lib/python3.6/dist-
packages/telethon/telegram_bare_client.py", line 551, in _invoke
raise next(x.rpc_error for x in requests if x.rpc_error)
telethon.errors.rpc_error_list.UsernameNotOccupiedError: The username is not in use by anyone else yet
您可以在Telethon V 0.19中使用此代码
from telethon import TelegramClient
import socks
from telethon.tl.functions.channels import GetParticipantsRequest
api_id = XXXX
api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
phone_number = '+98XXXXXXXX'
################################################
# invite link for private channel
inveite_link='telegram.me/joinchat/AAAAAE84o7DErer_YqAohQ'
################################################
proxy_ip = 'YY.YY.YY.YY'
proxy_username = 'proxy_username'
proxy_password = 'proxy_password'
proxy_port = 123456
################################################
client = TelegramClient('session_name',
api_id,
api_hash,
proxy=(socks.HTTP, proxy_ip, int(proxy_port),
None, proxy_username, proxy_password))
assert client.connect()
if not client.is_user_authorized():
client.send_code_request(phone_number)
me = client.sign_in(phone_number, input('Enter code: '))
channel = client.get_entity(inveite_link)
offset = 0
limit = 100
while True:
participants = client(GetParticipantsRequest(
channel, channel, offset, limit,hash=0))
if not participants.users:
break
for _user in participants.users:
print('user : ', _user.id, _user.username)
# save User info to Database
offset += len(participants.users)
我用袜子。 如果您不想使用袜子,请删除它
由于私人频道没有用户名,因此我使用了invite link
您还可以使用channel ID
,而不是invite link
。 在这种情况下,应该使用channel = client.get_entity(PeerChannel(channel_id))
而不是channel = client.get_entity(inveite_link)
我可以为我不是其成员或管理员的私人频道获得至少 2 个用户名吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.