From cb7836a9c23cd4eb679c5e660d36ad6a22639865 Mon Sep 17 00:00:00 2001 From: Evan Date: Thu, 15 Dec 2022 11:01:46 +0900 Subject: [PATCH] v2.4 :rocket: --- README.md | 67 +++++++++++++++++++++++++------- Taskfile.yaml | 7 ++++ bootstrap/telegram.go | 5 ++- bootstrap/wechat.go | 23 ++++++++++- config/config.go | 6 +-- config/config.yaml.example | 4 +- handler/wechat/wechat_handler.go | 12 +++--- main.go | 3 ++ 8 files changed, 98 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index b987ec2..77cfe69 100644 --- a/README.md +++ b/README.md @@ -28,14 +28,15 @@ cp config/config.yaml.example local/config.yaml 打开 [openai](https://beta.openai.com/account/api-keys) 并注册一个账号, 生成一个apiKey并把apiKey放到`local/config.yaml` 的token下,请看如下示例: -大陆用户注册`openai`请参考 [注册ChatGPT详细指南](https://sms-activate.org/cn/info/ChatGPT) - ``` chatgpt: - keyword: 小莫 + wechat: 小莫 token: sk-pKHZD1fLYqXDjjsdsdsdUvIODTT3ssjdfadsJC2gTuqqhTum + telegram: your telegram token ``` +大陆用户注册`openai`请参考 [注册ChatGPT详细指南](https://sms-activate.org/cn/info/ChatGPT) + ## 运行App ``` @@ -47,26 +48,62 @@ go run main.go 同时启动微信和telegram,微信登陆的地址请查看运行日志 ``` -# apple silicon 如果不加触发关键字的话默认为: chatgpt -docker run -d --name="wechatgpt" -e apiKey="你的chatgpt apiKey" -e wechat="触发关键字" telegram="你的telegram token" xiaomoinfo/wechatgpt:latest +# apple silicon +docker run -d \ +--name="wechatgpt" \ +-e apiKey="你的chatgpt apiKey" \ +-e wechat="微信触发关键字" \ +-e telegram="你的telegram token" \ +xiaomoinfo/wechatgpt:latest -# linux amd64 如果不加触发关键字的话默认为: chatgpt -docker run -d --name="wechatgpt" -e apiKey="你的chatgpt apiKey" telegram="你的telegram token" -e wechat="触发关键字" xiaomoinfo/wechatgpt-amd64:latest +# linux amd64 +docker run -d \ +--name="wechatgpt" \ +-e apiKey="你的chatgpt apiKey" \ +-e wechat="微信触发关键字" \ +-e telegram="你的telegram token" \ +xiaomoinfo/wechatgpt-amd64:latest ``` - - 如果只想运行微信智能机器人的话运行下面这段代码,微信登陆的地址请查看运行日志 ``` -# apple silicon 如果不加触发关键字的话默认为: chatgpt -docker run -d --name wechatgpt -e apiKey="你的chatgpt apiKey" -e wechat="触发关键字" xiaomoinfo/wechatgpt:latest +# apple silicon +docker run -d \ +--name wechatgpt \ +-e apiKey="你的chatgpt apiKey" \ +-e wechat="微信触发关键字" \ +xiaomoinfo/wechatgpt:latest -# linux amd64 如果不加触发关键字的话默认为: chatgpt -docker run -d --name wechatgpt -e apiKey="你的chatgpt apiKey" -e wechat="触发关键字" xiaomoinfo/wechatgpt-amd64:latest +# linux amd64 +docker run -d \ +--name wechatgpt \ +-e apiKey="你的chatgpt apiKey" \ +-e wechat="微信触发关键字" \ +xiaomoinfo/wechatgpt-amd64:latest ``` + +如果只想运行`telegram`智能机器人的话运行下面这段代码 + +``` +# apple silicon +docker run -d \ +--name wechatgpt \ +-e apiKey="你的chatgpt apiKey" \ +-e telegram="你的telegram token" \ +xiaomoinfo/wechatgpt:latest + +# linux amd64 +docker run -d \ +--name wechatgpt \ +-e apiKey="你的chatgpt apiKey" \ +-e telegram="你的telegram token" \ +xiaomoinfo/wechatgpt-amd64:latest + +``` + drawing ### 微信 @@ -99,7 +136,7 @@ INFO[0099] 3 ### 如何使用 -默认为`chatgpt`,如果想设置其他的触发方式可以修改`local/config.yaml`的keyword。此时,如果别人给你发消息带有关键字`chatgpt` +默认为`chatgpt`,如果想设置其他的触发方式可以修改`local/config.yaml`的wechat。此时,如果别人给你发消息带有关键字`chatgpt` ,你的微信就会调用`chatGPT`AI自动回复你的好友。 当然,在群里也是可以的。 @@ -138,7 +175,7 @@ INFO[0099] 3 ``` chatgpt: - keyword: 小莫 + wechat: 小莫 token: sk-pKHZD1fLYqXDjjsdsdsdUvIODTT3ssjdfadsJC2gTuqqhTum telegram: 5718911250:AAhR1pn52xcCFoM_GyI2g9BaX18S7WbYviQ ``` diff --git a/Taskfile.yaml b/Taskfile.yaml index e446ba1..d224dd3 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -16,3 +16,10 @@ tasks: - docker push xiaomoinfo/wechatgpt-amd64:latest - docker build -t xiaomoinfo/wechatgpt:latest . - docker push xiaomoinfo/wechatgpt:latest + + 2.4: + cmds: + - docker build --platform linux/amd64 -t xiaomoinfo/wechatgpt-amd64:2.4 . + - docker push xiaomoinfo/wechatgpt-amd64:2.4 + - docker build -t xiaomoinfo/wechatgpt:2.4 . + - docker push xiaomoinfo/wechatgpt:2.4 \ No newline at end of file diff --git a/bootstrap/telegram.go b/bootstrap/telegram.go index cbd1b89..a9d95fe 100644 --- a/bootstrap/telegram.go +++ b/bootstrap/telegram.go @@ -18,7 +18,10 @@ func StartTelegramBot() { return } botConfig := getConfig.ChatGpt - telegramKey = botConfig.Telegram + if botConfig.Telegram == nil { + return + } + telegramKey = *botConfig.Telegram log.Info("读取本地本置文件中的telegram token:", telegramKey) } else { log.Info("找到环境变量: telegram token:", telegramKey) diff --git a/bootstrap/wechat.go b/bootstrap/wechat.go index 7229403..9311408 100644 --- a/bootstrap/wechat.go +++ b/bootstrap/wechat.go @@ -3,10 +3,17 @@ package bootstrap import ( "github.com/eatmoreapple/openwechat" log "github.com/sirupsen/logrus" + "github.com/wechatgpt/wechatbot/config" "github.com/wechatgpt/wechatbot/handler/wechat" + "os" ) func StartWebChat() { + keyword := getKeyword() + if len(keyword) == 0 { + log.Info("未配置微信关键字,不启动微信") + return + } bot := openwechat.DefaultBot(openwechat.Desktop) bot.MessageHandler = wechat.Handler bot.UUIDCallback = openwechat.PrintlnQrcodeUrl @@ -36,11 +43,23 @@ func StartWebChat() { for i, group := range groups { log.Println(i, group) } - err = bot.Block() if err != nil { log.Fatal(err) return } - +} + +func getKeyword() string { + keyword := os.Getenv("wechat") + if len(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 19acffa..2b1bcdf 100644 --- a/config/config.go +++ b/config/config.go @@ -11,9 +11,9 @@ type Config struct { } type ChatGptConfig struct { - Keyword string `json:"keyword,omitempty"` - Token string `json:"token,omitempty" json:"token,omitempty"` - Telegram string `json:"telegram"` + Wechat *string `json:"wechat,omitempty"` + Token string `json:"token,omitempty" json:"token,omitempty"` + Telegram *string `json:"telegram"` } func LoadConfig() error { diff --git a/config/config.yaml.example b/config/config.yaml.example index f18ae5e..e192fa2 100644 --- a/config/config.yaml.example +++ b/config/config.yaml.example @@ -1,4 +1,4 @@ chatgpt: - keyword: chatgpt - token: your token + wechat: chatgpt + token: your chatgpt apiKey telegram: your telegram token \ No newline at end of file diff --git a/handler/wechat/wechat_handler.go b/handler/wechat/wechat_handler.go index bc4b96c..3676ee3 100644 --- a/handler/wechat/wechat_handler.go +++ b/handler/wechat/wechat_handler.go @@ -33,17 +33,17 @@ 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) - keyword := os.Getenv("wechat") - if len(keyword) == 0 { + wechat := os.Getenv("wechat") + if len(wechat) == 0 { appConfig := config.GetConfig() - if appConfig != nil { - keyword = appConfig.ChatGpt.Keyword + if appConfig.ChatGpt.Wechat != nil { + wechat = *appConfig.ChatGpt.Wechat } else { - keyword = "chatgpt" + wechat = "chatgpt" } } - content, key := utils.ContainsI(msg.Content, keyword) + content, key := utils.ContainsI(msg.Content, wechat) if len(key) == 0 { return nil } diff --git a/main.go b/main.go index 78dbf7c..f6b2d9c 100644 --- a/main.go +++ b/main.go @@ -13,4 +13,7 @@ func main() { } go bootstrap.StartTelegramBot() bootstrap.StartWebChat() + // + //// 阻塞进程 + select {} }