diff --git a/BOTFATHER_COMMANDS.txt b/BOTFATHER_COMMANDS.txt index f34070e..d7d9ed5 100644 --- a/BOTFATHER_COMMANDS.txt +++ b/BOTFATHER_COMMANDS.txt @@ -28,7 +28,7 @@ plot - Show weekly listing patterns (image) errorrate - Show autopilot success vs failure plot (image) retryfailed - Retry all failed applications up to 3 times (excludes deactivated listings) resetlistings - Delete all seen listings (forces re-check of all flats, does not affect stats or WGcompany) -logs - Show last 50 lines from monitor.log +logs - Show last n log lines (default 50). Usage: logs or logs 100 help - Show help and command usage Example: send `/setcommands` to @BotFather, then paste the above lines and confirm. diff --git a/telegram_bot.py b/telegram_bot.py index c1e08f8..d257a30 100644 --- a/telegram_bot.py +++ b/telegram_bot.py @@ -28,7 +28,7 @@ class TelegramBot: "/errorrate - Show autopilot error rate plot\n" "/retryfailed - Retry failed applications\n" "/resetlistings - Reset listings file\n" - "/logs - Show last 50 log lines\n" + "/logs [n] - Show last n log lines (default 50)\n" "/help - Show this help message" ) await self._send_message(help_text) @@ -60,8 +60,8 @@ class TelegramBot: ) logger.info("Monitoring paused via /stop command") - async def _handle_logs_command(self) -> None: - """Send the last 50 lines of the log file.""" + async def _handle_logs_command(self, num_lines: int = 50) -> None: + """Send the last n lines of the log file.""" log_file = "data/monitor.log" try: if not os.path.exists(log_file): @@ -71,7 +71,7 @@ class TelegramBot: with open(log_file, "r", encoding="utf-8") as f: lines = f.readlines() - last_lines = lines[-50:] if len(lines) > 50 else lines + last_lines = lines[-num_lines:] if len(lines) > num_lines else lines log_text = "".join(last_lines) # Truncate if too long for Telegram (4096 char limit) @@ -81,7 +81,7 @@ class TelegramBot: message = f"📋 Last {len(last_lines)} log lines:\n\n
{log_text}
" await self._send_message(message) - logger.info("Sent last 50 log lines via /logs command") + logger.info(f"Sent last {len(last_lines)} log lines via /logs command") except Exception as e: logger.error(f"Failed to read log file: {e}") await self._send_message(f"❌ Error reading logs: {e}") @@ -206,8 +206,17 @@ class TelegramBot: logger.error(f"/retryfailed command failed: {e}") elif text == "/resetlistings": asyncio.run_coroutine_threadsafe(self._handle_reset_listings_command(), loop) - elif text == "/logs": - asyncio.run_coroutine_threadsafe(self._handle_logs_command(), loop) + elif text.startswith("/logs"): + # Parse optional number parameter + parts = text.split() + num_lines = 50 # default + if len(parts) > 1: + try: + num_lines = int(parts[1]) + num_lines = max(1, min(num_lines, 500)) # clamp between 1-500 + except ValueError: + pass # use default if invalid + asyncio.run_coroutine_threadsafe(self._handle_logs_command(num_lines), loop) elif text.startswith("/"): asyncio.run_coroutine_threadsafe(self._handle_unknown_command(text), loop)