S
S
Sergo Zar2021-08-25 17:04:01
Python
Sergo Zar, 2021-08-25 17:04:01

TypeError: 'async_generator' object is not iterable?

I'm writing a bot on aiogram and I need to get all the chat participants. Since aiogram cannot get the list of participants, I need to get the list of participants through pyrogram and then pass it to the bot.
my code is
bot.py

bot.ru

from aiogram import Bot, Dispatcher, executor, types
import юзербот

бот = Bot(token=config.TOKEN)
диспетчер = Dispatcher(бот)
# код...
@диспетчер.message_handler(commands=['додати'], commands_prefix='+')
async def додати(message: types.Message):
    текст = message.text
    що = текст.split(" ")[1]
    ід_чату = message.chat.id
    print(текст, що)
    if що == "юзера":
        якого = текст.split(" ")[2]      
        if якого == "цього":
            #код..
        elif якого == "всіх":
            print(await юзербот.учасники_чату(ід_чату))
#код..
if __name__ == '__main__':
    executor.start_polling(диспетчер, skip_updates=True)


userbot.ru

from pyrogram import Client, filters
from pyrogram.errors import FloodWait

app = Client("my_account")

async def учасники_чату(ід):
    await app.start()
    учасники = []
    
    for учасник in app.iter_chat_members(ід):
        учасники.append(учасник.user.id)

    await app.stop()
    return учасники



full text of the error

future: <Task finished name='Task-9' coro=<Dispatcher._process_polling_updates() done, defined at C:\Program Files\Python38\lib\site-packages\aiogram\dispatcher\dispatcher.py:409> exception=TypeError("'async_generator' object is not iterable")>
Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 417, in _process_polling_updates
    for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
  File "C:\Program Files\Python38\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 238, in process_updates
    return await asyncio.gather(*tasks)
  File "C:\Program Files\Python38\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
    response = await handler_obj.handler(*args, **partial_data)
  File "C:\Program Files\Python38\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 259, in process_update
    return await self.message_handlers.notify(update.message)
  File "C:\Program Files\Python38\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
    response = await handler_obj.handler(*args, **partial_data)
  File "бот.py", line 122, in додати
    print(await юзербот.учасники_чату(-1001363517171))
  File "C:\_myprog\_\python\_tg\_smails\_2\хз\юзербот.py", line 14, in учасники_чату
    for учасник in app.iter_chat_members(ід):
TypeError: 'async_generator' object is not iterable

What am I doing wrong?

ps if you run userbot.ru directly (python userbot.ru) and
without async and await

from pyrogram import Client, filters
from pyrogram.errors import FloodWait

app = Client("my_account")

def учасники_чату(ід):
    app.start()
    учасники = []
    
    for учасник in app.iter_chat_members(ід):
        учасники.append(учасник.user.id)
    app.stop()
    return учасники
print(учасники_чату(-1234..))


then it works without problems. and if I get only
pps errors from the bot, maybe there is another way to do it, but I xs

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-25
@Sergomen

The error seems to be quite understandable.
The app.iter_chat_members() method you are calling is asynchronous, and you are iterating its output with a synchronous for loop. Use
async for item in app.iter_chat_members(id):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question