diff --git a/README.md b/README.md index 134edaa..0b0deb6 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,28 @@ xiaomoinfo/wechatgpt-amd64:latest ``` +如果运行`telegram`智能机器人时希望在群里回复别人消息,可以指定一个关键字触发 + +``` +# apple silicon +docker run -d \ +--name wechatgpt \ +-e apiKey="你的chatgpt apiKey" \ +-e telegram="你的telegram token" \ +-e tg_keyword="小莫" \ +xiaomoinfo/wechatgpt:latest + +# linux amd64 +docker run -d \ +--name wechatgpt \ +-e apiKey="你的chatgpt apiKey" \ +-e telegram="你的telegram token" \ +-e tg_keyword="小莫" \ +xiaomoinfo/wechatgpt-amd64:latest + +``` + + drawing diff --git a/Taskfile.yaml b/Taskfile.yaml index 53a1df4..aa68815 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -21,7 +21,7 @@ tasks: version: cmds: - - docker build --platform linux/amd64 -t xiaomoinfo/wechatgpt-amd64:2.6 . - - docker push xiaomoinfo/wechatgpt-amd64:2.6 - - docker build -t xiaomoinfo/wechatgpt:2.6 . - - docker push xiaomoinfo/wechatgpt:2.6 \ No newline at end of file + - 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 diff --git a/bootstrap/telegram.go b/bootstrap/telegram.go index b91c0c9..d4a3f72 100644 --- a/bootstrap/telegram.go +++ b/bootstrap/telegram.go @@ -6,6 +6,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/wechatgpt/wechatbot/config" "github.com/wechatgpt/wechatbot/handler/telegram" + "github.com/wechatgpt/wechatbot/utils" "os" "strings" "time" @@ -51,10 +52,8 @@ func StartTelegramBot() { chatUserName := update.Message.Chat.UserName tgUserNameStr := os.Getenv("tg_whitelist") - tgUserNames := strings.Split(tgUserNameStr, ",") - - if len(tgUserNames) > 0 { + if len(tgUserNames) > 0 && len(tgUserNameStr) > 0 { found := false for _, name := range tgUserNames { if name == chatUserName { @@ -65,19 +64,36 @@ func StartTelegramBot() { if !found { log.Error("用户设置了私人私用,白名单以外的人不生效: ", chatUserName) - return + continue } } - responseMsg := telegram.Handle(text) - if responseMsg == nil { + tgKeyWord := os.Getenv("tg_keyword") + var reply *string + // 如果设置了关键字就以关键字为准,没设置就所有消息都监听 + if len(tgKeyWord) > 0 { + content, key := utils.ContainsI(text, tgKeyWord) + if len(key) == 0 { + continue + } + splitItems := strings.Split(content, key) + if len(splitItems) < 2 { + continue + } + requestText := strings.TrimSpace(splitItems[1]) + log.Println("问题:", requestText) + reply = telegram.Handle(requestText) + } else { + reply = telegram.Handle(text) + } + if reply == nil { continue } - msg := tgbotapi.NewMessage(chatID, *responseMsg) + msg := tgbotapi.NewMessage(chatID, *reply) send, err := bot.Send(msg) if err != nil { log.Errorf("发送消息出错:%s", err.Error()) - return + continue } fmt.Println(send.Text) }