wechatgpt/config/config.go
2022-12-16 16:23:47 +09:00

98 lines
1.8 KiB
Go

package config
import (
"github.com/spf13/viper"
"os"
)
var config *Config
type Config struct {
ChatGpt ChatGptConfig `json:"chatgpt"`
}
type ChatGptConfig struct {
Token string `json:"token,omitempty" json:"token,omitempty"`
Wechat *string `json:"wechat,omitempty"`
WechatKeyword *string `json:"wechat_keyword"`
Telegram *string `json:"telegram"`
TgWhitelist *string `json:"tg_whitelist"`
TgKeyword *string `json:"tg_keyword"`
}
func LoadConfig() error {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./local")
viper.AddConfigPath("./config")
if err := viper.ReadInConfig(); err != nil {
return err
}
if err := viper.Unmarshal(&config); err != nil {
return err
}
return nil
}
func GetConfig() *Config {
return config
}
func GetWechat() *string {
wechat := getEnv("wechat")
if wechat == nil {
wechat = config.ChatGpt.Wechat
}
return wechat
}
func GetWechatKeyword() *string {
keyword := getEnv("wechat_keyword")
if keyword == nil {
keyword = config.ChatGpt.WechatKeyword
}
return keyword
}
func GetTelegram() *string {
tg := getEnv("telegram")
if tg == nil {
tg = config.ChatGpt.Telegram
}
return tg
}
func GetTelegramKeyword() *string {
tgKeyword := getEnv("tg_keyword")
if tgKeyword == nil {
tgKeyword = config.ChatGpt.TgKeyword
}
return tgKeyword
}
func GetTelegramWhitelist() *string {
tgWhitelist := getEnv("tg_whitelist")
if tgWhitelist == nil {
tgWhitelist = config.ChatGpt.TgWhitelist
}
return tgWhitelist
}
func GetOpenAiApiKey() *string {
apiKey := getEnv("api_key")
if apiKey == nil {
apiKey = &config.ChatGpt.Token
}
return apiKey
}
func getEnv(key string) *string {
value := os.Getenv(key)
if len(value) > 0 {
return &value
}
return nil
}