7
7
"net/http"
8
8
"net/url"
9
9
"os"
10
+ "path"
10
11
"regexp"
11
12
"strconv"
12
13
"strings"
@@ -26,12 +27,7 @@ import (
26
27
"github.com/FloatTech/ZeroBot-Plugin/plugin/vtb_quotation/model"
27
28
)
28
29
29
- const regStr = ".*/(.*)"
30
- const recordRe = "(\\ .mp3|\\ .wav|\\ .wma|\\ .mpa|\\ .ram|\\ .ra|\\ .aac|\\ .aif|\\ .m4a|\\ .tsa)"
31
-
32
- var (
33
- re = regexp .MustCompile (recordRe )
34
- )
30
+ var reg = regexp .MustCompile (".*/(.*)" )
35
31
36
32
func init () {
37
33
engine := control .Register ("vtbquotation" , & control.Options {
@@ -57,9 +53,7 @@ func init() {
57
53
58
54
engine .OnFullMatch ("vtb语录" , getdb ).SetBlock (true ).
59
55
Handle (func (ctx * zero.Ctx ) {
60
- var firstIndex int
61
- var secondIndex int
62
- var thirdIndex int
56
+ indexs := [3 ]int {}
63
57
echo , cancel := ctx .FutureEvent ("message" ,
64
58
ctx .CheckSession ()). // 只复读开启复读模式的人的消息
65
59
Repeat () // 不断监听复读
@@ -95,147 +89,122 @@ func init() {
95
89
ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("输入错误太多,请重新发指令" ))
96
90
return
97
91
}
92
+ msg := c .Event .Message .ExtractPlainText ()
93
+ num , err := strconv .Atoi (msg )
94
+ if err != nil {
95
+ ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("请输入正确的序号,三次输入错误,指令可退出重输" ))
96
+ errorCount ++
97
+ continue
98
+ }
98
99
switch step {
99
100
case 0 :
100
- firstIndex , err = strconv .Atoi (c .Event .RawMessage )
101
- // log.Debugln(fmt.Sprintf("当前在第%d步", step))
102
- // log.Debugln(fmt.Sprintf("firstIndex:%d,secondIndex:%d,thirdIndex:%d", firstIndex, secondIndex, thirdIndex))
101
+ indexs [0 ] = num
102
+ secondStepMessage , err := db .GetAllSecondCategoryMessageByFirstIndex (indexs [0 ])
103
103
if err != nil {
104
- ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("请输入正确的序号,三次输入错误,指令可退出重输" ))
104
+ ctx .SendChain (message .Text ("ERROR:" , err ))
105
+ return
106
+ }
107
+ if secondStepMessage == "" {
108
+ ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输" ))
109
+ r , err := db .GetAllFirstCategoryMessage ()
110
+ if err != nil {
111
+ ctx .SendChain (message .Text ("ERROR:" , err ))
112
+ return
113
+ }
114
+ firstStepImageBytes , err := text .RenderToBase64 (r , text .FontFile , 400 , 20 )
115
+ if err != nil {
116
+ ctx .SendChain (message .Text ("ERROR:" , err ))
117
+ return
118
+ }
119
+ if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (firstStepImageBytes ))); id .ID () == 0 {
120
+ ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
121
+ }
105
122
errorCount ++
106
123
} else {
107
- secondStepMessage , err := db . GetAllSecondCategoryMessageByFirstIndex ( firstIndex )
124
+ secondStepMessageBytes , err := text . RenderToBase64 ( secondStepMessage , text . FontFile , 400 , 20 )
108
125
if err != nil {
109
126
ctx .SendChain (message .Text ("ERROR:" , err ))
110
127
return
111
128
}
112
- // log.Debugln(secondStepMessage)
113
- if secondStepMessage == "" {
114
- ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输" ))
115
- r , err := db .GetAllFirstCategoryMessage ()
116
- if err != nil {
117
- ctx .SendChain (message .Text ("ERROR:" , err ))
118
- return
119
- }
120
- firstStepImageBytes , err := text .RenderToBase64 (r , text .FontFile , 400 , 20 )
121
- if err != nil {
122
- ctx .SendChain (message .Text ("ERROR:" , err ))
123
- return
124
- }
125
- if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (firstStepImageBytes ))); id .ID () == 0 {
126
- ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
127
- }
128
- errorCount ++
129
- } else {
130
- secondStepMessageBytes , err := text .RenderToBase64 (secondStepMessage , text .FontFile , 400 , 20 )
131
- if err != nil {
132
- ctx .SendChain (message .Text ("ERROR:" , err ))
133
- return
134
- }
135
- if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (secondStepMessageBytes ))); id .ID () == 0 {
136
- ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
137
- }
138
- step ++
129
+ if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (secondStepMessageBytes ))); id .ID () == 0 {
130
+ ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
139
131
}
132
+ step ++
140
133
}
134
+
141
135
case 1 :
142
- secondIndex , err = strconv .Atoi (c .Event .RawMessage )
143
- // log.Debugln(fmt.Sprintf("当前在第%d步", step))
144
- // log.Debugln(fmt.Sprintf("firstIndex:%d,secondIndex:%d,thirdIndex:%d", firstIndex, secondIndex, thirdIndex))
136
+ indexs [1 ] = num
137
+ thirdStepMessage , err := db .GetAllThirdCategoryMessageByFirstIndexAndSecondIndex (indexs [0 ], indexs [1 ])
145
138
if err != nil {
146
- ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("请输入正确的序号,三次输入错误,指令可退出重输" ))
139
+ ctx .SendChain (message .Text ("ERROR:" , err ))
140
+ return
141
+ }
142
+ if thirdStepMessage == "" {
143
+ ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输" ))
144
+ r , err := db .GetAllSecondCategoryMessageByFirstIndex (indexs [0 ])
145
+ if err != nil {
146
+ ctx .SendChain (message .Text ("ERROR:" , err ))
147
+ return
148
+ }
149
+ secondStepMessageBytes , err := text .RenderToBase64 (r , text .FontFile , 400 , 20 )
150
+ if err != nil {
151
+ ctx .SendChain (message .Text ("ERROR:" , err ))
152
+ return
153
+ }
154
+ if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (secondStepMessageBytes ))); id .ID () == 0 {
155
+ ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
156
+ }
147
157
errorCount ++
148
158
} else {
149
- thirdStepMessage , err := db . GetAllThirdCategoryMessageByFirstIndexAndSecondIndex ( firstIndex , secondIndex )
159
+ thirdStepMessageBytes , err := text . RenderToBase64 ( thirdStepMessage , text . FontFile , 400 , 20 )
150
160
if err != nil {
151
161
ctx .SendChain (message .Text ("ERROR:" , err ))
152
162
return
153
163
}
154
- // log.Debugln(thirdStepMessage)
155
- if thirdStepMessage == "" {
156
- ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输" ))
157
- r , err := db .GetAllSecondCategoryMessageByFirstIndex (firstIndex )
158
- if err != nil {
159
- ctx .SendChain (message .Text ("ERROR:" , err ))
160
- return
161
- }
162
- secondStepMessageBytes , err := text .RenderToBase64 (r , text .FontFile , 400 , 20 )
163
- if err != nil {
164
- ctx .SendChain (message .Text ("ERROR:" , err ))
165
- return
166
- }
167
- if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (secondStepMessageBytes ))); id .ID () == 0 {
168
- ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
169
- }
170
- errorCount ++
171
- } else {
172
- thirdStepMessageBytes , err := text .RenderToBase64 (thirdStepMessage , text .FontFile , 400 , 20 )
173
- if err != nil {
174
- ctx .SendChain (message .Text ("ERROR:" , err ))
175
- return
176
- }
177
- if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (thirdStepMessageBytes ))); id .ID () == 0 {
178
- ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
179
- }
180
- step ++
164
+ if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (thirdStepMessageBytes ))); id .ID () == 0 {
165
+ ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
181
166
}
167
+ step ++
182
168
}
183
169
case 2 :
184
- thirdIndex , err = strconv .Atoi (c .Event .RawMessage )
185
- // log.Debugln(fmt.Sprintf("当前在第%d步", step))
186
- // log.Debugln(fmt.Sprintf("firstIndex:%d,secondIndex:%d,thirdIndex:%d", firstIndex, secondIndex, thirdIndex))
187
- if err != nil {
188
- ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("请输入正确的序号,三次输入错误,指令可退出重输" ))
170
+ indexs [2 ] = num
171
+ tc := db .GetThirdCategory (indexs [0 ], indexs [1 ], indexs [2 ])
172
+ recURL := tc .ThirdCategoryPath
173
+ if recURL == "" {
174
+ ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("没有内容请重新选择,三次输入错误,指令可退出重输" ))
175
+ r , err := db .GetAllFirstCategoryMessage ()
176
+ if err != nil {
177
+ ctx .SendChain (message .Text ("ERROR:" , err ))
178
+ return
179
+ }
180
+ firstStepImageBytes , err := text .RenderToBase64 (r , text .FontFile , 400 , 20 )
181
+ if err != nil {
182
+ ctx .SendChain (message .Text ("ERROR:" , err ))
183
+ return
184
+ }
185
+ if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (firstStepImageBytes ))); id .ID () == 0 {
186
+ ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
187
+ }
189
188
errorCount ++
189
+ step = 1
190
190
} else {
191
- tc := db .GetThirdCategory (firstIndex , secondIndex , thirdIndex )
192
- reg := regexp .MustCompile (regStr )
193
- recURL := tc .ThirdCategoryPath
194
- if recURL == "" {
195
- ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("没有内容请重新选择,三次输入错误,指令可退出重输" ))
196
- r , err := db .GetAllFirstCategoryMessage ()
197
- if err != nil {
198
- ctx .SendChain (message .Text ("ERROR:" , err ))
199
- return
200
- }
201
- firstStepImageBytes , err := text .RenderToBase64 (r , text .FontFile , 400 , 20 )
202
- if err != nil {
203
- ctx .SendChain (message .Text ("ERROR:" , err ))
204
- return
205
- }
206
- if id := ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Image ("base64://" + helper .BytesToString (firstStepImageBytes ))); id .ID () == 0 {
207
- ctx .SendChain (message .Text ("ERROR:可能被风控了" ))
208
- }
209
- errorCount ++
210
- step = 1
211
- } else {
212
- if reg .MatchString (recURL ) {
213
- // log.Debugln(reg.FindStringSubmatch(recordUrl)[1])
214
- // log.Debugln(url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1]))
215
- recURL = strings .ReplaceAll (recURL , reg .FindStringSubmatch (recURL )[1 ], url .QueryEscape (reg .FindStringSubmatch (recURL )[1 ]))
216
- recURL = strings .ReplaceAll (recURL , "+" , "%20" )
217
- // log.Debugln(recordUrl)
218
- }
219
- ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("请欣赏《" + tc .ThirdCategoryName + "》" ))
220
-
221
- if ! re .MatchString (recURL ) {
222
- ctx .SendChain (message .Text ("ERROR:文件格式不匹配" ))
223
- return
224
- }
225
- format := re .FindStringSubmatch (recURL )[1 ]
226
- recordFile := storePath + fmt .Sprintf ("%d-%d-%d" , firstIndex , secondIndex , thirdIndex ) + format
227
- if file .IsExist (recordFile ) {
228
- ctx .SendChain (message .Record ("file:///" + file .BOTPATH + "/" + recordFile ))
229
- return
230
- }
231
- err = initRecord (recordFile , recURL )
232
- if err != nil {
233
- ctx .SendChain (message .Text ("ERROR:" , err ))
234
- return
235
- }
191
+ if reg .MatchString (recURL ) {
192
+ recURL = strings .ReplaceAll (recURL , reg .FindStringSubmatch (recURL )[1 ], url .QueryEscape (reg .FindStringSubmatch (recURL )[1 ]))
193
+ recURL = strings .ReplaceAll (recURL , "+" , "%20" )
194
+ }
195
+ ctx .SendChain (message .Reply (c .Event .MessageID ), message .Text ("请欣赏《" + tc .ThirdCategoryName + "》" ))
196
+ recordFile := storePath + fmt .Sprintf ("%d-%d-%d" , indexs [0 ], indexs [1 ], indexs [2 ]) + path .Ext (recURL )
197
+ if file .IsExist (recordFile ) {
236
198
ctx .SendChain (message .Record ("file:///" + file .BOTPATH + "/" + recordFile ))
237
199
return
238
200
}
201
+ err = initRecord (recordFile , recURL )
202
+ if err != nil {
203
+ ctx .SendChain (message .Text ("ERROR:" , err ))
204
+ return
205
+ }
206
+ ctx .SendChain (message .Record ("file:///" + file .BOTPATH + "/" + recordFile ))
207
+ return
239
208
}
240
209
default :
241
210
return
@@ -257,19 +226,13 @@ func init() {
257
226
tc := db .RandomVtb ()
258
227
fc := db .GetFirstCategoryByFirstUID (tc .FirstCategoryUID )
259
228
if (tc != model.ThirdCategory {}) && (fc != model.FirstCategory {}) {
260
- reg := regexp .MustCompile (regStr )
261
229
recURL := tc .ThirdCategoryPath
262
230
if reg .MatchString (recURL ) {
263
231
recURL = strings .ReplaceAll (recURL , reg .FindStringSubmatch (recURL )[1 ], url .QueryEscape (reg .FindStringSubmatch (recURL )[1 ]))
264
232
recURL = strings .ReplaceAll (recURL , "+" , "%20" )
265
233
}
266
234
ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("请欣赏" + fc .FirstCategoryName + "的《" + tc .ThirdCategoryName + "》" ))
267
- if ! re .MatchString (recURL ) {
268
- ctx .SendChain (message .Text ("ERROR:文件格式不匹配" ))
269
- return
270
- }
271
- format := re .FindStringSubmatch (recURL )[1 ]
272
- recordFile := storePath + fmt .Sprintf ("%d-%d-%d" , fc .FirstCategoryIndex , tc .SecondCategoryIndex , tc .ThirdCategoryIndex ) + format
235
+ recordFile := storePath + fmt .Sprintf ("%d-%d-%d" , fc .FirstCategoryIndex , tc .SecondCategoryIndex , tc .ThirdCategoryIndex ) + path .Ext (recURL )
273
236
if file .IsExist (recordFile ) {
274
237
ctx .SendChain (message .Record ("file:///" + file .BOTPATH + "/" + recordFile ))
275
238
return
0 commit comments