Issue with Capturing New Members in aiogram Bot when "Hide Members" is Enabled

I'm working on creating a welcome bot using the aiogram library to greet new members who join a group. However, I'm encountering an issue when the group has enabled the "Hide Members" setting. In this case, new members joining the group don't receive the standard "user joined the group" notification. As a result, the @dp.message_handler(content_types=types.ContentTypes.NEW_CHAT_MEMBERS) method doesn't seem to capture these new members.

Has anyone else faced a similar issue? If so, how did you manage to handle the situation? Is there a way to detect new members joining a group even when the "Hide Members" setting is active?

I would greatly appreciate any insights or suggestions on how to overcome this challenge. Thank you in advance for your help!

Best regards,

aiogram Issue with Capturing New Members in aiogram Bot when "Hide Members" is Enabled

Upvotes: 3

Views: 415

Answers (1)

NanoApe
NanoApe

Reputation: 1

When starting the Bot, include "chat_member" in allowed_updates, and then use app.add_handler(ChatMemberHandler(callback, chat_member_types=0)), and it will work.

Code demo (using python-telegram-bot):

from telegram import Update
from telegram.constants import ChatMemberStatus
from telegram.ext import Application, ChatMemberHandler, ContextTypes


async def member_status_change(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """
    Handle chat member status changes.
    """
    assert update.chat_member
    chat = update.chat_member.chat
    user = update.chat_member.from_user
    old_status = update.chat_member.old_chat_member.status
    new_status = update.chat_member.new_chat_member.status

    # Log the status change
    print(f"[Chat ID: {chat.id}] {chat.title or 'Chat'}: User {user.full_name} ({user.id}) status changed "
          f"from {old_status} to {new_status}")

    # Example: Implement specific actions based on the status change
    if old_status == ChatMemberStatus.LEFT and new_status == ChatMemberStatus.MEMBER:
        print(f"User {user.full_name} has joined the chat.")
    elif new_status == ChatMemberStatus.BANNED:
        print(f"User {user.full_name} has been banned from the chat.")


def main():
    """
    Start the bot.
    """
    # Replace 'your-bot-token' with your actual bot token.
    bot_token = 'your-bot-token'

    # Create the application
    app = Application.builder().token(bot_token).build()

    # Add ChatMemberHandler to monitor and handle chat member status changes
    # chat_member_types=0 means that the handler will only handle chat_member updates
    app.add_handler(ChatMemberHandler(member_status_change, chat_member_types=0))

    # Run the bot with explicit allowed_updates (limiting it to 'chat_member' updates)
    # Remember to add other allowed_updates if you need them
    print("Bot is running...")
    app.run_polling(allowed_updates=['chat_member'])


if __name__ == '__main__':
    main()

Upvotes: 0

Related Questions