Skip to content

Commit

Permalink
Dropping media messages
Browse files Browse the repository at this point in the history
  • Loading branch information
f213 committed Apr 3, 2023
1 parent 7bdb8dd commit a709371
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 4 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import rekognition
import text
from filters import (
ContainsLink, ContainsTelegramContact, ContainsThreeOrMoreEmojies, IsMessageOnBehalfOfChat, with_default_filters)
ContainsLink, ContainsTelegramContact, ContainsThreeOrMoreEmojies, IsMedia, IsMessageOnBehalfOfChat,
with_default_filters)
from helpers import DB_ENABLED, enable_logging, in_production, init_sentry


Expand All @@ -31,7 +32,7 @@ def log_message(message: Message, action: Optional[str] = ''):
user_id=message.from_user.id,
chat_id=message.chat_id,
message_id=message.message_id,
text=message.text,
text=message.text or '',
meta={
'tags': [
*rekognition.get_labels(image_url=get_profile_picture(message)),
Expand Down Expand Up @@ -74,6 +75,7 @@ def delete_messages_that_match(*filters: BaseFilter) -> MessageHandler:
dispatcher.add_handler(delete_messages_that_match(ContainsLink()))
dispatcher.add_handler(delete_messages_that_match(IsMessageOnBehalfOfChat()))
dispatcher.add_handler(delete_messages_that_match(ContainsThreeOrMoreEmojies()))
dispatcher.add_handler(delete_messages_that_match(IsMedia()))

if DB_ENABLED(): # log all not handled messages
from models import create_tables
Expand Down
20 changes: 18 additions & 2 deletions filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import operator
from functools import reduce
from telegram import Message
from telegram.ext import BaseFilter, Filters, MessageFilter
from telegram.ext import BaseFilter, MessageFilter
from urlextract import URLExtract

import text
Expand All @@ -15,7 +15,6 @@ def filter(self, message: Message) -> bool:
def with_default_filters(*filters: BaseFilter) -> BaseFilter:
"""Apply default filters to the given filter classes"""
default_filters = [
Filters.text,
ChatMessageOnly(),
]
return reduce(operator.and_, [*default_filters, *filters]) # МАМА Я УМЕЮ ФУНКЦИОНАЛЬНО ПРОГРАММИРОВАТЬ
Expand All @@ -28,6 +27,9 @@ def filter(self, message: Message) -> bool:

class ContainsTelegramContact(MessageFilter):
def filter(self, message: Message) -> bool:
if message.text is None:
return False # type: ignore

return ' @' in message.text or message.text.startswith('@')


Expand All @@ -36,6 +38,9 @@ def __init__(self) -> None:
self.extractor = URLExtract()

def filter(self, message: Message) -> bool:
if message.text is None:
return False # type: ignore

return len(self.extractor.find_urls(message.text)) >= 1


Expand All @@ -44,6 +49,17 @@ def filter(self, message: Message) -> bool:
return 'three_or_more_emojies' in text.Labels(message.text)()


class IsMedia(MessageFilter):
def filter(self, message: Message) -> bool:
if any([message.document, message.audio, message.voice, message.video_note]):
return True

if len(message.photo) > 0:
return True

return False


__all__ = [
'ChatMessageOnly',
'ContainsLink',
Expand Down
7 changes: 5 additions & 2 deletions text.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from typing import List
from typing import List, Optional

import emoji


class Labels:
def __init__(self, text: str) -> None:
def __init__(self, text: Optional[str]) -> None:
self.text = text

def __call__(self) -> List[str]:
return self.get_emoji_label()

def get_emoji_label(self) -> List[str]:
if self.text is None:
return []

emoji_count = len(emoji.emoji_lis(self.text))

if emoji_count == 0:
Expand Down

0 comments on commit a709371

Please sign in to comment.