|
1 | 1 | from functools import wraps |
2 | 2 | from typing import Optional |
3 | | - |
4 | 3 | from tg_bot.modules.helper_funcs.misc import is_module_loaded |
5 | 4 |
|
6 | 5 | FILENAME = __name__.rsplit(".", 1)[-1] |
7 | 6 |
|
8 | 7 | if is_module_loaded(FILENAME): |
9 | 8 | from telegram import Bot, Update, ParseMode, Message, Chat |
10 | 9 | from telegram.error import BadRequest, Unauthorized |
11 | | - from telegram.ext import CommandHandler, run_async |
| 10 | + from telegram.ext import CommandHandler, MessageHandler, run_async |
12 | 11 | from telegram.utils.helpers import escape_markdown |
| 12 | + from telegram.ext import Filters |
13 | 13 |
|
14 | 14 | from tg_bot import dispatcher, LOGGER |
15 | 15 | from tg_bot.modules.helper_funcs.chat_status import user_admin |
@@ -39,6 +39,28 @@ def log_action(bot: Bot, update: Update, *args, **kwargs): |
39 | 39 |
|
40 | 40 | return log_action |
41 | 41 |
|
| 42 | + @run_async |
| 43 | + def log_resource(bot: Bot, update: Update): |
| 44 | + entities = update.effective_message.parse_entities() |
| 45 | + caption_entities = update.effective_message.parse_caption_entities() |
| 46 | + chat = update.effective_chat # type: Optional[Chat] |
| 47 | + if chat.type == chat.SUPERGROUP: |
| 48 | + log_chat = sql.get_chat_log_channel(chat.id) |
| 49 | + if log_chat: |
| 50 | + for descriptor, entity in entities.items(): |
| 51 | + result = f'<b>Risorsa inviata da @{update.effective_user.username}:</b>\n' |
| 52 | + if descriptor['type'] in ['url', 'text_link']: |
| 53 | + result += f'{entity}' |
| 54 | + LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}") |
| 55 | + send_log(bot, log_chat, chat.id, result) |
| 56 | + |
| 57 | + for descriptor, entity in caption_entities.items(): |
| 58 | + result = '<b>Risorsa inviata da @{update.effective_user.username}:</b>\n' |
| 59 | + if descriptor['type'] in ['url', 'text_link']: |
| 60 | + result += f'{entity}' |
| 61 | + LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}") |
| 62 | + send_log(bot, log_chat, chat.id, result) |
| 63 | + |
42 | 64 |
|
43 | 65 | def send_log(bot: Bot, log_chat_id: str, orig_chat_id: str, result: str): |
44 | 66 | try: |
@@ -160,9 +182,12 @@ def __chat_settings__(chat_id, user_id): |
160 | 182 | SET_LOG_HANDLER = CommandHandler("setlog", setlog) |
161 | 183 | UNSET_LOG_HANDLER = CommandHandler("unsetlog", unsetlog) |
162 | 184 |
|
| 185 | + LOG_RESOURCES_HANDLER = MessageHandler(Filters.all & Filters.group, log_resource) |
| 186 | + |
163 | 187 | dispatcher.add_handler(LOG_HANDLER) |
164 | 188 | dispatcher.add_handler(SET_LOG_HANDLER) |
165 | 189 | dispatcher.add_handler(UNSET_LOG_HANDLER) |
| 190 | + dispatcher.add_handler(LOG_RESOURCES_HANDLER) |
166 | 191 |
|
167 | 192 | else: |
168 | 193 | # run anyway if module not loaded |
|
0 commit comments