LonamiWebs / Telethon

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

TypeError: 'ChannelParticipants' object is not subscriptable #3787

Closed JawadPy closed 2 years ago

JawadPy commented 2 years ago

Code that causes the issue

    i = 0
    async for user in client.iter_participants(dialog):
        if not user.bot:
            DATA.update(
                {
                    i:{
                        'first_name': user.first_name,
                        'last_name': user.last_name,
                        'username': user.username,
                        'phone': user.phone,
                        'access_hash': user.access_hash,
                        'id': user.id
                    }
                }
            )
            i +=1

Traceback

Traceback (most recent call last):
  File ".\main.py", line 112, in <module>
    client.loop.run_until_complete(main())
  File "D:\python\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File ".\main.py", line 54, in main
    async for user in client.iter_participants(dialog):
  File "D:\python\lib\site-packages\telethon\requestiter.py", line 74, in __anext__
    if await self._load_next_chunk():
  File "D:\python\lib\site-packages\telethon\client\chats.py", line 224, in _load_next_chunk
    participants = results[i]
TypeError: 'ChannelParticipants' object is not subscriptable

As same as 928, @Lonami referenced a solution in comment section which was The issue is fixed in the development version.

The issue appears to be persisting from 2018 until today, and development version seems not useful in this case. any solutions or replacements?

JawadPy commented 2 years ago

Update: This error happens with some "megagroup"s not everytime! Strange!!

However this group results the error on my both OS (windows, linux):

https://t.me/for_saudi_jobs

Idk how useful this information, but it might help, also i changed my code to:

flood = 1
    while i < GROUP_TO_ADD_FROM.entity.participants_count:
        try:
            if not MEMBER[i].bot:
                print(f'[ADD] {i} - {MEMBER[i].first_name}...')
                await client(InviteToChannelRequest(GROUP_TO_ADD_IN, [InputPeerUser(MEMBER[i].id, MEMBER[i].access_hash)]))

        except PeerFloodError:
            print("[Error] Flood Error")
            whu.add_embed(DiscordEmbed(title=f'issue', description=f'||@everyone||\n{acc_data[19]}, {acc_data[20]} \nPeerFloodError', color='03b2f8'))
            whu.execute()
            flood +=1
            slpSystem(flood * 30)
        except UserPrivacyRestrictedError:
            print("[Error] User Privacy Settings.")
        except UserAlreadyInvitedError:
            print("[Error] User Already In.")
        except UserNotMutualContactError:
            print("[Error] User Privacy Settings.")
        except:
            print(f"[Error] ...\n{traceback.print_exc()}")
            whu.add_embed(DiscordEmbed(title=f'issue', description=f'||@everyone||\n{acc_data[19]}, {acc_data[20]} \n{traceback.print_exc()}', color='03b2f8'))
            whu.execute()
ljhOfGithub commented 2 years ago

I meet the same prpblem in repo https://github.com/edogab33/telegram-groups-crawler,its code is:

try:
            if group.username != None and group.broadcast != True:
                async for m in client.iter_participants(dialog.id):
                    members.append(m.to_dict())
            if group.username != None:
                # change limit according to how many messages have to be saved
                async for m in client.iter_messages(dialog.id, limit=3000):
                    messages.append(m.message)
        except telethon.errors.rpcerrorlist.ChannelPrivateError as e:
            print(" ---[✘] Data collection failed: "+str(e)) 

error is repoted:

  File "scraper.py", line 370, in <module>
    client.loop.run_until_complete(main())
  File "/root/.miniconda3/envs/python38/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "scraper.py", line 41, in main
    await init_empty()
  File "scraper.py", line 197, in init_empty
    groups.append(await collect_data(dialog, ""))
  File "scraper.py", line 253, in collect_data
    async for m in client.iter_participants(dialog.id):
  File "/root/.miniconda3/envs/python38/lib/python3.8/site-packages/telethon/requestiter.py", line 74, in __anext__
    if await self._load_next_chunk():
  File "/root/.miniconda3/envs/python38/lib/python3.8/site-packages/telethon/client/chats.py", line 224, in _load_next_chunk
    participants = results[i]
