autoregister commands
This commit is contained in:
parent
a628bdb9db
commit
867c6f3152
1 changed files with 31 additions and 0 deletions
|
|
@ -142,11 +142,42 @@ class TelegramBot:
|
|||
await self._http_client.aclose()
|
||||
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:
|
||||
if not self.bot_token:
|
||||
logger.warning("Telegram bot token not configured, commands disabled")
|
||||
return
|
||||
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.start()
|
||||
logger.info("Telegram command listener started")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue