Files
atm/scripts/register_telegram_commands.py
2026-06-13 14:00:09 +03:00

71 lines
2.6 KiB
Python

r"""Înregistrează comenzile slash ale bot-ului Telegram (meniul "/" din client).
`atm run` înregistrează deja comenzile automat la pornirea poller-ului
(`TelegramPoller._register_commands`). Acest script e pentru cazurile când vrei
să le (re)setezi fără să pornești aplicația, să le listezi sau să le ștergi.
Apelează Bot API `setMyCommands` cu aceeași listă (`atm.commands.TELEGRAM_COMMANDS`).
După rulare, în clientul Telegram apare meniul de comenzi cu autocomplete + descriere.
Notă: Telegram NU suportă parametri tipați pe slash command. Comenzile cu
argument (`/interval`, `/window`, `/rebase`) inserează doar prefixul; formatul
argumentului e documentat în descriere.
Discord nu e acoperit aici: în acest proiect Discord e webhook *outbound* —
nu există bot application / interactions endpoint, deci nu poate avea slash commands.
Usage:
.\.venv\Scripts\python.exe scripts\register_telegram_commands.py
.\.venv\Scripts\python.exe scripts\register_telegram_commands.py --list # doar afișează ce e setat acum
.\.venv\Scripts\python.exe scripts\register_telegram_commands.py --clear # șterge toate comenzile
"""
from __future__ import annotations
import sys
import requests
from atm.commands import TELEGRAM_COMMANDS
from atm.config import _ensure_env_loaded, _require_env
_BASE = "https://api.telegram.org/bot{token}/{method}"
def _call(token: str, method: str, payload: dict | None = None) -> dict:
resp = requests.post(_BASE.format(token=token, method=method), json=payload or {}, timeout=10)
body = resp.json()
if not body.get("ok"):
raise SystemExit(f"Telegram {method} eșuat: {body}")
return body
def main(argv: list[str]) -> int:
_ensure_env_loaded() # citește .env din rădăcină dacă există
token = _require_env("ATM_TG_TOKEN")
if "--list" in argv:
body = _call(token, "getMyCommands")
cmds = body.get("result", [])
if not cmds:
print("(niciun command setat)")
for c in cmds:
print(f"/{c['command']:<10} {c['description']}")
return 0
if "--clear" in argv:
_call(token, "deleteMyCommands")
print("✓ Comenzi șterse.")
return 0
commands = [{"command": c, "description": d} for c, d in TELEGRAM_COMMANDS]
_call(token, "setMyCommands", {"commands": commands})
print(f"{len(commands)} comenzi înregistrate la bot-ul Telegram:")
for c, d in TELEGRAM_COMMANDS:
print(f" /{c:<10} {d}")
print("\nDeschide chat-ul bot-ului și apasă butonul de meniu (/) ca să le vezi.")
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))