TypeError: 'ChannelParticipants' object is not subscriptable

Can change the version of telethon work?Thank you.

JawadPy commented 2 years ago

I meet the same prpblem in repo https://github.com/edogab33/telegram-groups-crawler,its code is:

try:
          if group.username != None and group.broadcast != True:
              async for m in client.iter_participants(dialog.id):
                  members.append(m.to_dict())
          if group.username != None:
              # change limit according to how many messages have to be saved
              async for m in client.iter_messages(dialog.id, limit=3000):
                  messages.append(m.message)
      except telethon.errors.rpcerrorlist.ChannelPrivateError as e:
          print(" ---[✘] Data collection failed: "+str(e)) 

error is repoted:

  File "scraper.py", line 370, in <module>
    client.loop.run_until_complete(main())
  File "/root/.miniconda3/envs/python38/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "scraper.py", line 41, in main
    await init_empty()
  File "scraper.py", line 197, in init_empty
    groups.append(await collect_data(dialog, ""))
  File "scraper.py", line 253, in collect_data
    async for m in client.iter_participants(dialog.id):
  File "/root/.miniconda3/envs/python38/lib/python3.8/site-packages/telethon/requestiter.py", line 74, in __anext__
    if await self._load_next_chunk():
  File "/root/.miniconda3/envs/python38/lib/python3.8/site-packages/telethon/client/chats.py", line 224, in _load_next_chunk
    participants = results[i]
