diff --git a/README.md b/README.md index 0b0deb6..cd17b81 100644 --- a/README.md +++ b/README.md @@ -39,53 +39,64 @@ chatgpt: ## 运行App +### 环境变量 + +| 变量名 | 值 | 作用 | +|----------------|-------------------|------------------| +| api_key | "chatgpt的apiKey" | 必填项 | +| wechat | "true" 或缺省 | 如果为true就会启动微信机器人 | +| wechat_keyword | "关键字"或缺省 | 如果缺省则发任何消息机器都会回复 | +| telegram | telegram的token或缺省 | 如果要启动tg机器人需要填写 | +| tg_keyword | telegram触发关键字或缺省 | 如果需要关键字触发就填写 | +| tg_whitelist | telegram的触发白名单 | 白名单以外的用户名发消息不会触发 | + ``` go run main.go ``` ## `Docker` 方式运行`wechatgpt` -`建议单独跑多个docker以免互相影响` -同时启动微信和telegram,微信登陆的地址请查看运行日志 +运行微信智能机器人的话运行下面这段代码,微信登陆的地址请查看运行日志`docker logs ` + ``` -# apple silicon +# apple silicon docker run -d \ ---name="wechatgpt" \ +--name wechatgpt \ -e apiKey="你的chatgpt apiKey" \ --e wechat="微信触发关键字" \ --e telegram="你的telegram token" \ +-e wechat="true" \ +-e wechatKeyword="微信触发关键字" \ xiaomoinfo/wechatgpt:latest # linux amd64 docker run -d \ ---name="wechatgpt" \ --e apiKey="你的chatgpt apiKey" \ --e wechat="微信触发关键字" \ --e telegram="你的telegram token" \ +--name wechatgpt \ +-e wechat="true" \ +-e wechatKeyword="微信触发关键字" \ xiaomoinfo/wechatgpt-amd64:latest ``` -如果只想运行微信智能机器人的话运行下面这段代码,微信登陆的地址请查看运行日志 +运行微信智能机器人不需要任何触发关键字请运行下面这段代码,适合微信小号专业做机器人用,微信登陆的地址请查看运行日志`docker logs ` +`警告:以下命令会让任何消息都会被机器人接管,微信主号不要用下面这个命令` ``` # apple silicon docker run -d \ --name wechatgpt \ -e apiKey="你的chatgpt apiKey" \ --e wechat="微信触发关键字" \ +-e wechat="true" \ xiaomoinfo/wechatgpt:latest # linux amd64 docker run -d \ --name wechatgpt \ -e apiKey="你的chatgpt apiKey" \ --e wechat="微信触发关键字" \ +-e wechat="true" \ xiaomoinfo/wechatgpt-amd64:latest ``` -如果只想运行`telegram`智能机器人的话运行下面这段代码 +运行`telegram`智能机器人的话运行下面这段代码 ``` # apple silicon @@ -104,7 +115,6 @@ xiaomoinfo/wechatgpt-amd64:latest ``` - 如果运行`telegram`智能机器人时只希望指定的人使用,白名单以外的人发消息机器人不会回复 ``` @@ -126,7 +136,6 @@ xiaomoinfo/wechatgpt-amd64:latest ``` - 如果运行`telegram`智能机器人时希望在群里回复别人消息,可以指定一个关键字触发 ``` @@ -148,8 +157,6 @@ xiaomoinfo/wechatgpt-amd64:latest ``` - - drawing ### 微信 diff --git a/bootstrap/telegram.go b/bootstrap/telegram.go index d4a3f72..e5b7af3 100644 --- a/bootstrap/telegram.go +++ b/bootstrap/telegram.go @@ -7,14 +7,13 @@ import ( "github.com/wechatgpt/wechatbot/config" "github.com/wechatgpt/wechatbot/handler/telegram" "github.com/wechatgpt/wechatbot/utils" - "os" "strings" "time" ) func StartTelegramBot() { - telegramKey := os.Getenv("telegram") - if len(telegramKey) == 0 { + telegramKey := config.GetTelegram() + if telegramKey == nil { getConfig := config.GetConfig() if getConfig == nil { return @@ -23,12 +22,12 @@ func StartTelegramBot() { if botConfig.Telegram == nil { return } - telegramKey = *botConfig.Telegram + 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 { return } @@ -51,28 +50,30 @@ func StartTelegramBot() { chatID := update.Message.Chat.ID chatUserName := update.Message.Chat.UserName - tgUserNameStr := os.Getenv("tg_whitelist") - tgUserNames := strings.Split(tgUserNameStr, ",") - if len(tgUserNames) > 0 && len(tgUserNameStr) > 0 { - found := false - for _, name := range tgUserNames { - if name == chatUserName { - found = true - break + tgUserNameStr := config.GetTelegramWhitelist() + if tgUserNameStr != nil { + tgUserNames := strings.Split(*tgUserNameStr, ",") + if len(tgUserNames) > 0 { + found := false + for _, name := range tgUserNames { + if name == chatUserName { + found = true + break + } } - } - if !found { - log.Error("用户设置了私人私用,白名单以外的人不生效: ", chatUserName) - continue + if !found { + log.Error("用户设置了私人私用,白名单以外的人不生效: ", chatUserName) + continue + } } } - tgKeyWord := os.Getenv("tg_keyword") + tgKeyWord := config.GetTelegramKeyword() var reply *string // 如果设置了关键字就以关键字为准,没设置就所有消息都监听 - if len(tgKeyWord) > 0 { - content, key := utils.ContainsI(text, tgKeyWord) + if tgKeyWord != nil { + content, key := utils.ContainsI(text, *tgKeyWord) if len(key) == 0 { continue } @@ -97,4 +98,5 @@ func StartTelegramBot() { } fmt.Println(send.Text) } + select {} } diff --git a/config/config.go b/config/config.go index 2b1bcdf..73ff1c0 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "github.com/spf13/viper" + "os" ) var config *Config @@ -34,3 +35,35 @@ func LoadConfig() error { func GetConfig() *Config { return config } + +func GetWechatEnv() *string { + return getEnv("wechat") +} + +func GetWechatKeywordEnv() *string { + return getEnv("wechat_keyword") +} + +func GetTelegram() *string { + return getEnv("telegram") +} + +func GetTelegramKeyword() *string { + return getEnv("tg_keyword") +} + +func GetTelegramWhitelist() *string { + return getEnv("tg_whitelist") +} + +func GetOpenAiApiKey() *string { + return getEnv("api_key") +} + +func getEnv(key string) *string { + value := os.Getenv(key) + if len(value) > 0 { + return &value + } + return nil +} diff --git a/handler/wechat/wechat_handler.go b/handler/wechat/wechat_handler.go index 3676ee3..342db01 100644 --- a/handler/wechat/wechat_handler.go +++ b/handler/wechat/wechat_handler.go @@ -7,7 +7,6 @@ import ( "github.com/wechatgpt/wechatbot/config" "github.com/wechatgpt/wechatbot/openai" "github.com/wechatgpt/wechatbot/utils" - "os" "strings" ) @@ -33,25 +32,26 @@ 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 := os.Getenv("wechat") - if len(wechat) == 0 { + wechat := config.GetWechatKeywordEnv() + if wechat == nil { appConfig := config.GetConfig() if appConfig.ChatGpt.Wechat != nil { - wechat = *appConfig.ChatGpt.Wechat - } else { - wechat = "chatgpt" + wechat = appConfig.ChatGpt.Wechat } } - - content, key := utils.ContainsI(msg.Content, wechat) - if len(key) == 0 { - return nil - } - splitItems := strings.Split(content, key) - if len(splitItems) < 2 { - return nil + requestText := msg.Content + if wechat != nil { + content, key := utils.ContainsI(msg.Content, *wechat) + if len(key) == 0 { + return nil + } + splitItems := strings.Split(content, key) + if len(splitItems) < 2 { + return nil + } + requestText = strings.TrimSpace(splitItems[1]) } - requestText := strings.TrimSpace(splitItems[1]) + log.Println("问题:", requestText) reply, err := openai.Completions(requestText) if err != nil { diff --git a/main.go b/main.go index f6b2d9c..e63a2b1 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,12 @@ func main() { if err != nil { log.Warn("没有找到配置文件,尝试读取环境变量") } - go bootstrap.StartTelegramBot() - bootstrap.StartWebChat() - // - //// 阻塞进程 - select {} + wechatEnv := config.GetWechatEnv() + 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 6dcf089..dddda71 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -10,7 +10,6 @@ import ( "io" "io/ioutil" "net/http" - "os" "strings" ) @@ -57,13 +56,13 @@ type ChatGPTRequestBody struct { // // Completions sendMsg func Completions(msg string) (*string, error) { - apiKey := os.Getenv("apiKey") - if len(apiKey) == 0 { + apiKey := config.GetOpenAiApiKey() + if apiKey == nil { appConfig := config.GetConfig() if appConfig == nil { return nil, errors.New("config not found") } - apiKey = appConfig.ChatGpt.Token + apiKey = &appConfig.ChatGpt.Token log.Info("找到本地配置文件中的chatgpt apiKey:", apiKey) } else { log.Info("找到环境变量中的chatgpt apiKey:", apiKey) @@ -92,7 +91,7 @@ func Completions(msg string) (*string, error) { } req.Header.Set("Content-Type", "application/json") - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey)) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiKey)) client := &http.Client{} response, err := client.Do(req) if err != nil {