diff --git a/README.md b/README.md index 57cef3e..6d523c3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # AutoBanBot -Automatically kicks and bans users, which first name or last name matches one of predefined regexes, -from a telegram group. +If message matches configured regexes then bot bans the user from the channel and removes the +message. ## You're not a spammer but you got banned? diff --git a/autobanbot.py b/autobanbot.py index 1c3fd31..83aaa98 100755 --- a/autobanbot.py +++ b/autobanbot.py @@ -1,7 +1,7 @@ #!/usr/bin/env python -from telegram.ext import Updater, MessageHandler -from telegram.ext.filters import Filters +from telegram import Update +from telegram.ext import Application, MessageHandler, filters import re import logging import json @@ -16,56 +16,25 @@ def load_config(): retval["regexes"].append(re.compile(regex, re.I)) return retval -def handle_spam_message(bot, chat, member, message_id): - logging.warning("SPAM USER JOINED: {} | {} | {} | {}".format( - member.id, - member.first_name, - member.last_name, - member.username - )) - logging.debug("WILL BAN: chat_id: {} | user_id: {} | AND DELETE MSG ID = {}".format( - chat.id, - member.id, - message_id - )) - kick_result = bot.kick_chat_member(chat.id, member.id) - if not kick_result: - logging.error("Bankick success: {}".format(kick_result)) - delete_result = bot.delete_message(chat.id, message_id) - if not delete_result: - logging.error("Message delete success: {}".format(delete_result)) - -def hello(bot, update, regexes): - for user in update.message.new_chat_members: - is_spam = False - for regex in regexes: - if user.first_name is not None and regex.search(user.first_name) is not None: - is_spam = True - if user.last_name is not None and regex.search(user.last_name) is not None: - is_spam = True - if is_spam == True: - break - logging.info("User joined: {} | {} | {} | {} | {}".format( - user.id, - user.first_name, - user.last_name, - user.username, - is_spam - )) - if is_spam: - handle_spam_message(bot, update.effective_chat, user, update.message.message_id) - return True - return False +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: + logger.info(f"Banning: {update.message}") + update.message.chat.ban_member(update.message.from_user.id) + update.message.delete() if __name__ == '__main__': logging.basicConfig(format='%(asctime)s|%(levelname)s|%(message)s', level=logging.INFO) logging.info("Starting") config = load_config() - updater = Updater(config["telegramApiKey"]) - - updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, ( - lambda bot, update: hello(bot, update, config["regexes"]) + application = Application.builder().token(config["telegramApiKey"]).build() + + application.add_handler(MessageHandler(filters.ALL, ( + lambda update, context: new_msg(update, context, config['regexes']) ))) - - updater.start_polling() - updater.idle() + + application.run_polling(allowed_updates=Update.ALL_TYPES) diff --git a/requirements.txt b/requirements.txt index a5b7dc5..5f65916 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1 @@ -certifi==2018.4.16 -future==0.16.0 -pkg-resources==0.0.0 -python-telegram-bot==10.1.0 +python-telegram-bot==21.3