LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.96k stars 1.4k forks source link

Problem with getting group members. #315

Closed the-lookin closed 7 years ago

the-lookin commented 7 years ago

I have a problem with getting all members of chanel. It`s an error 400:

File "./main.py", line 49, in <module>
    channel, ChannelParticipantsRecent(), offset, limit
  File "/usr/lib64/python3.4/site-packages/telethon/telegram_bare_client.py", line 447, in __call__
    result = self._invoke(sender, call_receive, *requests)
  File "/usr/lib64/python3.4/site-packages/telethon/telegram_bare_client.py", line 505, in _invoke
    raise next(x.rpc_error for x in requests if x.rpc_error)
telethon.errors.rpc_errors_400.ChatAdminRequiredError: (ChatAdminRequiredError(...), 'Chat admin privileges are required to do that in the specified chat (for example, to send a message in a channel which is not yours).')

This is code:


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 code '))

i = 0;

for param in sys.argv:
    if i != 0:
        channel = client(ResolveUsernameRequest(param)).chats[0] 
        #client(JoinChannelRequest(channel))

        offset = 0
        limit = 100
        all_participants = []
        usernames = []

        while True:
            participants = client.invoke(GetParticipantsRequest(
                channel, ChannelParticipantsRecent(), offset, limit
            ))
            if not participants.users:
                break
            all_participants.extend(participants.users)

            for user in participants.users:
                if user.username != None:
                    usernames.append('@' + user.username)
                else:
                    usernames.append('ID' + str(user.id))

            offset += len(participants.users)
'''
Lonami commented 7 years ago

Well, it seems like you need to be an admin:

ChatAdminRequiredError

Don't use ChannelParticipantsRecent. Use something else. And please format the code more nicely next time, it's not so hard to do it:

`⁣``python
code here
`⁣``
the-lookin commented 7 years ago

Something like what?

Lonami commented 7 years ago

No idea, test or use search. I know how the API works, not how to work with the API.

owneroxxor commented 6 years ago

I'am also having the same issue. GetParticipantsRequest returns:

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)

when I try to get the usernames inside my own telegram channel, in which I'am Admin. The interesting part is that the same routine works for groups of mine. Only channels are not working.

Here goes the routine:

def getUserNames(channel):
    offset = 0
    limit = 100
    all_participants = []
    selected_participants = []
    channel = client(ResolveUsernameRequest(channel))
    while True:
        participants = client.invoke(GetParticipantsRequest(
            InputChannel(channel.chats[0].id, channel.chats[0].access_hash), 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)
        return selected_participants
    else:
        return -1

Is there something I'am missing here?

Lonami commented 6 years ago
InputChannel(channel.chats[0].id, channel.chats[0].access_hash)

That's redundant, the library will do it for you. Besides that I've no idea, it should work, make sure you're using the right channel and all that.

l33t-daniel commented 5 years ago
from telethon import TelegramClient, sync

api_id = 00000
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

client = TelegramClient('xxx', api_id, api_hash).start()

for u in client.get_participants('ChannelUserName'):
    print(u.id, u.first_name, u.last_name, u.username)

Work 100%

PashaM999 commented 3 years ago

@l33t-daniel certainly doesn't work now)

ohld commented 1 year ago

Sorry for resurrecting the old issue but

Is there a way to check if logged in user has enough rights to call GetParticipantsRequest for a given chat / channel? I don't feel comfortable to try/except for ChatAdminRequiredError each time. Maybe I can somehow check that with chat/channel metadata?

I've tried to analyse the chat info to understand which field tells me that I can iterate over chat's participants. But I couldn't find it. Here is an example of a chat which allows me to iterate over its participants:

{'_': 'Channel',
 'id': *******,
 'title': '******',
 'photo': {'_': 'ChatPhoto',
  'photo_id': *******,
  'dc_id': 2,
  'has_video': False,
  'stripped_thumb': b'\x01\x08\x08\xa9\x1c\xab\x18Ud\xe4w\x1dh\xa2\x8a,6\xcf'},
 'date': datetime.datetime(2023, 9, 18, 14, 12, 1, tzinfo=datetime.timezone.utc),
 'creator': False,
 'left': True,
 'broadcast': False,
 'verified': False,
 'megagroup': True,
 'restricted': False,
 'signatures': False,
 'min': False,
 'scam': False,
 'has_link': True,
 'has_geo': False,
 'slowmode_enabled': False,
 'call_active': False,
 'call_not_empty': False,
 'fake': False,
 'gigagroup': False,
 'noforwards': False,
 'join_to_send': False,
 'join_request': False,
 'forum': False,
 'access_hash': *********,
 'username': '********',
 'restriction_reason': [],
 'admin_rights': None,
 'banned_rights': None,
 'default_banned_rights': {'_': 'ChatBannedRights',
  'until_date': datetime.datetime(2038, 1, 19, 3, 14, 7, tzinfo=datetime.timezone.utc),
  'view_messages': False,
  'send_messages': False,
  'send_media': False,
  'send_stickers': False,
  'send_gifs': False,
  'send_games': False,
  'send_inline': False,
  'embed_links': False,
  'send_polls': False,
  'change_info': True,
  'invite_users': False,
  'pin_messages': True,
  'manage_topics': False,
  'send_photos': False,
  'send_videos': False,
  'send_roundvideos': False,
  'send_audios': False,
  'send_voices': False,
  'send_docs': False,
  'send_plain': False},
 'participants_count': 3,
 'usernames': []}
AveZorgen commented 4 months ago

I believe we are allowed to view the participants of megagroup channels, but I haven't been able to find any official information confirming this.

fukemy commented 2 months ago

@AveZorgen, dont think so, because I openned Telegram App and join any channel, but can not see other user of this channel. It's seem only admin can see