添加配置文件读取

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:
cmds:
- docker build --platform linux/amd64 -t xiaomoinfo/wechatgpt-amd64:2.8.0 .
- docker push xiaomoinfo/wechatgpt-amd64:2.8.0
- docker build -t xiaomoinfo/wechatgpt:2.8.0 .
- docker push xiaomoinfo/wechatgpt:2.8.0
- docker build --platform linux/amd64 -t xiaomoinfo/wechatgpt-amd64:2.9.2 .
- docker push xiaomoinfo/wechatgpt-amd64:2.9.2
- docker build -t xiaomoinfo/wechatgpt:2.9.2 .
- docker push xiaomoinfo/wechatgpt:2.9.2

View File

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

View File

@ -3,18 +3,10 @@ package bootstrap
import (
"github.com/eatmoreapple/openwechat"
log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/config"
"github.com/wechatgpt/wechatbot/handler/wechat"
"os"
"strings"
)
func StartWebChat() {
keyword := getKeyword()
if len(keyword) == 0 {
log.Info("未配置微信关键字,不启动微信")
return
}
bot := openwechat.DefaultBot(openwechat.Desktop)
bot.MessageHandler = wechat.Handler
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
@ -50,17 +42,3 @@ func StartWebChat() {
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 {
Wechat *string `json:"wechat,omitempty"`
Token string `json:"token,omitempty" json:"token,omitempty"`
Telegram *string `json:"telegram"`
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 {
@ -36,28 +39,53 @@ func GetConfig() *Config {
return config
}
func GetWechatEnv() *string {
return getEnv("wechat")
func GetWechat() *string {
wechat := getEnv("wechat")
if wechat == nil {
wechat = config.ChatGpt.Wechat
}
return wechat
}
func GetWechatKeywordEnv() *string {
return getEnv("wechat_keyword")
func GetWechatKeyword() *string {
keyword := getEnv("wechat_keyword")
if keyword == nil {
keyword = config.ChatGpt.WechatKeyword
}
return keyword
}
func GetTelegram() *string {
return getEnv("telegram")
tg := getEnv("telegram")
if tg == nil {
tg = config.ChatGpt.Telegram
}
return tg
}
func GetTelegramKeyword() *string {
return getEnv("tg_keyword")
tgKeyword := getEnv("tg_keyword")
if tgKeyword == nil {
tgKeyword = config.ChatGpt.TgKeyword
}
return tgKeyword
}
func GetTelegramWhitelist() *string {
return getEnv("tg_whitelist")
tgWhitelist := getEnv("tg_whitelist")
if tgWhitelist == nil {
tgWhitelist = config.ChatGpt.TgWhitelist
}
return tgWhitelist
}
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 {

View File

@ -1,4 +1,7 @@
chatgpt:
wechat: chatgpt
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}
log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content)
wechat := config.GetWechatKeywordEnv()
if wechat == nil {
appConfig := config.GetConfig()
if appConfig.ChatGpt.Wechat != nil {
wechat = appConfig.ChatGpt.Wechat
}
}
wechat := config.GetWechatKeyword()
requestText := msg.Content
if wechat != nil {
content, key := utils.ContainsI(msg.Content, *wechat)

View File

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

View File

@ -58,14 +58,7 @@ type ChatGPTRequestBody struct {
func Completions(msg string) (*string, error) {
apiKey := config.GetOpenAiApiKey()
if apiKey == nil {
appConfig := config.GetConfig()
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)
return nil, errors.New("未配置apiKey")
}
requestBody := ChatGPTRequestBody{