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!
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:
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.