// Package config loads the Telegram connector's environment configuration. package config import ( "fmt" "os" "strconv" "strings" ) // Config is the Telegram connector's runtime configuration, read from the // environment. The bot token lives only in this process (ARCHITECTURE.md §12). type Config struct { // BotToken is the Telegram Bot API token (TELEGRAM_BOT_TOKEN, required). It // both authenticates the Bot API client and is the HMAC secret for Mini App // initData validation. BotToken string // GRPCAddr is the listen address of the connector gRPC server that gateway and // backend call (TELEGRAM_GRPC_ADDR, default :9091). GRPCAddr string // MiniAppURL is the HTTPS origin of the Mini App registered with BotFather; it // is the base of every launch button, to which a deep-link adds a startapp // query parameter (TELEGRAM_MINIAPP_URL, required). MiniAppURL string // APIBaseURL overrides the Bot API host (TELEGRAM_API_BASE_URL, optional; // default https://api.telegram.org) — used for a local mock or a self-hosted // Bot API server. APIBaseURL string // TestEnv routes the Bot API client to Telegram's test environment // (.../bot/test/METHOD) (TELEGRAM_TEST_ENV=true, default false). TestEnv bool // GameChannelID is the chat id of the bot's game channel for SendToGameChannel // (TELEGRAM_GAME_CHANNEL_ID, optional; 0 disables channel posts). GameChannelID int64 // LogLevel is the zap log level (TELEGRAM_LOG_LEVEL, default info). LogLevel string } // Load reads the connector configuration from the environment, applying defaults // and validating the required fields. func Load() (Config, error) { cfg := Config{ BotToken: os.Getenv("TELEGRAM_BOT_TOKEN"), GRPCAddr: envOr("TELEGRAM_GRPC_ADDR", ":9091"), MiniAppURL: os.Getenv("TELEGRAM_MINIAPP_URL"), APIBaseURL: os.Getenv("TELEGRAM_API_BASE_URL"), TestEnv: os.Getenv("TELEGRAM_TEST_ENV") == "true", LogLevel: envOr("TELEGRAM_LOG_LEVEL", "info"), } if cfg.BotToken == "" { return Config{}, fmt.Errorf("config: TELEGRAM_BOT_TOKEN is required") } if cfg.MiniAppURL == "" { return Config{}, fmt.Errorf("config: TELEGRAM_MINIAPP_URL is required") } if v := strings.TrimSpace(os.Getenv("TELEGRAM_GAME_CHANNEL_ID")); v != "" { id, err := strconv.ParseInt(v, 10, 64) if err != nil { return Config{}, fmt.Errorf("config: TELEGRAM_GAME_CHANNEL_ID %q: %w", v, err) } cfg.GameChannelID = id } return cfg, nil } // envOr returns the environment value for key, or def when it is unset or empty. func envOr(key, def string) string { if v := os.Getenv(key); v != "" { return v } return def }