Skip to content

Commit b8858f0

Browse files
authored
[issues 840] Modify the "score" of the Image send type add base64 (#853)
Modify the "score":Image send type add base64 适配shamrock,以路径发送图片失败后,使用base64发送图片。
1 parent 9c65383 commit b8858f0

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

plugin/score/sign_in.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
package score
33

44
import (
5+
"encoding/base64"
6+
"io"
57
"math"
68
"math/rand"
79
"os"
@@ -106,7 +108,7 @@ func init() {
106108
// 如果签到时间是今天
107109
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("今天你已经签到过了!"))
108110
if file.IsExist(drawedFile) {
109-
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
111+
trySendImage(drawedFile, ctx)
110112
}
111113
return
112114
case siUpdateTimeStr != today:
@@ -176,7 +178,7 @@ func init() {
176178
ctx.SendChain(message.Text("ERROR: ", err))
177179
return
178180
}
179-
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
181+
trySendImage(drawedFile, ctx)
180182
})
181183

182184
engine.OnPrefix("获得签到背景", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
@@ -193,16 +195,14 @@ func init() {
193195
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请先签到!"))
194196
return
195197
}
196-
if id := ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + picFile)); id.ID() == 0 {
197-
ctx.SendChain(message.Text("ERROR: 消息发送失败, 账号可能被风控"))
198-
}
198+
trySendImage(picFile, ctx)
199199
})
200200
engine.OnFullMatch("查看等级排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
201201
Handle(func(ctx *zero.Ctx) {
202202
today := time.Now().Format("20060102")
203203
drawedFile := cachePath + today + "scoreRank.png"
204204
if file.IsExist(drawedFile) {
205-
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
205+
trySendImage(drawedFile, ctx)
206206
return
207207
}
208208
st, err := sdb.GetScoreRankByTopN(10)
@@ -267,7 +267,7 @@ func init() {
267267
ctx.SendChain(message.Text("ERROR: ", err))
268268
return
269269
}
270-
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
270+
trySendImage(drawedFile, ctx)
271271
})
272272
engine.OnRegex(`^设置签到预设\s*(\d+)$`, zero.SuperUserPermission).Limit(ctxext.LimitByUser).SetBlock(true).Handle(func(ctx *zero.Ctx) {
273273
key := ctx.State["regex_matched"].([]string)[1]
@@ -342,3 +342,32 @@ func initPic(picFile string, uid int64) (avatar []byte, err error) {
342342
}
343343
return avatar, os.WriteFile(picFile, data, 0644)
344344
}
345+
346+
// 使用"file:"发送图片失败后,改用base64发送
347+
func trySendImage(filePath string, ctx *zero.Ctx) {
348+
filePath = file.BOTPATH + "/" + filePath
349+
if id := ctx.SendChain(message.Image("file:///" + filePath)); id.ID() != 0 {
350+
return
351+
}
352+
imgFile, err := os.Open(filePath)
353+
if err != nil {
354+
ctx.SendChain(message.Text("ERROR: 无法打开文件", err))
355+
return
356+
}
357+
defer imgFile.Close()
358+
// 使用 base64.NewEncoder 将文件内容编码为 base64 字符串
359+
var encodedFileData strings.Builder
360+
encodedFileData.WriteString("base64://")
361+
encoder := base64.NewEncoder(base64.StdEncoding, &encodedFileData)
362+
_, err = io.Copy(encoder, imgFile)
363+
if err != nil {
364+
ctx.SendChain(message.Text("ERROR: 无法编码文件内容", err))
365+
return
366+
}
367+
encoder.Close()
368+
drawedFileBase64 := encodedFileData.String()
369+
if id := ctx.SendChain(message.Image(drawedFileBase64)); id.ID() == 0 {
370+
ctx.SendChain(message.Text("ERROR: 无法读取图片文件", err))
371+
return
372+
}
373+
}

0 commit comments

Comments
 (0)