gateway_env.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """QQ (Official Bot API v2) env -> gateway config bridge (``PlatformEntry.env_overrides``)."""
  2. import logging
  3. from gateway.config import GatewayConfig, HomeChannel, Platform, PlatformConfig
  4. from gateway.platform_env import EnvCred, getenv
  5. # Logger name parity with the origin module (gateway.config_env): records stay under "gateway.config".
  6. logger = logging.getLogger("gateway.config")
  7. def _home(config: GatewayConfig, qq_config: PlatformConfig) -> None:
  8. qq_home = getenv("QQBOT_HOME_CHANNEL").strip()
  9. name_env = "QQBOT_HOME_CHANNEL_NAME"
  10. if not qq_home and (qq_home := getenv("QQ_HOME_CHANNEL").strip()):
  11. # Back-compat: accept the pre-rename name and warn.
  12. name_env = "QQ_HOME_CHANNEL_NAME"
  13. logger.warning(
  14. "QQ_HOME_CHANNEL is deprecated; rename to QQBOT_HOME_CHANNEL "
  15. "in your .env for consistency with the platform key."
  16. )
  17. if qq_home:
  18. qq_config.home_channel = HomeChannel(
  19. platform=Platform.QQBOT, chat_id=qq_home,
  20. name=getenv("QQBOT_HOME_CHANNEL_NAME") or getenv(name_env, "Home"),
  21. thread_id=getenv("QQBOT_HOME_CHANNEL_THREAD_ID") or getenv("QQ_HOME_CHANNEL_THREAD_ID") or None,
  22. )
  23. ENV_ENABLE_CREDENTIALS = ("QQ_APP_ID", "QQ_CLIENT_SECRET")
  24. ENV_OVERRIDES = (
  25. EnvCred(
  26. Platform.QQBOT, (("QQ_APP_ID", "QQ_CLIENT_SECRET"),),
  27. optional=(("app_id", "QQ_APP_ID"), ("client_secret", "QQ_CLIENT_SECRET")),
  28. optional_stripped=(("allow_from", "QQ_ALLOWED_USERS"), ("group_allow_from", "QQ_GROUP_ALLOWED_USERS")),
  29. then=_home,
  30. ),
  31. )
  32. # Authorization env names (``PlatformEntry`` fields). Core reads them from this file when the
  33. # live entry lacks them: a user plugin overriding this platform, safe mode, the plugin disabled.
  34. AUTHZ = {
  35. "allowed_users_env": "QQ_ALLOWED_USERS",
  36. "allow_all_env": "QQ_ALLOW_ALL_USERS",
  37. "own_policy_env": ("", ""),
  38. "group_allowed_chats_env": "QQ_GROUP_ALLOWED_USERS",
  39. "allowlist_suppresses_pairing": True,
  40. }