From b69656b33836dd8eb53950fc66019d85e60a297d Mon Sep 17 00:00:00 2001 From: Fedor Borshev Date: Tue, 27 Aug 2024 21:15:40 +0300 Subject: [PATCH] Deleting messages on behalf of chats --- bot.py | 2 +- filters.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index af5c214..f19451c 100644 --- a/bot.py +++ b/bot.py @@ -93,9 +93,9 @@ def delete_messages_that_match(*filters: BaseFilter) -> MessageHandler: bot.add_handler(CommandHandler('start', introduce_myself)) bot.add_handler(CommandHandler('ping', ping)) + bot.add_handler(delete_messages_that_match(IsMessageOnBehalfOfChat())) bot.add_handler(delete_messages_that_match(ContainsTelegramContact())) bot.add_handler(delete_messages_that_match(ContainsLink())) - bot.add_handler(delete_messages_that_match(IsMessageOnBehalfOfChat())) bot.add_handler(delete_messages_that_match(ContainsThreeOrMoreEmojies())) bot.add_handler(delete_messages_that_match(IsMedia())) diff --git a/filters.py b/filters.py index 81ec5c9..6a9b44d 100644 --- a/filters.py +++ b/filters.py @@ -31,10 +31,19 @@ def filter(self, message: Message) -> bool: return message.forward_from_message_id is None +class NotRootChatMessage(MessageFilter): + def filter(self, message: Message) -> bool: + if message.sender_chat is None: + return True + + return message.sender_chat.id != message.chat.id + + def with_default_filters(*filters: BaseFilter) -> BaseFilter: """Apply default filters to the given filter classes""" default_filters = [ ChatMessageOnly(), + NotRootChatMessage(), HasNoValidPreviousMessages(), ] return reduce(operator.and_, [*default_filters, *filters]) # МАМА Я УМЕЮ ФУНКЦИОНАЛЬНО ПРОГРАММИРОВАТЬ @@ -54,7 +63,6 @@ def filter(self, message: Message) -> bool: class ContainsLink(MessageFilter): - def filter(self, message: Message) -> bool: if message.text is None: return False