使用"file:"发送图片失败后,改用base64发送
(filePath string, ctx *zero.Ctx)
| 346 | |
| 347 | // 使用"file:"发送图片失败后,改用base64发送 |
| 348 | func trySendImage(filePath string, ctx *zero.Ctx) { |
| 349 | filePath = file.BOTPATH + "/" + filePath |
| 350 | if id := ctx.SendChain(message.Image("file:///" + filePath)); id.ID() != 0 { |
| 351 | return |
| 352 | } |
| 353 | imgFile, err := os.Open(filePath) |
| 354 | if err != nil { |
| 355 | ctx.SendChain(message.Text("ERROR: 无法打开文件", err)) |
| 356 | return |
| 357 | } |
| 358 | defer imgFile.Close() |
| 359 | // 使用 base64.NewEncoder 将文件内容编码为 base64 字符串 |
| 360 | var encodedFileData strings.Builder |
| 361 | encodedFileData.WriteString("base64://") |
| 362 | encoder := base64.NewEncoder(base64.StdEncoding, &encodedFileData) |
| 363 | _, err = io.Copy(encoder, imgFile) |
| 364 | if err != nil { |
| 365 | ctx.SendChain(message.Text("ERROR: 无法编码文件内容", err)) |
| 366 | return |
| 367 | } |
| 368 | encoder.Close() |
| 369 | drawedFileBase64 := encodedFileData.String() |
| 370 | if id := ctx.SendChain(message.Image(drawedFileBase64)); id.ID() == 0 { |
| 371 | ctx.SendChain(message.Text("ERROR: 无法读取图片文件", err)) |
| 372 | return |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // 从已有签到背景中,复制出一张图片 |
| 377 | func copyImage(picFile string) (err error) { |