我正在尝试在我是管理员的私人电报频道中获取使用 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