This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirai_pusher.go
180 lines (171 loc) · 4.68 KB
/
mirai_pusher.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
package main
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"io"
"net/http"
"os"
"strings"
"time"
)
func init() {
buf, err := os.ReadFile("application.json")
if err == nil {
if err = json.Unmarshal(buf, &miraiPusher); err != nil {
panic(err)
}
} else if errors.Is(err, os.ErrNotExist) {
if buf, err = json.Marshal(miraiPusher); err == nil {
_ = os.WriteFile("application.json", buf, 0666)
}
} else {
panic(err)
}
}
type MiraiPusher struct {
EnablePush bool `json:"enablePush"`
SelfRoomAddr string `json:"selfRoomAddr"`
MiraiHttpUrl string `json:"miraiHttpUrl"`
MiraiVerifyKey string `json:"miraiVerifyKey"`
RobotQQ int64 `json:"robotQQ"`
PushQQGroups []int64 `json:"pushQQGroups"`
PushInterval int64 `json:"pushInterval"`
lastPushTime int64
}
func (p *MiraiPusher) Push(room *Room) {
if !p.EnablePush {
return
}
now := time.Now().UnixMilli()
if now-p.lastPushTime < p.PushInterval*60000 {
return
}
p.lastPushTime = now
text := fmt.Sprintf("Bingo %s正在激烈进行,快来围观吧:\n%s/%s", room.Type().Name(), p.SelfRoomAddr, room.RoomId)
go func() {
defer func() {
if r := recover(); r != nil {
log.Errorln("panic recover: ", r)
}
}()
session, err := p.verify()
if err != nil {
log.Errorf("%+v", err)
return
}
if err = p.bind(session); err != nil {
log.Errorf("%+v", err)
return
}
defer func() {
if err = p.release(session); err != nil {
log.Errorf("%+v", err)
return
}
}()
for _, group := range p.PushQQGroups {
if err = p.sendGroupMessage(session, group, text); err != nil {
log.Errorf("%+v", err)
}
}
}()
}
func (p *MiraiPusher) verify() (string, error) {
resp, err := (&http.Client{Timeout: 20 * time.Second}).Post(
p.MiraiHttpUrl+"/verify",
"application/json; charset=utf-8",
strings.NewReader(fmt.Sprintf(`{"verifyKey":"%s"}`, p.MiraiVerifyKey)),
)
if err != nil {
return "", errors.Wrap(err, "verify failed")
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "verify failed")
}
if !gjson.ValidBytes(buf) {
return "", errors.Errorf("invalid json: %s", string(buf))
}
code := gjson.GetBytes(buf, "code")
if !code.Exists() || code.Int() != 0 {
return "", errors.Errorf("bind failed: %d", code.Int())
}
return gjson.GetBytes(buf, "session").String(), nil
}
func (p *MiraiPusher) bind(sessionKey string) error {
resp, err := (&http.Client{Timeout: 20 * time.Second}).Post(
p.MiraiHttpUrl+"/bind",
"application/json; charset=utf-8",
strings.NewReader(fmt.Sprintf(`{"sessionKey":"%s","qq":%d}`, sessionKey, p.RobotQQ)),
)
if err != nil {
return errors.Wrap(err, "verify failed")
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "verify failed")
}
if !gjson.ValidBytes(buf) {
return errors.Errorf("invalid json: %s", string(buf))
}
code := gjson.GetBytes(buf, "code")
if !code.Exists() || code.Int() != 0 {
return errors.Errorf("bind failed: %d", code.Int())
}
return nil
}
func (p *MiraiPusher) sendGroupMessage(sessionKey string, groupId int64, message string) error {
resp, err := (&http.Client{Timeout: 20 * time.Second}).Post(
p.MiraiHttpUrl+"/sendGroupMessage",
"application/json; charset=utf-8",
strings.NewReader(fmt.Sprintf(`{"sessionKey":"%s","target":%d,"messageChain":[{"type":"Plain","text":"%s"}]}`, sessionKey, groupId, message)),
)
if err != nil {
return errors.Wrap(err, "verify failed")
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "verify failed")
}
if !gjson.ValidBytes(buf) {
return errors.Errorf("invalid json: %s", string(buf))
}
code := gjson.GetBytes(buf, "code")
if !code.Exists() || code.Int() != 0 {
return errors.Errorf("sendGroupMessage failed: %d", code.Int())
}
return nil
}
func (p *MiraiPusher) release(sessionKey string) error {
resp, err := (&http.Client{Timeout: 20 * time.Second}).Post(
p.MiraiHttpUrl+"/release",
"application/json; charset=utf-8",
strings.NewReader(fmt.Sprintf(`{"sessionKey":"%s","qq":%d}`, sessionKey, p.RobotQQ)),
)
if err != nil {
return errors.Wrap(err, "verify failed")
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "verify failed")
}
if !gjson.ValidBytes(buf) {
return errors.Errorf("invalid json: %s", string(buf))
}
code := gjson.GetBytes(buf, "code")
if !code.Exists() || code.Int() != 0 {
return errors.Errorf("release failed: %d", code.Int())
}
return nil
}
var miraiPusher = MiraiPusher{
EnablePush: false,
SelfRoomAddr: "http://127.0.0.1:9961/room",
MiraiHttpUrl: "http://127.0.0.1:8080",
MiraiVerifyKey: "",
RobotQQ: 12345678,
PushQQGroups: nil,
PushInterval: 10,
}