gptbot/wechatbot/config/config.go

65 lines
1.4 KiB
Go

package config
import (
"encoding/json"
"log"
"os"
"sync"
)
// Configuration 项目配置
type Configuration struct {
// gpt apikey
ApiKey string `json:"api_key"`
// 自动通过好友
AutoPass bool `json:"auto_pass"`
// bing 聊天接口
BingChatUrl string `json:"bing_chat_url"`
GptChatUrl string `json:"gpt_chat_url"`
// 机器人唤醒词
BingChatWakeWord string `json:"bing_chat_wake_word"`
GptChatWakeWord string `json:"gpt_chat_wake_word"`
// 消息缓存数量
GptMessageCache int `json:"gpt_message_cache"`
// gpt上下文清空指令
GptCleanContext string `json:"gpt_clean_context"`
}
var config *Configuration
var once sync.Once
// LoadConfig 加载配置
func LoadConfig() *Configuration {
once.Do(func() {
// 从文件中读取
config = &Configuration{}
f, err := os.Open("config.json")
if err != nil {
log.Fatalf("open config err: %v", err)
return
}
defer f.Close()
encoder := json.NewDecoder(f)
err = encoder.Decode(config)
if err != nil {
log.Fatalf("decode config err: %v", err)
return
}
// 如果环境变量有配置,读取环境变量
ApiKey := os.Getenv("ApiKey")
AutoPass := os.Getenv("AutoPass")
BingChatUrl := os.Getenv("BingChatUrl")
if ApiKey != "" {
config.ApiKey = ApiKey
}
if AutoPass == "true" {
config.AutoPass = true
}
if BingChatUrl != "" {
config.BingChatUrl = BingChatUrl
}
})
return config
}