Compare commits

...

9 Commits
master ... main

Author SHA1 Message Date
b643c23a8a 更新 '.drone.yml'
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-09 20:57:15 +08:00
f286199d89 更新 '.drone.yml'
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-09 20:50:42 +08:00
b3329448eb 更新 '.drone.yml'
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-24 14:34:18 +08:00
8905f0e5ae update config.json file
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-23 16:49:49 +08:00
140182aef8 更新 '.drone.yml'
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-15 20:09:22 +08:00
rainerosion
48bf5368b5 modify context
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-15 18:46:39 +08:00
dcac1149f9 cicd: add linux-amd64
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-15 16:46:29 +08:00
a2ac7b02a6 cicd: add linux-amd64
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-15 16:34:03 +08:00
a6244f7a5a autu build
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-15 16:18:30 +08:00
8 changed files with 56 additions and 23 deletions

View File

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

1
.gitignore vendored
View File

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

View File

@ -1,7 +1,11 @@
## 微信机器人
## 微信机器人[![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,7 +2,9 @@
"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": 1 # 消息缓存数量
}
"gpt_message_cache": 20, # 消息缓存数量
"gpt_clean_context": "#cls" # 清理Gpt上下文
}

View File

@ -49,7 +49,7 @@ func BingSearch(msg string, nickName string) (string, error) {
if err != nil {
return "", err
}
req.Header.Set("Referer", Referer)
req.Header.Set("Referer", Referer)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(req)

View File

@ -7,9 +7,9 @@ import (
func TestBing(t *testing.T) {
reply, err := BingSearch("今天北京的天气怎么样", "nickname")
if err != nil{
t.Error(err)
}
fmt.Printf("%+v\n", reply)
if err != nil {
t.Error(err)
}
fmt.Printf("%+v\n", reply)
}

View File

@ -25,12 +25,16 @@ 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 {
@ -45,7 +49,8 @@ var MessageCacheRegistry = make(map[string][]Message)
func CleanContext(nickName string) (string, error) {
messageCache := []Message{}
MessageCacheRegistry[nickName] = messageCache
return "用户[" + nickName + "]上下文已清空", nil
log.Println("用户[" + nickName + "]上下文已清空")
return "上下文已清空", nil
}
func Completions(msg string, nickName string) (string, error) {
@ -64,7 +69,7 @@ func Completions(msg string, nickName string) (string, error) {
MessageCacheRegistry[nickName] = messageCache
requestBody := ChatGPTRequestBody{
Model: "gpt-3.5-turbo",
MaxTokens: 4096,
MaxTokens: 2048,
Temperature: 1.2,
Messages: messageCache,
}
@ -107,6 +112,8 @@ 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,10 +42,19 @@ 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
@ -53,17 +62,14 @@ func (g *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
requestText := strings.TrimSpace(strings.ReplaceAll(msg.Content, gptWakeWord, ""))
var reply = ""
if strings.EqualFold(msg.Content, config.LoadConfig().GptCleanContext) {
cleanReply, _ := gpt.CleanContext(group.UserName)
reply = "\n" + cleanReply
} else if strings.Contains(msg.Content, bingWakeWord) {
if strings.Contains(msg.Content, bingWakeWord) {
requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, bingWakeWord, ""))
reply, err = gpt.BingSearch(requestText, group.UserName)
reply, err = gpt.BingSearch(requestText, userName)
if reply != "" && strings.HasPrefix(reply, "[") {
reply = "\n" + reply
}
} else {
reply, err = gpt.Completions(requestText, group.UserName)
reply, err = gpt.Completions(requestText, userName)
}
if err != nil {
log.Printf("gpt request error: %v \n", err)