"""Operator commands for a future bot owner. Never runs as part of application startup.""" import argparse import asyncio import sys from pydantic import ValidationError from app.channels.max_client import MaxApiError, MaxClient from app.config import Settings async def run(command: str) -> None: # Validate the same configuration even while the deployed app is still disabled. settings = Settings(max_enabled=True) client = MaxClient(settings) try: bot = await client.request("GET", "/me") if bot.get("is_bot") is not True: raise MaxApiError("not_a_bot") if bot.get("username") != settings.max_bot_username: raise MaxApiError("bot_username_mismatch") if command == "subscribe": await client.subscribe(settings) print("Webhook MAX зарегистрирован. Проверьте командой status, затем выполните пробное подключение.") elif command == "status": result = await client.request("GET", "/subscriptions") subscriptions = result.get("subscriptions", []) configured = any(isinstance(s, dict) and s.get("url") == settings.max_webhook_url for s in subscriptions) print("Нужный webhook зарегистрирован." if configured else "Нужный webhook не найден.") if not configured: raise MaxApiError("webhook_missing") else: print("Токен принят MAX, имя бота совпадает с настройкой. Подписка не изменялась.") finally: await client.close() def main() -> int: parser = argparse.ArgumentParser(description="Подготовка MAX: параметры берутся только из .env/окружения.") parser.add_argument("command", choices=("check", "subscribe", "status")) args = parser.parse_args() try: asyncio.run(run(args.command)) except ValidationError: print("Проверьте параметры MAX в .env по app/channels/MAX_SETUP.md.", file=sys.stderr) return 1 except (MaxApiError, OSError) as exc: code = exc.code if isinstance(exc, MaxApiError) else "tls_or_file_error" print(f"Не удалось выполнить настройку MAX: {code}. Секреты и ответ сервера скрыты.", file=sys.stderr) return 1 return 0 if __name__ == "__main__": raise SystemExit(main())