彻底分开微信和tg,添加微信无关键字回复功能
This commit is contained in:
parent
28958ce22a
commit
789dccf512
65
README.md
65
README.md
@ -39,53 +39,64 @@ chatgpt:
|
|||||||
|
|
||||||
## 运行App
|
## 运行App
|
||||||
|
|
||||||
|
### 环境变量
|
||||||
|
|
||||||
|
| 变量名 | 值 | 作用 |
|
||||||
|
|----------------|-------------------|------------------|
|
||||||
|
| api_key | "chatgpt的apiKey" | 必填项 |
|
||||||
|
| wechat | "true" 或缺省 | 如果为true就会启动微信机器人 |
|
||||||
|
| wechat_keyword | "关键字"或缺省 | 如果缺省则发任何消息机器都会回复 |
|
||||||
|
| telegram | telegram的token或缺省 | 如果要启动tg机器人需要填写 |
|
||||||
|
| tg_keyword | telegram触发关键字或缺省 | 如果需要关键字触发就填写 |
|
||||||
|
| tg_whitelist | telegram的触发白名单 | 白名单以外的用户名发消息不会触发 |
|
||||||
|
|
||||||
```
|
```
|
||||||
go run main.go
|
go run main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
## `Docker` 方式运行`wechatgpt`
|
## `Docker` 方式运行`wechatgpt`
|
||||||
`建议单独跑多个docker以免互相影响`
|
|
||||||
|
|
||||||
同时启动微信和telegram,微信登陆的地址请查看运行日志
|
运行微信智能机器人的话运行下面这段代码,微信登陆的地址请查看运行日志`docker logs <containerId>`
|
||||||
```
|
|
||||||
# apple silicon
|
|
||||||
docker run -d \
|
|
||||||
--name="wechatgpt" \
|
|
||||||
-e apiKey="你的chatgpt apiKey" \
|
|
||||||
-e wechat="微信触发关键字" \
|
|
||||||
-e telegram="你的telegram token" \
|
|
||||||
xiaomoinfo/wechatgpt:latest
|
|
||||||
|
|
||||||
# linux amd64
|
|
||||||
docker run -d \
|
|
||||||
--name="wechatgpt" \
|
|
||||||
-e apiKey="你的chatgpt apiKey" \
|
|
||||||
-e wechat="微信触发关键字" \
|
|
||||||
-e telegram="你的telegram token" \
|
|
||||||
xiaomoinfo/wechatgpt-amd64:latest
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
如果只想运行微信智能机器人的话运行下面这段代码,微信登陆的地址请查看运行日志
|
|
||||||
|
|
||||||
```
|
```
|
||||||
# apple silicon
|
# apple silicon
|
||||||
docker run -d \
|
docker run -d \
|
||||||
--name wechatgpt \
|
--name wechatgpt \
|
||||||
-e apiKey="你的chatgpt apiKey" \
|
-e apiKey="你的chatgpt apiKey" \
|
||||||
-e wechat="微信触发关键字" \
|
-e wechat="true" \
|
||||||
|
-e wechatKeyword="微信触发关键字" \
|
||||||
|
xiaomoinfo/wechatgpt:latest
|
||||||
|
|
||||||
|
# linux amd64
|
||||||
|
docker run -d \
|
||||||
|
--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="true" \
|
||||||
xiaomoinfo/wechatgpt:latest
|
xiaomoinfo/wechatgpt:latest
|
||||||
|
|
||||||
# linux amd64
|
# linux amd64
|
||||||
docker run -d \
|
docker run -d \
|
||||||
--name wechatgpt \
|
--name wechatgpt \
|
||||||
-e apiKey="你的chatgpt apiKey" \
|
-e apiKey="你的chatgpt apiKey" \
|
||||||
-e wechat="微信触发关键字" \
|
-e wechat="true" \
|
||||||
xiaomoinfo/wechatgpt-amd64:latest
|
xiaomoinfo/wechatgpt-amd64:latest
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
如果只想运行`telegram`智能机器人的话运行下面这段代码
|
运行`telegram`智能机器人的话运行下面这段代码
|
||||||
|
|
||||||
```
|
```
|
||||||
# apple silicon
|
# apple silicon
|
||||||
@ -104,7 +115,6 @@ xiaomoinfo/wechatgpt-amd64:latest
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
如果运行`telegram`智能机器人时只希望指定的人使用,白名单以外的人发消息机器人不会回复
|
如果运行`telegram`智能机器人时只希望指定的人使用,白名单以外的人发消息机器人不会回复
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -126,7 +136,6 @@ xiaomoinfo/wechatgpt-amd64:latest
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
如果运行`telegram`智能机器人时希望在群里回复别人消息,可以指定一个关键字触发
|
如果运行`telegram`智能机器人时希望在群里回复别人消息,可以指定一个关键字触发
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -148,8 +157,6 @@ xiaomoinfo/wechatgpt-amd64:latest
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<img src="screenshots/docker部署.png" alt="drawing" style="width:450px;"/>
|
<img src="screenshots/docker部署.png" alt="drawing" style="width:450px;"/>
|
||||||
|
|
||||||
### 微信
|
### 微信
|
||||||
|
@ -7,14 +7,13 @@ import (
|
|||||||
"github.com/wechatgpt/wechatbot/config"
|
"github.com/wechatgpt/wechatbot/config"
|
||||||
"github.com/wechatgpt/wechatbot/handler/telegram"
|
"github.com/wechatgpt/wechatbot/handler/telegram"
|
||||||
"github.com/wechatgpt/wechatbot/utils"
|
"github.com/wechatgpt/wechatbot/utils"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartTelegramBot() {
|
func StartTelegramBot() {
|
||||||
telegramKey := os.Getenv("telegram")
|
telegramKey := config.GetTelegram()
|
||||||
if len(telegramKey) == 0 {
|
if telegramKey == nil {
|
||||||
getConfig := config.GetConfig()
|
getConfig := config.GetConfig()
|
||||||
if getConfig == nil {
|
if getConfig == nil {
|
||||||
return
|
return
|
||||||
@ -23,12 +22,12 @@ func StartTelegramBot() {
|
|||||||
if botConfig.Telegram == nil {
|
if botConfig.Telegram == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
telegramKey = *botConfig.Telegram
|
telegramKey = botConfig.Telegram
|
||||||
log.Info("读取本地本置文件中的telegram token:", telegramKey)
|
log.Info("读取本地本置文件中的telegram token:", telegramKey)
|
||||||
} else {
|
} else {
|
||||||
log.Info("找到环境变量: telegram token:", telegramKey)
|
log.Info("找到环境变量: telegram token:", telegramKey)
|
||||||
}
|
}
|
||||||
bot, err := tgbotapi.NewBotAPI(telegramKey)
|
bot, err := tgbotapi.NewBotAPI(*telegramKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -51,28 +50,30 @@ func StartTelegramBot() {
|
|||||||
chatID := update.Message.Chat.ID
|
chatID := update.Message.Chat.ID
|
||||||
chatUserName := update.Message.Chat.UserName
|
chatUserName := update.Message.Chat.UserName
|
||||||
|
|
||||||
tgUserNameStr := os.Getenv("tg_whitelist")
|
tgUserNameStr := config.GetTelegramWhitelist()
|
||||||
tgUserNames := strings.Split(tgUserNameStr, ",")
|
if tgUserNameStr != nil {
|
||||||
if len(tgUserNames) > 0 && len(tgUserNameStr) > 0 {
|
tgUserNames := strings.Split(*tgUserNameStr, ",")
|
||||||
found := false
|
if len(tgUserNames) > 0 {
|
||||||
for _, name := range tgUserNames {
|
found := false
|
||||||
if name == chatUserName {
|
for _, name := range tgUserNames {
|
||||||
found = true
|
if name == chatUserName {
|
||||||
break
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
log.Error("用户设置了私人私用,白名单以外的人不生效: ", chatUserName)
|
log.Error("用户设置了私人私用,白名单以外的人不生效: ", chatUserName)
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tgKeyWord := os.Getenv("tg_keyword")
|
tgKeyWord := config.GetTelegramKeyword()
|
||||||
var reply *string
|
var reply *string
|
||||||
// 如果设置了关键字就以关键字为准,没设置就所有消息都监听
|
// 如果设置了关键字就以关键字为准,没设置就所有消息都监听
|
||||||
if len(tgKeyWord) > 0 {
|
if tgKeyWord != nil {
|
||||||
content, key := utils.ContainsI(text, tgKeyWord)
|
content, key := utils.ContainsI(text, *tgKeyWord)
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -97,4 +98,5 @@ func StartTelegramBot() {
|
|||||||
}
|
}
|
||||||
fmt.Println(send.Text)
|
fmt.Println(send.Text)
|
||||||
}
|
}
|
||||||
|
select {}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var config *Config
|
var config *Config
|
||||||
@ -34,3 +35,35 @@ func LoadConfig() error {
|
|||||||
func GetConfig() *Config {
|
func GetConfig() *Config {
|
||||||
return 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
|
||||||
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/wechatgpt/wechatbot/config"
|
"github.com/wechatgpt/wechatbot/config"
|
||||||
"github.com/wechatgpt/wechatbot/openai"
|
"github.com/wechatgpt/wechatbot/openai"
|
||||||
"github.com/wechatgpt/wechatbot/utils"
|
"github.com/wechatgpt/wechatbot/utils"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,25 +32,26 @@ func (gmh *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
|
|||||||
group := openwechat.Group{User: sender}
|
group := openwechat.Group{User: sender}
|
||||||
log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content)
|
log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content)
|
||||||
|
|
||||||
wechat := os.Getenv("wechat")
|
wechat := config.GetWechatKeywordEnv()
|
||||||
if len(wechat) == 0 {
|
if wechat == nil {
|
||||||
appConfig := config.GetConfig()
|
appConfig := config.GetConfig()
|
||||||
if appConfig.ChatGpt.Wechat != nil {
|
if appConfig.ChatGpt.Wechat != nil {
|
||||||
wechat = *appConfig.ChatGpt.Wechat
|
wechat = appConfig.ChatGpt.Wechat
|
||||||
} else {
|
|
||||||
wechat = "chatgpt"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
|
||||||
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])
|
|
||||||
log.Println("问题:", requestText)
|
log.Println("问题:", requestText)
|
||||||
reply, err := openai.Completions(requestText)
|
reply, err := openai.Completions(requestText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
13
main.go
13
main.go
@ -11,9 +11,12 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("没有找到配置文件,尝试读取环境变量")
|
log.Warn("没有找到配置文件,尝试读取环境变量")
|
||||||
}
|
}
|
||||||
go bootstrap.StartTelegramBot()
|
wechatEnv := config.GetWechatEnv()
|
||||||
bootstrap.StartWebChat()
|
telegramEnv := config.GetTelegram()
|
||||||
//
|
if wechatEnv != nil && *wechatEnv == "true" {
|
||||||
//// 阻塞进程
|
bootstrap.StartWebChat()
|
||||||
select {}
|
} else if telegramEnv != nil {
|
||||||
|
bootstrap.StartTelegramBot()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -57,13 +56,13 @@ type ChatGPTRequestBody struct {
|
|||||||
//
|
//
|
||||||
// Completions sendMsg
|
// Completions sendMsg
|
||||||
func Completions(msg string) (*string, error) {
|
func Completions(msg string) (*string, error) {
|
||||||
apiKey := os.Getenv("apiKey")
|
apiKey := config.GetOpenAiApiKey()
|
||||||
if len(apiKey) == 0 {
|
if apiKey == nil {
|
||||||
appConfig := config.GetConfig()
|
appConfig := config.GetConfig()
|
||||||
if appConfig == nil {
|
if appConfig == nil {
|
||||||
return nil, errors.New("config not found")
|
return nil, errors.New("config not found")
|
||||||
}
|
}
|
||||||
apiKey = appConfig.ChatGpt.Token
|
apiKey = &appConfig.ChatGpt.Token
|
||||||
log.Info("找到本地配置文件中的chatgpt apiKey:", apiKey)
|
log.Info("找到本地配置文件中的chatgpt apiKey:", apiKey)
|
||||||
} else {
|
} else {
|
||||||
log.Info("找到环境变量中的chatgpt apiKey:", apiKey)
|
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("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{}
|
client := &http.Client{}
|
||||||
response, err := client.Do(req)
|
response, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user