2018-07-24 13:31:04 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2024-06-11 19:52:37 +00:00
|
|
|
from telegram import Update
|
|
|
|
from telegram.ext import Application, MessageHandler, filters
|
2018-07-24 13:31:04 +00:00
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
|
2024-06-15 04:47:55 +00:00
|
|
|
def load_config(filename: str = "config.json"):
|
2018-07-25 10:03:11 +00:00
|
|
|
retval = {}
|
2024-06-15 04:47:55 +00:00
|
|
|
with open(filename) as config_file:
|
2018-07-24 13:31:04 +00:00
|
|
|
config = json.load(config_file)
|
2018-07-25 10:03:11 +00:00
|
|
|
retval["telegramApiKey"] = config["telegramApiKey"]
|
2024-06-15 08:03:15 +00:00
|
|
|
retval["allowedChats"] = config["allowedChats"]
|
2024-06-15 04:47:55 +00:00
|
|
|
retval["regexes"] = list(map(lambda r: re.compile(r, re.I), config["regexes"]))
|
2018-07-25 10:03:11 +00:00
|
|
|
return retval
|
2018-07-24 13:31:04 +00:00
|
|
|
|
2024-06-15 08:03:15 +00:00
|
|
|
async def new_msg(update, context, regexes, allowed_chats):
|
|
|
|
if update.message is None:
|
|
|
|
logging.info(f"Got following update: {update}")
|
|
|
|
return
|
|
|
|
if update.message.chat.id not in allowed_chats:
|
|
|
|
logging.info(f"Got update from not allowed chat: {update.message.chat}. Ignoring")
|
|
|
|
return
|
2024-06-11 19:52:37 +00:00
|
|
|
is_spam = False
|
|
|
|
for regex in regexes:
|
2024-06-15 04:47:55 +00:00
|
|
|
if update.message is not None and \
|
|
|
|
update.message.text is not None and \
|
|
|
|
regex.search(update.message.text) is not None:
|
2024-06-11 19:52:37 +00:00
|
|
|
is_spam = True
|
|
|
|
break
|
|
|
|
if is_spam:
|
2024-06-13 14:45:41 +00:00
|
|
|
logging.info(f"Banning {update.message.from_user.name} from {update.message.chat.effective_name}")
|
2024-06-12 21:14:15 +00:00
|
|
|
await update.message.chat.ban_member(update.message.from_user.id)
|
|
|
|
await update.message.delete()
|
|
|
|
logging.info("Banned.")
|
2018-07-24 13:31:04 +00:00
|
|
|
|
2024-06-15 04:47:55 +00:00
|
|
|
async def handle_error(update, context):
|
|
|
|
logging.error("Exception while handling an update:", exc_info=context.error)
|
|
|
|
|
2018-07-24 13:31:04 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(format='%(asctime)s|%(levelname)s|%(message)s', level=logging.INFO)
|
2024-06-12 04:54:59 +00:00
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
2018-07-24 13:31:04 +00:00
|
|
|
logging.info("Starting")
|
|
|
|
config = load_config()
|
2024-06-11 19:52:37 +00:00
|
|
|
application = Application.builder().token(config["telegramApiKey"]).build()
|
|
|
|
|
|
|
|
application.add_handler(MessageHandler(filters.ALL, (
|
2024-06-15 08:03:15 +00:00
|
|
|
lambda update, context: new_msg(update, context, config['regexes'], config['allowedChats'])
|
2018-07-25 10:03:11 +00:00
|
|
|
)))
|
2024-06-15 04:47:55 +00:00
|
|
|
application.add_error_handler(handle_error, block=False)
|
2024-06-11 19:52:37 +00:00
|
|
|
|
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|