#!/usr/bin/env python from telegram import Update from telegram.ext import Application, MessageHandler, filters import re import logging import json def load_config(): retval = {} with open("config.json") as config_file: config = json.load(config_file) retval["telegramApiKey"] = config["telegramApiKey"] retval["regexes"] = [] for regex in config["regexes"]: retval["regexes"].append(re.compile(regex, re.I)) return retval async def new_msg(update, context, regexes): is_spam = False for regex in regexes: if update.message.text is not None and regex.search(update.message.text) is not None: is_spam = True break if is_spam: logging.info(f"Banning: {update.message}") await update.message.chat.ban_member(update.message.from_user.id) await update.message.delete() logging.info("Banned.") if __name__ == '__main__': logging.basicConfig(format='%(asctime)s|%(levelname)s|%(message)s', level=logging.INFO) logging.getLogger("httpx").setLevel(logging.WARNING) logging.info("Starting") config = load_config() application = Application.builder().token(config["telegramApiKey"]).build() application.add_handler(MessageHandler(filters.ALL, ( lambda update, context: new_msg(update, context, config['regexes']) ))) application.run_polling(allowed_updates=Update.ALL_TYPES)