This commit is contained in:
Aron Petau 2026-01-04 18:29:56 +01:00
parent 743508ca66
commit a83f3002ca
2 changed files with 17 additions and 8 deletions

View file

@ -28,7 +28,7 @@ plot - Show weekly listing patterns (image)
errorrate - Show autopilot success vs failure plot (image) errorrate - Show autopilot success vs failure plot (image)
retryfailed - Retry all failed applications up to 3 times (excludes deactivated listings) 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) 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 help - Show help and command usage
Example: send `/setcommands` to @BotFather, then paste the above lines and confirm. Example: send `/setcommands` to @BotFather, then paste the above lines and confirm.

View file

@ -28,7 +28,7 @@ class TelegramBot:
"/errorrate - Show autopilot error rate plot\n" "/errorrate - Show autopilot error rate plot\n"
"/retryfailed - Retry failed applications\n" "/retryfailed - Retry failed applications\n"
"/resetlistings - Reset listings file\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" "/help - Show this help message"
) )
await self._send_message(help_text) await self._send_message(help_text)
@ -60,8 +60,8 @@ class TelegramBot:
) )
logger.info("Monitoring paused via /stop command") logger.info("Monitoring paused via /stop command")
async def _handle_logs_command(self) -> None: async def _handle_logs_command(self, num_lines: int = 50) -> None:
"""Send the last 50 lines of the log file.""" """Send the last n lines of the log file."""
log_file = "data/monitor.log" log_file = "data/monitor.log"
try: try:
if not os.path.exists(log_file): if not os.path.exists(log_file):
@ -71,7 +71,7 @@ class TelegramBot:
with open(log_file, "r", encoding="utf-8") as f: with open(log_file, "r", encoding="utf-8") as f:
lines = f.readlines() 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) log_text = "".join(last_lines)
# Truncate if too long for Telegram (4096 char limit) # Truncate if too long for Telegram (4096 char limit)
@ -81,7 +81,7 @@ class TelegramBot:
message = f"📋 <b>Last {len(last_lines)} log lines:</b>\n\n<pre>{log_text}</pre>" message = f"📋 <b>Last {len(last_lines)} log lines:</b>\n\n<pre>{log_text}</pre>"
await self._send_message(message) 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: except Exception as e:
logger.error(f"Failed to read log file: {e}") logger.error(f"Failed to read log file: {e}")
await self._send_message(f"❌ Error reading logs: {e}") await self._send_message(f"❌ Error reading logs: {e}")
@ -206,8 +206,17 @@ class TelegramBot:
logger.error(f"/retryfailed command failed: {e}") logger.error(f"/retryfailed command failed: {e}")
elif text == "/resetlistings": elif text == "/resetlistings":
asyncio.run_coroutine_threadsafe(self._handle_reset_listings_command(), loop) asyncio.run_coroutine_threadsafe(self._handle_reset_listings_command(), loop)
elif text == "/logs": elif text.startswith("/logs"):
asyncio.run_coroutine_threadsafe(self._handle_logs_command(), loop) # 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("/"): elif text.startswith("/"):
asyncio.run_coroutine_threadsafe(self._handle_unknown_command(text), loop) asyncio.run_coroutine_threadsafe(self._handle_unknown_command(text), loop)