Skip to content

Commit

Permalink
添加配置文件读取
Browse files Browse the repository at this point in the history
  • Loading branch information
houko committed Dec 16, 2022
1 parent 789dccf commit 6053c0d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 68 deletions.
8 changes: 4 additions & 4 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 3 additions & 12 deletions bootstrap/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
22 changes: 0 additions & 22 deletions bootstrap/wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
50 changes: 39 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions config/config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
chatgpt:
wechat: chatgpt
token: your chatgpt apiKey

This comment has been minimized.

Copy link
@FOCH772

FOCH772 Feb 6, 2023

token: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

telegram: your telegram token
wechat: true
wechat_keyword: chatgpt
#telegram: your telegram token
#tgWhitelist: username1,username2
#tgKeyword: chatgpt
8 changes: 1 addition & 7 deletions handler/wechat/wechat_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
9 changes: 1 addition & 8 deletions openai/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 6053c0d

Please sign in to comment.