我正在编写一个机器人,但我找不到如何获取群组中成员数量的明确答案,当用户在机器人中写入
/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)
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)
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)
# ...
get_chat_members_count()方法应将机器人的聊天ID传递给chat_id参数