添加配置文件读取

This commit is contained in:
Evan 2022-12-16 16:23:47 +09:00
parent 789dccf512
commit 6053c0d0be
8 changed files with 54 additions and 68 deletions

View File

@ -19,7 +19,7 @@ tasks:
version: version:
cmds: cmds:
- docker build --platform linux/amd64 -t xiaomoinfo/wechatgpt-amd64:2.8.0 . - docker build --platform linux/amd64 -t xiaomoinfo/wechatgpt-amd64:2.9.2 .
- docker push xiaomoinfo/wechatgpt-amd64:2.8.0 - docker push xiaomoinfo/wechatgpt-amd64:2.9.2
- docker build -t xiaomoinfo/wechatgpt:2.8.0 . - docker build -t xiaomoinfo/wechatgpt:2.9.2 .
- docker push xiaomoinfo/wechatgpt:2.8.0 - docker push xiaomoinfo/wechatgpt:2.9.2

View File

@ -14,21 +14,12 @@ import (
func StartTelegramBot() { func StartTelegramBot() {
telegramKey := config.GetTelegram() telegramKey := config.GetTelegram()
if telegramKey == nil { if telegramKey == nil {
getConfig := config.GetConfig() log.Info("未找到tg token,不启动tg tot")
if getConfig == nil { return
return
}
botConfig := getConfig.ChatGpt
if botConfig.Telegram == nil {
return
}
telegramKey = botConfig.Telegram
log.Info("读取本地本置文件中的telegram token:", telegramKey)
} else {
log.Info("找到环境变量: telegram token:", telegramKey)
} }
bot, err := tgbotapi.NewBotAPI(*telegramKey) bot, err := tgbotapi.NewBotAPI(*telegramKey)
if err != nil { if err != nil {
log.Error("tg bot 启动失败:", err.Error())
return return
} }

View File

@ -3,18 +3,10 @@ package bootstrap
import ( import (
"github.com/eatmoreapple/openwechat" "github.com/eatmoreapple/openwechat"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/config"
"github.com/wechatgpt/wechatbot/handler/wechat" "github.com/wechatgpt/wechatbot/handler/wechat"
"os"
"strings"
) )
func StartWebChat() { func StartWebChat() {
keyword := getKeyword()
if len(keyword) == 0 {
log.Info("未配置微信关键字,不启动微信")
return
}
bot := openwechat.DefaultBot(openwechat.Desktop) bot := openwechat.DefaultBot(openwechat.Desktop)
bot.MessageHandler = wechat.Handler bot.MessageHandler = wechat.Handler
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
@ -50,17 +42,3 @@ func StartWebChat() {
return return
} }
} }
func getKeyword() string {
keyword := os.Getenv("wechat")
if len(strings.Trim(keyword, " ")) == 0 {
gptConfig := config.GetConfig()
if gptConfig != nil {
if gptConfig.ChatGpt.Wechat != nil {
keyword = *gptConfig.ChatGpt.Wechat
}
}
}
return keyword
}

View File

@ -12,9 +12,12 @@ type Config struct {
} }
type ChatGptConfig struct { type ChatGptConfig struct {
Wechat *string `json:"wechat,omitempty"` Token string `json:"token,omitempty" json:"token,omitempty"`
Token string `json:"token,omitempty" json:"token,omitempty"` Wechat *string `json:"wechat,omitempty"`
Telegram *string `json:"telegram"` WechatKeyword *string `json:"wechat_keyword"`
Telegram *string `json:"telegram"`
TgWhitelist *string `json:"tg_whitelist"`
TgKeyword *string `json:"tg_keyword"`
} }
func LoadConfig() error { func LoadConfig() error {
@ -36,28 +39,53 @@ func GetConfig() *Config {
return config return config
} }
func GetWechatEnv() *string { func GetWechat() *string {
return getEnv("wechat") wechat := getEnv("wechat")
if wechat == nil {
wechat = config.ChatGpt.Wechat
}
return wechat
} }
func GetWechatKeywordEnv() *string { func GetWechatKeyword() *string {
return getEnv("wechat_keyword") keyword := getEnv("wechat_keyword")
if keyword == nil {
keyword = config.ChatGpt.WechatKeyword
}
return keyword
} }
func GetTelegram() *string { func GetTelegram() *string {
return getEnv("telegram") tg := getEnv("telegram")
if tg == nil {
tg = config.ChatGpt.Telegram
}
return tg
} }
func GetTelegramKeyword() *string { func GetTelegramKeyword() *string {
return getEnv("tg_keyword") tgKeyword := getEnv("tg_keyword")
if tgKeyword == nil {
tgKeyword = config.ChatGpt.TgKeyword
}
return tgKeyword
} }
func GetTelegramWhitelist() *string { func GetTelegramWhitelist() *string {
return getEnv("tg_whitelist") tgWhitelist := getEnv("tg_whitelist")
if tgWhitelist == nil {
tgWhitelist = config.ChatGpt.TgWhitelist
}
return tgWhitelist
} }
func GetOpenAiApiKey() *string { func GetOpenAiApiKey() *string {
return getEnv("api_key") apiKey := getEnv("api_key")
if apiKey == nil {
apiKey = &config.ChatGpt.Token
}
return apiKey
} }
func getEnv(key string) *string { func getEnv(key string) *string {

View File

@ -1,4 +1,7 @@
chatgpt: chatgpt:
wechat: chatgpt
token: your chatgpt apiKey token: your chatgpt apiKey
telegram: your telegram token wechat: true
wechat_keyword: chatgpt
#telegram: your telegram token
#tgWhitelist: username1,username2
#tgKeyword: chatgpt

View File

@ -32,13 +32,7 @@ func (gmh *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
group := openwechat.Group{User: sender} group := openwechat.Group{User: sender}
log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content) log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content)
wechat := config.GetWechatKeywordEnv() wechat := config.GetWechatKeyword()
if wechat == nil {
appConfig := config.GetConfig()
if appConfig.ChatGpt.Wechat != nil {
wechat = appConfig.ChatGpt.Wechat
}
}
requestText := msg.Content requestText := msg.Content
if wechat != nil { if wechat != nil {
content, key := utils.ContainsI(msg.Content, *wechat) content, key := utils.ContainsI(msg.Content, *wechat)

View File

@ -11,12 +11,11 @@ func main() {
if err != nil { if err != nil {
log.Warn("没有找到配置文件,尝试读取环境变量") log.Warn("没有找到配置文件,尝试读取环境变量")
} }
wechatEnv := config.GetWechatEnv() wechatEnv := config.GetWechat()
telegramEnv := config.GetTelegram() telegramEnv := config.GetTelegram()
if wechatEnv != nil && *wechatEnv == "true" { if wechatEnv != nil && *wechatEnv == "true" {
bootstrap.StartWebChat() bootstrap.StartWebChat()
} else if telegramEnv != nil { } else if telegramEnv != nil {
bootstrap.StartTelegramBot() bootstrap.StartTelegramBot()
} }
} }

View File

@ -58,14 +58,7 @@ type ChatGPTRequestBody struct {
func Completions(msg string) (*string, error) { func Completions(msg string) (*string, error) {
apiKey := config.GetOpenAiApiKey() apiKey := config.GetOpenAiApiKey()
if apiKey == nil { if apiKey == nil {
appConfig := config.GetConfig() return nil, errors.New("未配置apiKey")
if appConfig == nil {
return nil, errors.New("config not found")
}
apiKey = &appConfig.ChatGpt.Token
log.Info("找到本地配置文件中的chatgpt apiKey:", apiKey)
} else {
log.Info("找到环境变量中的chatgpt apiKey:", apiKey)
} }
requestBody := ChatGPTRequestBody{ requestBody := ChatGPTRequestBody{