无法使用 GetParticipantsRequest (Telethon API) 获取 Telegram 频道用户
Cant get Telegram Channel Users with GetParticipantsRequest (Telethon API)
我正在使用 Telethon API,当我尝试让用户进入我自己的电报 channel 时,函数 GetParticipantsRequest 出现权限错误,我在其中我是管理员
telethon.errors.rpc_error_list.ChatAdminRequiredError: (ChatAdminRequiredError(...), 'Chat admin privileges are required to do that in the specified chat (for e
xample, to send a message in a channel which is not yours)')
没有意义,因为当我用我的电报组执行相同的代码例程时,它就像一个魅力。
例程如下:
def getUserNames(channel):
offset = 0
limit = 100
all_participants = []
selected_participants = []
channel = client(ResolveUsernameRequest(channel))
channel = InputChannel(channel.chats[0].id, channel.chats[0].access_hash)
while True:
participants = client(
GetParticipantsRequest(channel, ChannelParticipantsSearch(''), offset, limit, 0)
)
if not participants.users:
break
all_participants.extend(participants.users)
offset += len(participants.users)
if len(all_participants):
for x in all_participants:
if x.username: selected_participants.append(x.username)
print (selected_participants)
return selected_participants
else:
return -1
用户可以使用此代码获取所有参与者:
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
,但是之前的版本都差不多
我正在使用 Telethon API,当我尝试让用户进入我自己的电报 channel 时,函数 GetParticipantsRequest 出现权限错误,我在其中我是管理员
telethon.errors.rpc_error_list.ChatAdminRequiredError: (ChatAdminRequiredError(...), 'Chat admin privileges are required to do that in the specified chat (for e
xample, to send a message in a channel which is not yours)')
没有意义,因为当我用我的电报组执行相同的代码例程时,它就像一个魅力。
例程如下:
def getUserNames(channel):
offset = 0
limit = 100
all_participants = []
selected_participants = []
channel = client(ResolveUsernameRequest(channel))
channel = InputChannel(channel.chats[0].id, channel.chats[0].access_hash)
while True:
participants = client(
GetParticipantsRequest(channel, ChannelParticipantsSearch(''), offset, limit, 0)
)
if not participants.users:
break
all_participants.extend(participants.users)
offset += len(participants.users)
if len(all_participants):
for x in all_participants:
if x.username: selected_participants.append(x.username)
print (selected_participants)
return selected_participants
else:
return -1
用户可以使用此代码获取所有参与者:
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
,但是之前的版本都差不多