TypeError: 'ChannelParticipants' object is not subscriptable```
Can change the version of telethon work?Thank you.

thx for your reply, but man that was long time ago. I found another way around and worked for me, took me so much time to figure it out because of the lack of information on the doc.

the doc is just awful.

vijaypareek13 commented 2 years ago

I am getting same error . Python script is working with some group. But it is giving error with some group. Please reply if anyone have any alternative. I will also share my code in sometime. Thank you awaiting for answer

JawadPy commented 2 years ago

I am getting same error . Python script is working with some group. But it is giving error with some group. Please reply if anyone have any alternative. I will also share my code in sometime. Thank you awaiting for answer

Read. i wish if i can help, but i already forgot how did i solved this issue :D

bsde1234 commented 2 years ago

This error is applicable to some groups only, may be with some special permissions. It happens when setting aggressive=False.

edogab33 commented 2 years ago

This error is applicable to some groups only, may be with some special permissions. It happens when setting aggressive=False.

I believe that the problem is caused by legacy groups (supergroups?) which are not supported by Telegram APIs anymore.

vijaypareek13 commented 2 years ago

Just setting limit while extracting solve this problem.

JawadPy commented 2 years ago

This error is applicable to some groups only, may be with some special permissions. It happens when setting aggressive=False.

I believe that the problem is caused by legacy groups (supergroups?) which are not supported by Telegram APIs anymore.

yes it is supergroups

JawadPy commented 2 years ago

the quickest solution I figured out: await client.get_participants(group, aggressive=False, limit=limit)

hope it's helpful.

CJ-ONYERO99 commented 2 years ago

Please does anyone have the fixed code. I can't seem to find it

abc-1211 commented 2 years ago

the quickest solution I figured out:

await client.get_participants(group, aggressive=False, limit=limit)

hope it's helpful.

What is the value of limit you provides in line 3. I doubt that this code works. As the problem of this issue only happens under some scenarios currently, I believe when groupmembers amount is around 10k, this bug pops out

JawadPy commented 2 years ago

the quickest solution I figured out:

await client.get_participants(group, aggressive=False, limit=limit)

hope it's helpful.

What is the value of limit you provides in line 3. I doubt that this code works. As the problem of this issue only happens under some scenarios currently, I believe when groupmembers amount is around 10k, this bug pops out

Value is 200 or less. But it fixes some cases, sometimes you still get the error.

abc-1211 commented 2 years ago

the quickest solution I figured out:

await client.get_participants(group, aggressive=False, limit=limit)

hope it's helpful.

What is the value of limit you provides in line 3. I doubt that this code works. As the problem of this issue only happens under some scenarios currently, I believe when groupmembers amount is around 10k, this bug pops out

Value is 200 or less. But it fixes some cases, sometimes you still get the error.

Erm... the problem is I am trying to scrap the member for the entire group with around 10k people. If I set a limit to 200 then how can I get all the member's data?

JawadPy commented 2 years ago

Nope, only 200 members, but groups with 10k, 5k it would be a problem. Tell me what you're trying to do exactly so i can help you with another method

the quickest solution I figured out:

await client.get_participants(group, aggressive=False, limit=limit)

hope it's helpful.

What is the value of limit you provides in line 3. I doubt that this code works. As the problem of this issue only happens under some scenarios currently, I believe when groupmembers amount is around 10k, this bug pops out

Value is 200 or less. But it fixes some cases, sometimes you still get the error.

Erm... the problem is I am trying to scrap the member for the entire group with around 10k people. If I set a limit to 200 then how can I get all the member's data?

sajalkmr commented 2 years ago

the quickest solution I figured out: await client.get_participants(group, aggressive=False, limit=limit)

hope it's helpful.

Thanks, That worked

luyifan233 commented 2 years ago

the quickest solution I figured out: await client.get_participants(group, aggressive=False, limit=limit) hope it's helpful.

What is the value of limit you provides in line 3. I doubt that this code works. As the problem of this issue only happens under some scenarios currently, I believe when groupmembers amount is around 10k, this bug pops out

Value is 200 or less. But it fixes some cases, sometimes you still get the error.

Erm... the problem is I am trying to scrap the member for the entire group with around 10k people. If I set a limit to 200 then how can I get all the member's data?

Did you figure out how to scrap the members for the entire group with over 10k members? It seems to be possible since it can be displayed in the official client. But I don't know how to make it.

coolroman commented 2 years ago

As a quick fix for telethon v.1.24 I used this code in file site-packages/telethon/client/chats.py starting on line 223:

        results = await self.client(self.requests)
        try:
          len(results)
        except:
          results = await self.client(self.requests)
          pass
luyifan233 commented 2 years ago

As a quick fix for telethon v.1.24 I used this code in file site-packages/telethon/client/chats.py starting on line 223:

        results = await self.client(self.requests)
        try:
          len(results)
        except:
          results = await self.client(self.requests)
          pass

but it still can't get all members in a large group, it just make the code free from error

coolroman commented 2 years ago

but it still can't get all members in a large group, it just make the code free from error

It tries one more time to get members in except handler.

luyifan233 commented 2 years ago

but it still can't get all members in a large group, it just make the code free from error

It tries one more time to get members in except handler.

when participants count over 10k, it won't get all the members

Jayiitb commented 2 years ago

If offset can be implemented. Then, that could solve the problem for everyone.

luyifan233 commented 2 years ago

If offset can be implemented. Then, that could solve the problem for everyone.

actually, I think server has a restriction on the request.

abc-1211 commented 2 years ago

Nope, only 200 members, but groups with 10k, 5k it would be a problem. Tell me what you're trying to do exactly so i can help you with another method

the quickest solution I figured out:

await client.get_participants(group, aggressive=False, limit=limit)

hope it's helpful.

What is the value of limit you provides in line 3. I doubt that this code works. As the problem of this issue only happens under some scenarios currently, I believe when groupmembers amount is around 10k, this bug pops out

Value is 200 or less. But it fixes some cases, sometimes you still get the error.

Erm... the problem is I am trying to scrap the member for the entire group with around 10k people. If I set a limit to 200 then how can I get all the member's data?

I am trying to scrape the group all in once, in order to get all datas of the user. However, some supergroup might have 200k ppl. Is there any suggested method or workaround i can use? Thx