How to remove automatic messages when users join or leave a group in PTB?

I’m using Python-Telegram-bot v13.7 and I’m trying to get rid of those system messages that pop up when people join or leave a group. Here’s what I’ve got so far:

if (msg.new_chat_members or msg.left_chat_member or msg.new_chat_title or
    msg.new_chat_photo or msg.delete_chat_photo or msg.group_chat_created or
    msg.supergroup_chat_created or msg.channel_chat_created or msg.migrate_to_chat_id or
    msg.migrate_from_chat_id or msg.pinned_message):
    # delete message code here

The problem is it’s not catching all cases. Like when someone joins through an invite link or gets approved by an admin, the bot doesn’t see it as a join message.

I tried using Message.text to look for specific words, but it turns out these aren’t regular text messages, so that didn’t work either.

Any ideas on how to catch all these join/leave messages? I’m pretty stuck here and could use some help!

hey lucas, have u tried using filters? something like this might work:

from telegram.ext import Filters

def remove_service_messages(update, context):
    if update.message.chat.type != 'private':
        update.message.delete()

dp.add_handler(MessageHandler(Filters.status_update, remove_service_messages))

this should catch most service msgs. lmk if it helps!

I’ve encountered this issue before, and there’s a more comprehensive approach you can try. Instead of relying solely on message attributes, you can leverage the ChatMemberUpdated event. This event is triggered for all member status changes, including those that might slip through traditional filters.

Here’s a snippet that might help:

from telegram.ext import ChatMemberHandler

def handle_member_update(update, context):
    if update.chat_member.new_chat_member:
        update.effective_message.delete()

dispatcher.add_handler(ChatMemberHandler(handle_member_update, ChatMemberHandler.CHAT_MEMBER))

This should catch most, if not all, join/leave scenarios. It’s been quite effective in my experience, though you might need to adjust based on your specific use case.

I’ve been working with PTB for a while now and found that combining different approaches is usually most effective. For example, I started using ChatMemberHandler as ZoeStar42 suggested because it reliably catches most join and leave events. In addition, I implemented a custom filter to handle any remaining service messages. Here is my implementation:

def service_message_filter(message):
    return any([getattr(message, attr) for attr in [
        'new_chat_members', 'left_chat_member', 'new_chat_title',
        'new_chat_photo', 'delete_chat_photo', 'group_chat_created',
        'supergroup_chat_created', 'channel_chat_created',
        'migrate_to_chat_id', 'migrate_from_chat_id', 'pinned_message'
    ]])

dp.add_handler(MessageHandler(Filters.create(service_message_filter), delete_message))

This approach has worked well for me, capturing virtually all service messages. Just be sure to include proper error handling in case you lack permissions to delete some messages.