|
1 | 1 | from tg_bot import dispatcher, LOGGER |
| 2 | +from tg_bot.modules.helper_funcs.chat_status import bot_can_delete |
2 | 3 | from telegram import Update, Bot |
3 | | -from telegram.ext import MessageHandler, Filters |
| 4 | +from telegram.ext import MessageHandler, Filters, CommandHandler |
| 5 | +from telegram.ext.dispatcher import run_async |
4 | 6 |
|
5 | 7 |
|
| 8 | +FORBIDDEN_ENTITY_TYPES = ['url', 'text_link', 'email', 'phone_number'] |
| 9 | + |
| 10 | +@run_async |
| 11 | +@bot_can_delete |
6 | 12 | def spam_filter(bot: Bot, update: Update): |
7 | 13 | msg = update.effective_message.text |
8 | | - LOGGER.debug("spam" in msg) |
9 | | - if msg and "spam" in msg: |
10 | | - user = update.effective_user.username |
11 | | - update.effective_message.reply_text(f"Non spammare! @{user}", quote=False) |
12 | | - update.effective_message.delete() |
| 14 | + message_entities = update.effective_message.parse_entities() |
| 15 | + message_caption_entities = update.effective_message.parse_caption_entities() |
13 | 16 |
|
| 17 | + found = False |
| 18 | + for descriptor, entity in message_entities.items(): |
| 19 | + LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}") |
| 20 | + found = True |
14 | 21 |
|
15 | | -SPAM_HANDLER = MessageHandler(Filters.all & Filters.group, spam_filter) |
| 22 | + if found: |
| 23 | + spam_action(update) |
| 24 | + |
| 25 | + |
| 26 | +def spam_action(update: Update): |
| 27 | + user = update.effective_user.username |
| 28 | + update.effective_message.reply_text(f"Non spammare! @{user}", quote=False) |
| 29 | + update.effective_message.delete() |
16 | 30 |
|
17 | | -dispatcher.add_handler(SPAM_HANDLER) |
| 31 | + |
| 32 | +def white_spam_add(bot: Bot, update: Update): |
| 33 | + if update.effective_message.reply_to_message: |
| 34 | + pass |
| 35 | + else: |
| 36 | + update.effective_message.reply_text("Cosa vuoi aggiungere in whitelist?") |
| 37 | + |
| 38 | +SPAM_HANDLER = MessageHandler(Filters.all & Filters.group, spam_filter) |
| 39 | +WHITE_SPAM_HANDLER = CommandHandler("whitespam", white_spam_add) |
| 40 | +#dispatcher.add_handler(SPAM_HANDLER) |
0 commit comments