"""CLI entry point. python -m meetings_telegram_bot # run the polling loop python -m meetings_telegram_bot --once # single cycle, then exit python -m meetings_telegram_bot --dry-run # log what would be posted python -m meetings_telegram_bot --check # validate config, then exit Or from the project root: python bots/run_bot.py [flags] """ from __future__ import annotations import argparse import logging import signal import sys from .bot import MeetingsBot from .config import Config, ConfigError, load_config log = logging.getLogger("meetings_telegram_bot") def _configure_logging(debug: bool) -> None: logging.basicConfig( level=logging.DEBUG if debug else logging.INFO, format="%(asctime)s %(levelname)-8s %(name)s: %(message)s", datefmt="%Y-%m-%dT%H:%M:%S%z", ) # Windows consoles often default to a legacy code page; switch to UTF-8. for stream in (sys.stdout, sys.stderr): try: stream.reconfigure(encoding="utf-8") except (AttributeError, ValueError): pass def _parse_args(argv: list[str] | None = None) -> argparse.Namespace: parser = argparse.ArgumentParser( prog="meetings_telegram_bot", description="Watch for new meetings and announce them on Telegram.", ) parser.add_argument("--once", action="store_true", help="run one cycle and exit") parser.add_argument("--dry-run", action="store_true", help="log only, post nothing") parser.add_argument("--check", action="store_true", help="validate config and exit") parser.add_argument("--debug", action="store_true", help="enable debug logging") return parser.parse_args(argv) def _mask(value: str) -> str: if not value: return "(empty)" if len(value) <= 4: return "*" * len(value) return value[:2] + "*" * (len(value) - 4) + value[-2:] def _mask_proxy(value: str) -> str: """Hide proxy credentials, e.g. socks5://user:pass@host -> socks5://***@host.""" if not value: return "(none)" if "@" in value: scheme, sep, rest = value.partition("://") return f"{scheme + '://' if sep else ''}***@{rest.rpartition('@')[2]}" return value def _check(cfg: Config) -> int: print("Configuration looks valid:") print(f" API_BASE_URL = {cfg.api_base_url}") print(f" API_LOGIN = {_mask(cfg.api_login)}") print(f" API_PASSWORD = {'(set)' if cfg.api_password else '(empty)'}") print(f" TELEGRAM_BOT_TOKEN = {_mask(cfg.bot_token)}") print(f" TELEGRAM_CHAT_ID = {cfg.chat_id}") print(f" BOT_POLL_INTERVAL_SECONDS = {cfg.poll_interval_seconds}") print(f" BOT_STATE_FILE = {cfg.state_file}") print(f" TIMEZONE = {cfg.timezone or 'local'}") print(f" SOCKS5_PROXY = {_mask_proxy(cfg.socks5_proxy)}") return 0 def main(argv: list[str] | None = None) -> int: args = _parse_args(argv) try: cfg = load_config() except ConfigError as exc: print(f"configuration error: {exc}", file=sys.stderr) return 2 _configure_logging(args.debug) if args.check: return _check(cfg) bot = MeetingsBot(cfg) def _handle_signal(signum, _frame): # noqa: ANN001 log.info("received signal %s, shutting down", signum) bot.stop() signal.signal(signal.SIGINT, _handle_signal) signal.signal(signal.SIGTERM, _handle_signal) if args.once or args.dry_run: bot.start() try: count = bot.poll_once(dry_run=args.dry_run) except Exception: # noqa: BLE001 log.exception("poll failed") return 1 log.info("single run complete: %d meeting(s) announced", count) return 0 bot.run() return 0 if __name__ == "__main__": raise SystemExit(main())