diff --git a/app/bot.py b/app/bot.py index 9c24a03..c46d371 100644 --- a/app/bot.py +++ b/app/bot.py @@ -10,7 +10,7 @@ import shelve from . import report from .query_log import logging from .me import get_me -from .config import bot, ADMIN_ID, TOKEN, PYTHONCHATRU +from .config import bot, URL_RX, ALLOWED_WORDS, ADMIN_ID, TOKEN, PYTHONCHATRU from . import reminder # https://core.telegram.org/bots/api Telegram Bot API @@ -42,7 +42,8 @@ def wait_for_readers(action, chat_id, msg_id): def check_spam_list(type_message: types.Message) -> bool: """ Check for mentioning unwanted persons in text. """ - unwanted_phrases = ['me.sv/', 'tg.sv/', 'goo.by/', 'go.sv/', 'intim.video/'] + unwanted_phrases = ['me.sv/', 'tg.sv/', 'goo.by/', 'go.sv/', 'intim.video/', + 'uclck.ru/'] for phrase in unwanted_phrases: if phrase in type_message.text.casefold(): return True @@ -83,16 +84,21 @@ def catch_videos(message: types.Message): chat_stats['Banned'] += 1 +def check_no_allowed(word_list, msg): + for word in word_list: + if word in msg.casefold(): + return False + return True + + def check_delete_list(type_message: types.Message) -> bool: - """ Check for unwanted text in message and delete. """ - unwanted_phrases = ['t.me/'] - for phrase in unwanted_phrases: - if phrase in type_message.text.casefold(): - return True - if type_message.entities: - for entity in type_message.entities: - if entity.url and phrase in entity.url: - return True + """ Check for URLs in message and delete. """ + if URL_RX.search(type_message.text) and check_no_allowed(ALLOWED_WORDS, type_message.text): + return True + if type_message.entities: + for entity in type_message.entities: + if entity.url and check_no_allowed(ALLOWED_WORDS, entity.url): + return True @bot.message_handler(func=check_delete_list) diff --git a/app/config.py b/app/config.py index 2de284b..bbeb7e7 100644 --- a/app/config.py +++ b/app/config.py @@ -1,4 +1,5 @@ import os +import re from telebot import TeleBot from dotenv import load_dotenv @@ -9,4 +10,8 @@ bot = TeleBot(TOKEN) ADMIN_ID = 280887861 # Rykov7 PYTHONCHATRU = -1001338616632 # pythonchatru -TEST_CHAT = -1001622893830 \ No newline at end of file +TEST_CHAT = -1001622893830 + +URL_RX = re.compile(r'\w+\.\w+/\w+') +ALLOWED_WORDS = ['pastebin', 'github', 'google', 'nometa', 'python', + 'stackoverflow', 'habr', 'medium']