如何使用telethon在电报专用频道中获取所有用户?

时间:2018-05-04 12:46:35

标签: python telegram 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

1 个答案:

答案 0 :(得分:0)

您可以在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)