-
Notifications
You must be signed in to change notification settings - Fork 130
/
main.go
166 lines (147 loc) · 4.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"fmt"
"github.com/go-chi/chi/v5"
m "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/singleflight"
"io"
"net"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
"wxChatGPT/chatGPT"
"wxChatGPT/config"
"wxChatGPT/convert"
"wxChatGPT/util"
"wxChatGPT/util/middleware"
"wxChatGPT/util/signature"
)
const wxToken = "" // 这里填微信开发平台里设置的 Token
var reqGroup singleflight.Group
func init() {
log.SetLevel(config.GetLogLevel())
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
DisableColors: runtime.GOOS == "windows",
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
config.AddConfigChangeCallback(func() {
log.SetLevel(config.GetLogLevel())
})
}
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recover)
// ChatGPT 可用性检查
r.Get("/healthCheck", healthCheck)
// 微信接入校验
r.Get("/weChatGPT", wechatCheck)
// 微信消息处理
r.Post("/weChatGPT", wechatMsgReceive)
l, err := net.Listen("tcp", ":7458")
if err != nil {
log.Fatalln(err)
}
log.Infof("Server listening at %s", l.Addr())
if err = http.Serve(l, r); err != nil {
log.Fatalln(err)
}
}
// ChatGPT 可用性检查
func healthCheck(w http.ResponseWriter, r *http.Request) {
defer chatGPT.DefaultGPT().DeleteUser("healthCheck")
msg, err, _ := reqGroup.Do("healthCheck", func() (interface{}, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
result := <-chatGPT.DefaultGPT().SendMsgChan("宇宙的终极答案是什么?", "healthCheck", ctx)
return result.Val, result.Err
})
if err != nil {
panic(err)
}
log.Infof("测试返回:%s", msg)
render.PlainText(w, r, "ok")
}
// 微信接入校验
func wechatCheck(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
sign := query.Get("signature")
timestamp := query.Get("timestamp")
nonce := query.Get("nonce")
echostr := query.Get("echostr")
// 校验
if signature.CheckSignature(sign, timestamp, nonce, wxToken) {
render.PlainText(w, r, echostr)
return
}
log.Warnln("微信接入校验失败")
}
// 微信消息处理
func wechatMsgReceive(w http.ResponseWriter, r *http.Request) {
// 解析消息
body, _ := io.ReadAll(r.Body)
xmlMsg := convert.ToTextMsg(body)
log.Infof("[消息接收] Type: %s, From: %s, MsgId: %d, Content: %s", xmlMsg.MsgType, xmlMsg.FromUserName, xmlMsg.MsgId, xmlMsg.Content)
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.WriteHeader(http.StatusOK)
// 回复消息
replyMsg := ""
// 关注公众号事件
if xmlMsg.MsgType == "event" {
if xmlMsg.Event == "unsubscribe" {
chatGPT.DefaultGPT().DeleteUser(xmlMsg.FromUserName)
}
if xmlMsg.Event != "subscribe" {
util.TodoEvent(w)
return
}
replyMsg = ":) 感谢你发现了这里"
} else if xmlMsg.MsgType == "text" {
// 【收到不支持的消息类型,暂无法显示】
if strings.Contains(xmlMsg.Content, "【收到不支持的消息类型,暂无法显示】") {
util.TodoEvent(w)
return
}
// 最多等待 15 s, 超时返回空值
msg, err, _ := reqGroup.Do(strconv.FormatInt(xmlMsg.MsgId, 10), func() (interface{}, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
select {
case result := <-chatGPT.DefaultGPT().SendMsgChan(xmlMsg.Content, xmlMsg.FromUserName, ctx):
return result.Val, result.Err
case <-time.After(14*time.Second + 500*time.Millisecond):
// 超时返回错误
return "", fmt.Errorf("请求超时, MsgId: %d", xmlMsg.MsgId)
}
})
if err != nil {
panic(err)
}
replyMsg = msg.(string)
} else {
util.TodoEvent(w)
return
}
textRes := &convert.TextRes{
ToUserName: xmlMsg.FromUserName,
FromUserName: xmlMsg.ToUserName,
CreateTime: time.Now().Unix(),
MsgType: "text",
Content: replyMsg,
}
_, err := w.Write(textRes.ToXml())
if err != nil {
log.Errorln(err)
if config.GetIsDebug() {
m.PrintPrettyStack(err)
}
}
}