Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

TypeError: 'ChannelParticipants' object is not subscriptable

Open JawadPy opened this issue 2 years ago • 17 comments

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 avatar Mar 28 '22 08:03 JawadPy

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()

JawadPy avatar Mar 28 '22 19:03 JawadPy

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.

ljhOfGithub avatar Apr 16 '22 06:04 ljhOfGithub

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.

JawadPy avatar Apr 16 '22 15:04 JawadPy

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

vijaypareek13 avatar Apr 21 '22 14:04 vijaypareek13

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

JawadPy avatar Apr 23 '22 04:04 JawadPy

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

bsde1234 avatar May 04 '22 05:05 bsde1234

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.

edogab33 avatar May 05 '22 14:05 edogab33

Just setting limit while extracting solve this problem.

vijaypareek13 avatar May 16 '22 17:05 vijaypareek13

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 avatar Jun 07 '22 19:06 JawadPy

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

hope it's helpful.

JawadPy avatar Jun 07 '22 21:06 JawadPy

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

CJ-ONYERO99 avatar Jun 12 '22 16:06 CJ-ONYERO99

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

abc-1211 avatar Jun 13 '22 20:06 abc-1211

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.

JawadPy avatar Jun 14 '22 16:06 JawadPy

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?

abc-1211 avatar Jun 15 '22 11:06 abc-1211

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?

JawadPy avatar Jun 15 '22 14:06 JawadPy

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

hope it's helpful.

Thanks, That worked

sajalkmr avatar Jul 08 '22 18:07 sajalkmr

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.

luyifan233 avatar Aug 07 '22 13:08 luyifan233

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

coolroman avatar Aug 24 '22 16:08 coolroman

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

luyifan233 avatar Aug 25 '22 12:08 luyifan233

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.

coolroman avatar Aug 25 '22 13:08 coolroman

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

luyifan233 avatar Aug 25 '22 13:08 luyifan233

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

Jayiitb avatar Aug 31 '22 07:08 Jayiitb

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

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

luyifan233 avatar Aug 31 '22 07:08 luyifan233

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

abc-1211 avatar Sep 25 '22 14:09 abc-1211