diff --git a/Taskfile.yaml b/Taskfile.yaml index e748902..599b034 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -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 \ No newline at end of file + - 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 \ No newline at end of file diff --git a/bootstrap/telegram.go b/bootstrap/telegram.go index e5b7af3..f59d50d 100644 --- a/bootstrap/telegram.go +++ b/bootstrap/telegram.go @@ -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 } diff --git a/bootstrap/wechat.go b/bootstrap/wechat.go index 92d1409..4e0ff85 100644 --- a/bootstrap/wechat.go +++ b/bootstrap/wechat.go @@ -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 -} diff --git a/config/config.go b/config/config.go index 73ff1c0..76dafba 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/config/config.yaml.example b/config/config.yaml.example index e192fa2..c42fdd5 100644 --- a/config/config.yaml.example +++ b/config/config.yaml.example @@ -1,4 +1,7 @@ chatgpt: - wechat: chatgpt token: your chatgpt apiKey - telegram: your telegram token \ No newline at end of file + wechat: true + wechat_keyword: chatgpt + #telegram: your telegram token + #tgWhitelist: username1,username2 + #tgKeyword: chatgpt diff --git a/handler/wechat/wechat_handler.go b/handler/wechat/wechat_handler.go index 342db01..d9de603 100644 --- a/handler/wechat/wechat_handler.go +++ b/handler/wechat/wechat_handler.go @@ -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) diff --git a/main.go b/main.go index e63a2b1..ee1fbf5 100644 --- a/main.go +++ b/main.go @@ -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() } - } diff --git a/openai/chatgpt.go b/openai/chatgpt.go index dddda71..84f4219 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -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{