我不知道如何使用aiogram获取聊天成员计数

问题描述 投票:0回答:3

我正在编写一个机器人,但我找不到如何获取群组中成员数量的明确答案,当用户在机器人中写入

/count
时,他会获得群组中的参与者数量。

请告诉我出了什么问题。这是我编写的代码:

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)
python telegram-bot aiogram
3个回答
3
投票

aiogram 有很棒的文档,你需要指定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 文档


2
投票

aiogram 版本 3.x 中,您可以使用

get_chat_member_count
方法,如下所示:

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)
    # ...

-1
投票

get_chat_members_count()方法应将机器人的聊天ID传递给chat_id参数

© www.soinside.com 2019 - 2024. All rights reserved.