From 269724345d77c95d68172c826c26a2683686a12eefc241804435d428f5cc2bc9 Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 26 Jul 2025 21:58:38 +0200 Subject: [PATCH] show logs inside the telegram bot --- bot.py | 5 +++-- bot_libs/{upstart.py => special_commands.py} | 20 ++++++++++++++++++-- bot_libs/syms.py | 2 ++ 3 files changed, 23 insertions(+), 4 deletions(-) rename bot_libs/{upstart.py => special_commands.py} (61%) diff --git a/bot.py b/bot.py index f59e33f..02bcec6 100644 --- a/bot.py +++ b/bot.py @@ -8,7 +8,7 @@ from telegram import ReplyKeyboardRemove from utils import logs as _log from entities import arena as _arena -from bot_libs import upstart as _bot_upstart +from bot_libs import special_commands as _bot_special_cmd from bot_libs import syms as _botsyms from bot_libs import commands_handling as _bot_commands @@ -63,7 +63,8 @@ async def bot_commands(update, context): if waiting_for_seconds: return await _bot_commands.get_seconds_from_input(context, update, chat_id, text) - if text == 'upstart': return await _bot_upstart.update_bot(update, context) + if text == 'upstart': return await _bot_special_cmd.update_bot(update, context) + if text == 'logs': return await _bot_special_cmd.show_logs(update, context) _log.log_debug(f'bot_command: {chat_id} - sent this text: {text}') await update.message.reply_text(_botsyms.WIP_MSG) diff --git a/bot_libs/upstart.py b/bot_libs/special_commands.py similarity index 61% rename from bot_libs/upstart.py rename to bot_libs/special_commands.py index a804fe2..a51128d 100644 --- a/bot_libs/upstart.py +++ b/bot_libs/special_commands.py @@ -10,19 +10,19 @@ async def _pull_repo(update): return err async def _upstart_service(update): - err= _os.system('/usr/bin/systemctl --user restart battle_royale.service') + err= _os.system(_botsyms.BOT_EXEC_CMD) # this error is fake, probably due to systemd restart that make the bot istance broke #if err: #_log.log_error(f'update_bot: {chat_id} error {err} while trying to upstart the app') #return await update.message.reply_text('Errore durante il riavvio del Bot') async def update_bot(update, context): - await update.message.reply_text('Sto aggiornando il Bot...') chat_id = update.effective_chat.id if update.message.chat.id not in _botsyms.SUPER_USERS: return _log.log_warning(f'update_bot: user {chat_id} not allowed') + await update.message.reply_text('Sto aggiornando il Bot...') _log.log_info(f'update_bot: {chat_id} bot is updating...') err= await _pull_repo(update) @@ -32,3 +32,19 @@ async def update_bot(update, context): _log.log_info(f'update_bot: {chat_id} bot successfully updated') await update.message.reply_text('Bot aggiornato e riavviato!') + +async def show_logs(update, context): + chat_id = update.effective_chat.id + if update.message.chat.id not in _botsyms.SUPER_USERS: + return _log.log_warning(f'open_logs: user {chat_id} not allowed') + + await update.message.reply_text('Sto provando ad aprire i log...') + _log.log_info(f'open_logs: {chat_id} trying opening logs...') + try: + game_log= _os.path.expanduser(f'{_botsyms.BOT_PATH}/battle_royale.log') + with open(game_log, 'r') as _log_file: + lines= _log_file.readlines()[-30:] # last 30 lines + log_content= '\n'.join(lines) + await update.message.reply_text(f"Contenuto del log:\n{log_content}") + except Exception as e: + await update.message.reply_text(f"Errore nella lettura del file di log: {str(e)}") diff --git a/bot_libs/syms.py b/bot_libs/syms.py index feff504..fa69520 100644 --- a/bot_libs/syms.py +++ b/bot_libs/syms.py @@ -1,9 +1,11 @@ from local_settings import TOKEN as _token from local_settings import BOT_PATH as _bot_path +from local_settings import BOT_EXEC_CMD as _bot_exec_cmd from local_settings import SUPER_USERS as _superusers TOKEN= _token BOT_PATH= _bot_path +BOT_EXEC_CMD= _bot_exec_cmd SUPER_USERS= _superusers START_MSG= """Benvenuto nel crudele mondo di Battle Royal Simulator,