autoregister commands

This commit is contained in:
Aron Petau 2026-01-12 16:24:53 +01:00
parent a628bdb9db
commit 867c6f3152

View file

@ -142,11 +142,42 @@ class TelegramBot:
await self._http_client.aclose() await self._http_client.aclose()
self._http_client = None self._http_client = None
async def _register_commands(self) -> None:
"""Register bot commands with Telegram API for autocomplete."""
if not self.bot_token:
return
commands = [
{"command": "start", "description": "Resume monitoring for new listings"},
{"command": "stop", "description": "Pause monitoring (bot stays running, commands still work)"},
{"command": "autopilot", "description": "Enable or disable automatic applications (usage: /autopilot on|off)"},
{"command": "status", "description": "Show current status and statistics"},
{"command": "plot", "description": "Show weekly listing patterns (image)"},
{"command": "errorrate", "description": "Show autopilot success vs failure plot"},
{"command": "retryfailed", "description": "Retry all failed applications"},
{"command": "resetlistings", "description": "Delete all seen listings (forces re-check)"},
{"command": "logs", "description": "Show last n log lines (usage: /logs [n])"},
{"command": "help", "description": "Show help and command usage"}
]
url = f"https://api.telegram.org/bot{self.bot_token}/setMyCommands"
try:
client = await self._get_http_client()
response = await client.post(url, json={"commands": commands})
if response.is_success:
logger.info("Successfully registered bot commands with Telegram")
else:
logger.warning(f"Failed to register bot commands: {response.text}")
except Exception as e:
logger.error(f"Error registering bot commands: {e}")
def start(self) -> None: def start(self) -> None:
if not self.bot_token: if not self.bot_token:
logger.warning("Telegram bot token not configured, commands disabled") logger.warning("Telegram bot token not configured, commands disabled")
return return
self.running = True self.running = True
# Register commands with Telegram API
asyncio.run_coroutine_threadsafe(self._register_commands(), self.event_loop)
thread = threading.Thread(target=self._poll_updates, daemon=True) thread = threading.Thread(target=self._poll_updates, daemon=True)
thread.start() thread.start()
logger.info("Telegram command listener started") logger.info("Telegram command listener started")