pythontelegram-botaiogram

I don't know how to get_chat_members_count with aiogram


I am writing a bot and I can't find a clear answer to how to get number of members in the group and when a user writes /count in the bot, he would get the number of participants in the group.

tell me pls what's wrong. Here is the code I made:

from aiogram import Bot
from aiogram import types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor

from config import token

bot = Bot(token=token)
dp = Dispatcher(bot)

@dp.message_handler(commands=['count'])
async def getCountMembers(message: types.Message):
    await bot.get_chat_members_count(-1001519650013)


executor.start_polling(dp, skip_updates=True)

Solution

  • aiogram has great documentation, you need to specify the chat_id

    from aiogram import Bot
    from aiogram import types
    from aiogram.dispatcher import Dispatcher
    from aiogram.utils import executor
    
    from config import token
    
    bot = Bot(token=token)
    dp = Dispatcher(bot)
    
    @dp.message_handler(commands=['count'])
    async def getCountMembers(message: types.Message):
        # you can get the chat id like so
        chat_id = message.chat.id
    
        # otherwise instead of getting the chat id from the incoming msg just do 
        # chat_id = -1001519650013
    
        await bot.get_chat_members_count(chat_id=chat_id)
    
    
    executor.start_polling(dp, skip_updates=True)
    

    aiogram documentation