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)

Upvotes: 2

Views: 2991

Answers (3)

wowandy
wowandy

Reputation: 1312

In aiogram version 3.x you can use get_chat_member_count method like this:

from aiogram import Router, F, Bot
from aiogram.filters import StateFilter
from aiogram.types import Message

from bot.states import States

some_router = Router()

@some_router.message(F.text, StateFilter(States.some_state))
async def some_message(message: Message, bot: Bot):
    chat_id = "@somechat"
    count_of_members = await bot.get_chat_member_count(chat_id=chat_id)
    # ...

Upvotes: 2

Joxon Xaker
Joxon Xaker

Reputation: 1

The get_chat_members_count() method should pass the bot's chat id to the chat_id parameter

Upvotes: -1

blackline
blackline

Reputation: 64

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

Upvotes: 4

Related Questions