Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

8 changed files with 23 additions and 56 deletions

View File

@ -10,11 +10,9 @@ steps:
commands:
# - sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# - apk update && apk add build-base
- mkdir wechatbot
- CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ./wechatbot/chatgpt-arm64
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./wechatbot/chatgpt-amd64
- name: upload-tencent-cos
image: rainteam/gocos:latest
- CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o chatgpt-arm64
- name: upload
image: rainerosion/gocos
settings:
secret_id:
from_secret: cos_secret_id
@ -22,19 +20,9 @@ steps:
from_secret: cos_secret_key
bucket_url:
from_secret: cos_bucket_url
source: wechatbot
target: cicd
- name: upload-upyun
image: rainteam/upcos:latest
settings:
up_operator:
from_secret: up_operator
up_password:
from_secret: up_password
up_bucket:
from_secret: up_bucket
local_base_path: wechatbot
remote_base_path: gitea/devops
source: chatgpt-arm64
target: cicd/wechatbot
- name: gitea_release
image: plugins/gitea-release
pull: if-not-exists
@ -43,8 +31,7 @@ steps:
from_secret: gitea_token
base_url: https://git.rainss.cn
files:
- ./wechatbot/chatgpt-arm64
- ./wechatbot/chatgpt-amd64
- chatgpt-arm64
checksum:
- sha256
when:

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
storage.json
/.idea/wechatbot.iml
/config.json

View File

@ -1,11 +1,7 @@
## 微信机器人[![Build Status](http://ci.rainss.cn/api/badges/rainerosion/wechatbot/status.svg)](http://ci.rainss.cn/rainerosion/wechatbot)
## 微信机器人
最近ChatGPT异常火爆想到将其接入到个人微信是件比较有趣的事所以有了这个项目。项目基于[openwechat](https://github.com/eatmoreapple/openwechat)开发支持ChatGPT和New Bing其中New Bing[依赖于new-bing项目](../new-bing)提供的http接口。
## 自动构建
- [chatgpt-arm64](https://cos.suger.live/cicd/wechatbot/chatgpt-arm64)
- [chatgpt-amd64](https://cos.suger.live/cicd/wechatbot/chatgpt-amd64)
## 目前实现了以下功能
+ 群聊@回复
+ 私聊回复

View File

@ -2,9 +2,7 @@
"api_key": "", # chatgpt api key
"auto_pass": true, # 是否自动通过好友申请
"bing_chat_url": "" # New Bing 聊天接口
"gpt_chat_url": "https://gpt.rains.work/v1/chat/", # Gpt 聊天接口
"bing_chat_wake_word": "#bing", # new Bing唤醒词
"gpt_chat_wake_word": "#gpt" # ChatGPT唤醒词
"gpt_message_cache": 20, # 消息缓存数量
"gpt_clean_context": "#cls" # 清理Gpt上下文
"gpt_message_cache": 1 # 消息缓存数量
}

View File

@ -25,16 +25,12 @@ type ChatGPTResponseBody struct {
Model string `json:"model"`
Choices []ChoiceItem `json:"choices"`
Usage map[string]interface{} `json:"usage"`
Error ErrorGPTResponseBody `json:"error"`
}
type ChoiceItem struct {
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
}
type ErrorGPTResponseBody struct {
Message string `json:"message"`
}
// ChatGPTRequestBody 响应体
type ChatGPTRequestBody struct {
@ -49,8 +45,7 @@ var MessageCacheRegistry = make(map[string][]Message)
func CleanContext(nickName string) (string, error) {
messageCache := []Message{}
MessageCacheRegistry[nickName] = messageCache
log.Println("用户[" + nickName + "]上下文已清空")
return "上下文已清空", nil
return "用户[" + nickName + "]上下文已清空", nil
}
func Completions(msg string, nickName string) (string, error) {
@ -69,7 +64,7 @@ func Completions(msg string, nickName string) (string, error) {
MessageCacheRegistry[nickName] = messageCache
requestBody := ChatGPTRequestBody{
Model: "gpt-3.5-turbo",
MaxTokens: 2048,
MaxTokens: 4096,
Temperature: 1.2,
Messages: messageCache,
}
@ -112,8 +107,6 @@ func Completions(msg string, nickName string) (string, error) {
reply = v.Message.Content
break
}
} else {
reply = gptResponseBody.Error.Message
}
log.Printf("gpt response text: %s \n", reply)
return reply, nil

View File

@ -42,19 +42,10 @@ func (g *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
sender, err := msg.Sender()
group := openwechat.Group{User: sender}
log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content)
inGroup, _ := msg.SenderInGroup()
// 组合群名+用户名
userName := inGroup.UserName + group.UserName
bingWakeWord := config.LoadConfig().BingChatWakeWord
gptWakeWord := config.LoadConfig().GptChatWakeWord
// gpt上下文清理
if strings.EqualFold(msg.Content, config.LoadConfig().GptCleanContext) {
cleanReply, _ := gpt.CleanContext(userName)
msg.ReplyText(cleanReply)
}
// @GPTBot 或者 @bing的消息才处理
if !(strings.Contains(msg.Content, gptWakeWord) || strings.Contains(msg.Content, bingWakeWord)) {
return nil
@ -62,14 +53,17 @@ func (g *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
requestText := strings.TrimSpace(strings.ReplaceAll(msg.Content, gptWakeWord, ""))
var reply = ""
if strings.Contains(msg.Content, bingWakeWord) {
if strings.EqualFold(msg.Content, config.LoadConfig().GptCleanContext) {
cleanReply, _ := gpt.CleanContext(group.UserName)
reply = "\n" + cleanReply
} else if strings.Contains(msg.Content, bingWakeWord) {
requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, bingWakeWord, ""))
reply, err = gpt.BingSearch(requestText, userName)
reply, err = gpt.BingSearch(requestText, group.UserName)
if reply != "" && strings.HasPrefix(reply, "[") {
reply = "\n" + reply
}
} else {
reply, err = gpt.Completions(requestText, userName)
reply, err = gpt.Completions(requestText, group.UserName)
}
if err != nil {
log.Printf("gpt request error: %v \n", err)