Aiogram: How to automatically detect when bot joins a new group and capture the group ID?

I’m working on a bot that forwards photos from private messages to all groups where it has admin rights. Right now I have to manually add group IDs to my list, but I want to automate this process.

my_bot = Bot(token=BOT_TOKEN)
dispatcher = Dispatcher()
group_list = []

async def verify_admin_status(group_id):
    chat_member = await my_bot.get_chat_member(group_id, my_bot.id)
    status = chat_member.status
    return status

@dispatcher.message(F.photo)
async def process_image(msg: types.Message):
    for target_group in group_list:
        admin_check = await verify_admin_status(target_group)
        if msg.chat.type == 'private' and admin_check:
            await msg.copy_to(chat_id=target_group)

async def start_bot():
    await dispatcher.start_polling(my_bot)

The bot works fine for forwarding images, but I need to populate the group_list manually. Is there a way to automatically catch when someone adds my bot to a group and store that group’s ID? I want the bot to automatically know about all groups it belongs to without manual configuration.

You need to handle ChatMemberUpdated events to track when your bot joins new groups. The previous answer got the handler right, but here’s a more complete approach:

@dispatcher.chat_member()
async def track_bot_membership(update: types.ChatMemberUpdated):
    if update.new_chat_member.user.id == my_bot.id:
        if update.new_chat_member.status in ['member', 'administrator']:
            group_id = update.chat.id
            if group_id not in group_list:
                group_list.append(group_id)
                print(f"Bot added to group: {group_id}")
        elif update.new_chat_member.status == 'left':
            if update.chat.id in group_list:
                group_list.remove(update.chat.id)

This handles both joining and leaving groups automatically. Just make sure you save the group_list to a file or database so it survives bot restarts - otherwise you’ll lose all the group IDs every time you restart.

use the my_chat_member handler to catch when ur bot gets added to groups. add @dispatcher.my_chat_member() and check if update.new_chat_member.status is member or admin. if it is, save the chat id to ur list automatically.

Both answers are right about chat member handlers, but here’s a gotcha that bit me hard. When your bot starts up, it won’t know about groups it’s already in - these handlers only catch new events, not existing memberships. You’ll need a startup check that queries your bot’s existing group memberships using get_updates with a filter, or just maintain persistent storage from day one. Also, some groups restrict member addition notifications, so the handler might not fire every time. I’d combine the automatic detection with a manual fallback command that admins can use to register their groups when the automatic method fails.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.