Ban on incoming messages, bump python-telegram-bot

This commit is contained in:
Michał Rudowicz 2024-06-11 21:52:37 +02:00
parent b593ca20d2
commit 59fefdb100
3 changed files with 21 additions and 55 deletions

View File

@ -1,7 +1,7 @@
# AutoBanBot # AutoBanBot
Automatically kicks and bans users, which first name or last name matches one of predefined regexes, If message matches configured regexes then bot bans the user from the channel and removes the
from a telegram group. message.
## You're not a spammer but you got banned? ## You're not a spammer but you got banned?

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from telegram.ext import Updater, MessageHandler from telegram import Update
from telegram.ext.filters import Filters from telegram.ext import Application, MessageHandler, filters
import re import re
import logging import logging
import json import json
@ -16,56 +16,25 @@ def load_config():
retval["regexes"].append(re.compile(regex, re.I)) retval["regexes"].append(re.compile(regex, re.I))
return retval return retval
def handle_spam_message(bot, chat, member, message_id): async def new_msg(update, context, regexes):
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 is_spam = False
for regex in regexes: for regex in regexes:
if user.first_name is not None and regex.search(user.first_name) is not None: if update.message.text is not None and regex.search(update.message.text) is not None:
is_spam = True 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 break
logging.info("User joined: {} | {} | {} | {} | {}".format(
user.id,
user.first_name,
user.last_name,
user.username,
is_spam
))
if is_spam: if is_spam:
handle_spam_message(bot, update.effective_chat, user, update.message.message_id) logger.info(f"Banning: {update.message}")
return True update.message.chat.ban_member(update.message.from_user.id)
return False update.message.delete()
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s|%(levelname)s|%(message)s', level=logging.INFO) logging.basicConfig(format='%(asctime)s|%(levelname)s|%(message)s', level=logging.INFO)
logging.info("Starting") logging.info("Starting")
config = load_config() config = load_config()
updater = Updater(config["telegramApiKey"]) application = Application.builder().token(config["telegramApiKey"]).build()
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, ( application.add_handler(MessageHandler(filters.ALL, (
lambda bot, update: hello(bot, update, config["regexes"]) lambda update, context: new_msg(update, context, config['regexes'])
))) )))
updater.start_polling() application.run_polling(allowed_updates=Update.ALL_TYPES)
updater.idle()

View File

@ -1,4 +1 @@
certifi==2018.4.16 python-telegram-bot==21.3
future==0.16.0
pkg-resources==0.0.0
python-telegram-bot==10.1.0