Files
bl/login/internal/cmd/seerrobot.go
xinian 6e268c66f4
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
feat: 实现金币行情卡片消息格式
2026-03-23 06:05:32 +08:00

77 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"blazing/modules/player/service"
"bytes"
"fmt"
"github.com/fogleman/gg"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/driver"
"github.com/wdvxdr1123/ZeroBot/message"
)
func startrobot() {
zero.OnCommand("行情").
Handle(func(ctx *zero.Ctx) {
var msgs []string
for _, v := range service.NewGoldListService(0).Get() {
v.Rate = 1 / v.Rate
msgs = append(msgs, fmt.Sprintf("数量:%d 单价:%.2f 总价:%.2f", v.ExchangeNum, v.Rate, float64(v.ExchangeNum)*v.Rate))
}
// ======================
// 生成行情图片
// ======================
const (
width = 400
lineHeight = 24
padding = 15
)
lineCount := len(msgs)
height := padding*2 + lineHeight*lineCount + 40
// 创建图片
img := gg.NewContext(width, height)
// 背景色
img.SetRGB255(245, 247, 250)
img.Clear()
// 标题
img.SetRGB255(44, 62, 80)
img.LoadFontFace("simhei.ttf", 20) // 黑体,解决中文乱码
img.DrawString("📊 金币行情列表", padding, 30)
// 画内容
img.SetRGB255(30, 30, 30)
img.LoadFontFace("simhei.ttf", 16)
for i, line := range msgs {
y := 60 + i*lineHeight
img.DrawString(line, padding, float64(y))
}
// 保存为图片到内存(不落地文件)
var buf bytes.Buffer
img.EncodePNG(&buf) // 👈 就是你要的这个方法!
// ======================
// 发送图片zeroBot 标准方式)
// ======================
ctx.Send(message.ImageBytes(buf.Bytes()))
})
zero.RunAndBlock(&zero.Config{
NickName: []string{"bot"},
CommandPrefix: "/",
SuperUsers: []int64{123456},
Driver: []zero.Driver{
// 正向 WS
driver.NewWebSocketClient("ws://43.248.3.21:3001", "ORQ5~lofO5VDwbG7"),
// 反向 WS
// driver.NewWebSocketServer(16, "ws://127.0.0.1:6701", ""),
// // HTTP
// driver.NewHTTPClient("http://127.0.0.1:6701", "", "http://127.0.0.1:6700", ""),
},
}, nil)
}