'How to solve the error 'coroutine' object has no attribute 'users'?

I want to Retrieving all chat members using Telethon.

Here is my code:

from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from time import sleep
api_id = 134565
api_hash = 'xxxxxx'
client = TelegramClient(None, api_id, api_hash)
 
client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input('Enter the code: '))
offset = 0
limit = 100
all_participants = []

while True:
    participants = client(GetParticipantsRequest(
        channel, ChannelParticipantsSearch(''), offset, limit,
        hash=0
    ))
    if not participants.users:
        break
    all_participants.extend(participants.users)
    offset += len(participants.users)
Note

The code rised the below error: I try this Q/A but it doesn't enough information about the error.

<module>
      19         hash=0
      20     ))
 ---> 21     if not participants.users:
      22         break
      23     all_participants.extend(participants.users)
 
 AttributeError: 'coroutine' object has no attribute 'users'

How to solve the issue?



Solution 1:[1]

Coroutine object in python was used for async program, for details can read Coroutines and Tasks.

In your code, raise an error 'coroutine' object has no attribute 'users', maybe caused by you calling an async API but not await it.

You can see an example in office site, the example used async/await enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WENJUN CHI