-
Notifications
You must be signed in to change notification settings - Fork 46
/
grasspicture.go
166 lines (159 loc) · 4.85 KB
/
grasspicture.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 grasspicture 草图
package grasspicture
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"os"
"strings"
"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/floatbox/web"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
const (
grassURL = "https://oss.grass.starxw.com"
infoURL = grassURL + "/service/info"
imageURL = grassURL + "/service/image"
statusURL = grassURL + "/service/status"
uploadURL = grassURL + "/service/upload"
)
type infoRsp struct {
Code int `json:"code"`
ID string `json:"id"`
LikeCount int `json:"likeCount"`
}
type statusRsp struct {
TotalImage int `json:"totalImage"`
TotalImageSize int `json:"totalImageSize"`
TotalImageSizeHuman string `json:"totalImageSizeHuman"`
WaitImage string `json:"waitImage"`
APICountToday int `json:"apiCountToday"`
APIFlowToday int `json:"apiFlowToday"`
APIFlowTodayHuman string `json:"apiFlowTodayHuman"`
Service bool `json:"service"`
}
type uploadRsp struct {
Code int `json:"code"`
}
func init() {
engine := control.Register("grasspicture", &ctrl.Options[*zero.Ctx]{
PrivateDataFolder: "grasspicture",
DisableOnDefault: false,
Brief: "草图",
Help: "- 生草 | 来张草图\n- 草图信息\n- 草图投稿 (来个大佬解决问题)\n开发文档: https://apifox.com/apidoc/shared-8a633395-412a-460a-87e0-b82012573873",
})
cachePath := engine.DataFolder() + "cache/"
_ = os.RemoveAll(cachePath)
_ = os.MkdirAll(cachePath, 0755)
engine.OnFullMatchGroup([]string{"生草", "来张草图"}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
data, err := web.GetData(infoURL)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
var info infoRsp
err = json.Unmarshal(data, &info)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
data, err = web.GetData(fmt.Sprintf("%v?id=%v&type=image", imageURL, info.ID))
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.ImageBytes(data))
})
engine.OnFullMatchGroup([]string{"草图信息"}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
data, err := web.GetData(statusURL)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
var status statusRsp
err = json.Unmarshal(data, &status)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
text := fmt.Sprintf("---==草图信息==---\n是否正常提供服务: %v\n图片总数: %v\n待审核图片数: %v\n今日调用次数: %v\n图片总大小: %v\n今日图片流量: %v\n", status.Service, status.TotalImage, status.WaitImage, status.APICountToday, status.TotalImageSizeHuman, status.APIFlowTodayHuman)
ctx.SendChain(message.Text(text))
})
engine.OnMessage(zero.AdminPermission, func(ctx *zero.Ctx) bool {
msg := ctx.Event.Message
if len(msg) < 1 {
return false
}
if msg[0].Type != "reply" {
return false
}
if !strings.Contains(msg.ExtractPlainText(), "草图投稿") {
return false
}
return true
}).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
id := ctx.Event.Message[0].Data["id"]
imageURL := ctx.GetMessage(message.NewMessageIDFromString(id)).Elements[0].Data["url"]
fileName := cachePath + id + ".jpg"
err := file.DownloadTo(imageURL, fileName)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
var payload bytes.Buffer
writer := multipart.NewWriter(&payload)
file, err := os.Open(fileName)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
defer file.Close()
part, err := writer.CreateFormFile("file", fileName)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
_, err = io.Copy(part, file)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
err = writer.Close()
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
data, err := web.PostData(uploadURL, writer.FormDataContentType(), &payload)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
var upload uploadRsp
err = json.Unmarshal(data, &upload)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
var text string
switch upload.Code {
case 200:
text = "草图投稿成功"
case 400:
text = "您已被限流"
case 403:
text = "您已被加入黑名单"
case 1000:
text = "没有发送文件,或发送的文件非有效图片格式"
case 1001:
text = "文件体积大于 2MB"
}
ctx.SendChain(message.ReplyWithMessage(ctx.Event.Message[0].Data["id"], message.Text(text))...)
})
}