Skip to content

Commit 61213cc

Browse files
committed
Added the logging of the link resources in log_channel module
1 parent ec49d4f commit 61213cc

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

tg_bot/deactivated-modules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
NO_LOAD = ['translation','rss', 'sed', 'afk','notes','filters','Locks','Log Channels']
1+
NO_LOAD = ['translation','rss', 'sed', 'afk','notes','filters']

tg_bot/modules/log_channel.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from functools import wraps
22
from typing import Optional
3-
43
from tg_bot.modules.helper_funcs.misc import is_module_loaded
54

65
FILENAME = __name__.rsplit(".", 1)[-1]
76

87
if is_module_loaded(FILENAME):
98
from telegram import Bot, Update, ParseMode, Message, Chat
109
from telegram.error import BadRequest, Unauthorized
11-
from telegram.ext import CommandHandler, run_async
10+
from telegram.ext import CommandHandler, MessageHandler, run_async
1211
from telegram.utils.helpers import escape_markdown
12+
from telegram.ext import Filters
1313

1414
from tg_bot import dispatcher, LOGGER
1515
from tg_bot.modules.helper_funcs.chat_status import user_admin
@@ -39,6 +39,28 @@ def log_action(bot: Bot, update: Update, *args, **kwargs):
3939

4040
return log_action
4141

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+
4264

4365
def send_log(bot: Bot, log_chat_id: str, orig_chat_id: str, result: str):
4466
try:
@@ -160,9 +182,12 @@ def __chat_settings__(chat_id, user_id):
160182
SET_LOG_HANDLER = CommandHandler("setlog", setlog)
161183
UNSET_LOG_HANDLER = CommandHandler("unsetlog", unsetlog)
162184

185+
LOG_RESOURCES_HANDLER = MessageHandler(Filters.all & Filters.group, log_resource)
186+
163187
dispatcher.add_handler(LOG_HANDLER)
164188
dispatcher.add_handler(SET_LOG_HANDLER)
165189
dispatcher.add_handler(UNSET_LOG_HANDLER)
190+
dispatcher.add_handler(LOG_RESOURCES_HANDLER)
166191

167192
else:
168193
# run anyway if module not loaded

0 commit comments

Comments
 (0)