【问题标题】:How to get all users in a telegram private channel using telethon?如何使用 Telethon 让所有用户进入电报私人频道?
【发布时间】:2018-10-14 23:08:14
【问题描述】:

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

【问题讨论】:

  • 你用的是哪个版本的?

标签: python telegram telethon


【解决方案1】:

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

【讨论】:

  • 我用上面的代码只能得到200个用户,我怎么能得到更多呢?多达 30000 个用户
猜你喜欢
  • 2018-05-21
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
相关资源
最近更新 更多