Skip to content

Commit

Permalink
彻底分开微信和tg,添加微信无关键字回复功能
Browse files Browse the repository at this point in the history
  • Loading branch information
houko committed Dec 16, 2022
1 parent 28958ce commit 789dccf
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 63 deletions.
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <containerId>`

```
# 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 <containerId>`
`警告:以下命令会让任何消息都会被机器人接管,微信主号不要用下面这个命令`

```
# 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
Expand All @@ -104,7 +115,6 @@ xiaomoinfo/wechatgpt-amd64:latest
```


如果运行`telegram`智能机器人时只希望指定的人使用,白名单以外的人发消息机器人不会回复

```
Expand All @@ -126,7 +136,6 @@ xiaomoinfo/wechatgpt-amd64:latest
```


如果运行`telegram`智能机器人时希望在群里回复别人消息,可以指定一个关键字触发

```
Expand All @@ -148,8 +157,6 @@ xiaomoinfo/wechatgpt-amd64:latest
```



<img src="screenshots/docker部署.png" alt="drawing" style="width:450px;"/>

### 微信
Expand Down
42 changes: 22 additions & 20 deletions bootstrap/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -97,4 +98,5 @@ func StartTelegramBot() {
}
fmt.Println(send.Text)
}
select {}
}
33 changes: 33 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/spf13/viper"
"os"
)

var config *Config
Expand Down Expand Up @@ -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
}
30 changes: 15 additions & 15 deletions handler/wechat/wechat_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/wechatgpt/wechatbot/config"
"github.com/wechatgpt/wechatbot/openai"
"github.com/wechatgpt/wechatbot/utils"
"os"
"strings"
)

Expand All @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
9 changes: 4 additions & 5 deletions openai/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 789dccf

Please sign in to comment.