auto-ban-bot/autobanbot.py

43 lines
1.5 KiB
Python
Raw Normal View History

2018-07-24 13:31:04 +00:00
#!/usr/bin/env python
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
def load_config():
retval = {}
2018-07-24 13:31:04 +00:00
with open("config.json") as config_file:
config = json.load(config_file)
retval["telegramApiKey"] = config["telegramApiKey"]
retval["regexes"] = []
2018-07-24 13:31:04 +00:00
for regex in config["regexes"]:
retval["regexes"].append(re.compile(regex, re.I))
return retval
2018-07-24 13:31:04 +00:00
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:
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
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()
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)