-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_engine.go
250 lines (234 loc) · 5.98 KB
/
robot_engine.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package ranni
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
jsoniter "github.com/json-iterator/go"
"github.com/robfig/cron/v3"
"log"
"net/url"
"os"
"os/signal"
"strconv"
"time"
)
var engine = &robotEngine{
cronClient: cron.New(),
}
type robotEngine struct {
HelpNotice string
innerListeners []EventHandler
cronClient *cron.Cron
}
func Start(config *Config) {
engine.Start(config)
}
func Register(handler EventHandler) {
engine.Register(handler)
}
func RegisterCron(cronStr string, cmd func()) {
engine.RegisterCron(cronStr, cmd)
}
var robotConfig *Config
func (robotEngine *robotEngine) Start(config *Config) {
robotConfig = config
//启动定时器
robotEngine.cronClient.Start()
//启动web服务
if robotConfig.ApiAddr != "" {
go startApiServer()
}
//连接cq-http
cqConnect()
}
func startApiServer() {
e := gin.Default()
e.POST("/send", ApiSendMessage)
err := e.Run(robotConfig.ApiAddr)
if err != nil {
log.Println("api服务启动失败!", err)
}
}
func cqConnect() {
u := url.URL{Scheme: "ws", Host: robotConfig.WsAddr}
values := url.Values{}
values.Add("access_token", robotConfig.AccessToken)
u.RawQuery = values.Encode()
log.Printf("connecting to %s", robotConfig.WsAddr)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
client, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
panic(err.Error())
return
}
defer func(client *websocket.Conn) {
err := client.Close()
if err != nil {
log.Println(err.Error())
}
}(client)
done := make(chan struct{})
go func() {
defer close(done)
for {
_, message, err := client.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
go func() {
if jsoniter.Valid(message) {
msgEvent := BaseEvent{}
if err := jsoniter.Unmarshal(message, &msgEvent); err == nil {
switch msgEvent.PostType {
case "message":
if event, err := messageEventDecode(message); err == nil {
engine.CallEvent(event)
}
}
}
}
}()
}
}()
for {
select {
case <-done:
return
case <-interrupt:
log.Println("interrupt")
err := client.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}
func HelpNotice() string {
return engine.HelpNotice
}
func (robotEngine *robotEngine) Register(listener EventHandler) {
if len(robotEngine.HelpNotice) == 0 {
robotEngine.HelpNotice = "使 用 指 南\n"
}
robotEngine.HelpNotice = robotEngine.HelpNotice + "\n" + listener.Help()
if robotEngine.innerListeners == nil {
robotEngine.innerListeners = make([]EventHandler, 0)
}
robotEngine.innerListeners = append(robotEngine.innerListeners, listener)
}
func (robotEngine *robotEngine) RegisterCron(cronStr string, cmd func()) {
_, err := robotEngine.cronClient.AddFunc(cronStr, cmd)
if err != nil {
log.Println(err.Error())
log.Panicln("定时任务添加异常")
}
}
func (robotEngine *robotEngine) CallEvent(event Event) {
context := &EventContext{}
context.OriginalEvent = event
context.Values = make(map[string]interface{})
if event.EventType() == GroupMessageEventType {
messageEvent := event.(GroupMessageEvent)
context.GroupId = messageEvent.GroupId
context.UserId = messageEvent.Sender.UserId
context.EventType = GroupMessageEventType
context.SelfId = messageEvent.SelfId
context.Sender = messageEvent.Sender
context.MessageChain = &messageEvent.MessageChain
}
if event.EventType() == PrivacyMessageEventType {
messageEvent := event.(PrivacyMessageEvent)
context.UserId = messageEvent.UserId
context.EventType = PrivacyMessageEventType
context.SelfId = messageEvent.SelfId
context.Sender = messageEvent.Sender
context.MessageChain = &messageEvent.MessageChain
}
for _, callBack := range robotEngine.innerListeners {
go func(callBack EventHandler) {
if callBack.Filter(context) {
callBack.Do(context)
}
}(callBack)
}
}
func messageEventDecode(post []byte) (event Event, err error) {
messages := jsoniter.Get(post, "message")
messageChain := JsonToMessageChain(messages)
messageType := jsoniter.Get(post, "message_type").ToString()
switch messageType {
case "group":
var p = &GroupMessageEvent{}
err = jsoniter.Unmarshal(post, p)
p.MessageChain = messageChain
event = *p
case "private":
p := &PrivacyMessageEvent{}
err = jsoniter.Unmarshal(post, p)
p.MessageChain = messageChain
event = *p
default:
return nil, errors.New("未知的消息类型!")
}
if err != nil {
log.Println(err.Error())
return nil, errors.New("解析事件内容错误!")
}
return event, nil
}
func JsonToMessageChain(messages jsoniter.Any) MessageChain {
var msgS []Message
for i := 0; i < messages.Size(); i++ {
msgItem := messages.Get(i)
data := msgItem.Get("data")
switch msgItem.Get("type").ToString() {
case Text.String():
message := &TextMessage{}
data.ToVal(message)
msgS = append(msgS, *message)
case Image.String():
message := &ImageMessage{}
data.ToVal(message)
msgS = append(msgS, *message)
case At.String():
message := &AtMessage{}
content := data.Get("qq").ToString()
if content == "all" {
message.AtAll = true
} else {
parseInt, err := strconv.ParseInt(content, 10, 64)
if err != nil {
log.Println("解析At对象QQ号失败!", err.Error())
continue
}
message.Qq = parseInt
}
msgS = append(msgS, *message)
case Reply.String():
message := &ReplyMessage{}
data.ToVal(message)
msgS = append(msgS, *message)
case Face.String():
message := &FaceMessage{}
data.ToVal(message)
msgS = append(msgS, *message)
case Record.String():
message := &RecordMessage{}
data.ToVal(message)
msgS = append(msgS, *message)
default:
fmt.Println("无匹配类型", msgItem.ToString())
continue
}
}
return MessageChain{messages: msgS}
}