refactor: 重命名Message.RawMsg

This commit is contained in:
lixiangwuxian 2024-10-13 14:40:32 +08:00
parent eed624194a
commit 1495ac516b
11 changed files with 61 additions and 38 deletions

View File

@ -10,11 +10,11 @@ func init() {
}
func echo(msg model.Message) (reply model.Reply) {
if len(msg.Msg) <= 5 {
if len(msg.RawMsg) <= 5 {
return model.Reply{}
}
return model.Reply{
ReplyMsg: msg.Msg[5:],
ReplyMsg: msg.RawMsg[5:],
ReferOriginMsg: true,
FromMsg: msg,
}

View File

@ -11,10 +11,10 @@ func init() {
}
func getweb(msg model.Message) (reply model.Reply) {
if len(msg.Msg) <= len("getweb ") {
if len(msg.RawMsg) <= len("getweb ") {
return model.Reply{}
}
url := msg.Msg[len("getweb "):]
url := msg.RawMsg[len("getweb "):]
if err := util.ScreenshotURL(url, "./tmp/getweb/url.png", 1920, 1080, 0, 0, 0, 0, ""); err != nil {
return model.Reply{
ReplyMsg: err.Error(),

View File

@ -34,7 +34,7 @@ func MsgInHandler(msg model.Message) (reply model.Reply) {
log.Default().Printf("Recovered in MsgInHandler: %v", r)
}
}()
if msg.Msg != "" {
if msg.RawMsg != "" {
log.Default().Printf("M:%v", msg)
} else {
return model.Reply{}
@ -50,7 +50,7 @@ func MsgInHandler(msg model.Message) (reply model.Reply) {
return handler(msg)
}
}
msgArray := strings.Split(msg.Msg, " ")
msgArray := strings.Split(msg.RawMsg, " ")
if handler, ok := handlers[msgArray[0]]; ok {
return handler(msg)
}

View File

@ -24,16 +24,16 @@ func init() {
func headmasterHandler(msg model.Message) (reply model.Reply) {
from := util.From(msg.GroupInfo.GroupId, msg.UserId)
if len(msg.Msg) > 7 && msg.Msg[0:7] == "校长 " {
if len(msg.RawMsg) > 7 && msg.RawMsg[0:7] == "校长 " {
return model.Reply{
ReplyMsg: ask(from, msg.Msg[7:]),
ReplyMsg: ask(from, msg.RawMsg[7:]),
ReferOriginMsg: true,
FromMsg: msg,
}
}
// nickname := msg.UserNickName
return model.Reply{
ReplyMsg: ask(from, msg.Msg),
ReplyMsg: ask(from, msg.RawMsg),
ReferOriginMsg: true,
FromMsg: msg,
}

View File

@ -10,7 +10,7 @@ func init() {
}
func say(msg model.Message) (reply model.Reply) {
if len(msg.Msg) <= len("kw ") {
if len(msg.RawMsg) <= len("kw ") {
return model.Reply{}
}
return model.Reply{

View File

@ -50,7 +50,7 @@ func getMySS(msg model.Message) (reply model.Reply) {
func bindSS(msg model.Message) (reply model.Reply) {
return model.Reply{
ReplyMsg: scoresaber.SSQuery.BindSS(strconv.Itoa(int(msg.UserId)), msg.Msg[len("绑定ss "):]),
ReplyMsg: scoresaber.SSQuery.BindSS(strconv.Itoa(int(msg.UserId)), msg.RawMsg[len("绑定ss "):]),
ReferOriginMsg: true,
FromMsg: msg,
}
@ -66,9 +66,9 @@ func unbindSS(msg model.Message) (reply model.Reply) {
func getRecentScore(msg model.Message) (reply model.Reply) {
count := 1
if len(msg.Msg) > len("最热ss ") {
if len(msg.RawMsg) > len("最热ss ") {
var err error
count, err = strconv.Atoi(msg.Msg[len("最热ss "):])
count, err = strconv.Atoi(msg.RawMsg[len("最热ss "):])
if err != nil || count <= 0 {
return model.Reply{
ReplyMsg: "",
@ -94,9 +94,9 @@ func getRecentScore(msg model.Message) (reply model.Reply) {
func getMyRecentScore(msg model.Message) (reply model.Reply) {
count := 1
scoreMsg := ""
if len(msg.Msg) > len("最新ss ") {
if len(msg.RawMsg) > len("最新ss ") {
var err error
count, err = strconv.Atoi(msg.Msg[len("最新ss "):])
count, err = strconv.Atoi(msg.RawMsg[len("最新ss "):])
if err != nil || count <= 0 {
return model.Reply{
ReplyMsg: "",

View File

@ -12,7 +12,7 @@ func init() {
}
func xiBao(msg model.Message) (reply model.Reply) {
xibao.GenerateCongratulationImage(msg.Msg[len("喜报 "):], "./resource/xibao_background.png", "./tmp/xibao.png", true)
xibao.GenerateCongratulationImage(msg.RawMsg[len("喜报 "):], "./resource/xibao_background.png", "./tmp/xibao.png", true)
return model.Reply{
ReplyMsg: "[CQ:image,file=file:///root/qqbot/tmp/xibao.png]",
ReferOriginMsg: true,
@ -21,7 +21,7 @@ func xiBao(msg model.Message) (reply model.Reply) {
}
func beiBao(msg model.Message) (reply model.Reply) {
xibao.GenerateCongratulationImage(msg.Msg[len("悲报 "):], "./resource/beibao_background.png", "./tmp/beibao.png", false)
xibao.GenerateCongratulationImage(msg.RawMsg[len("悲报 "):], "./resource/beibao_background.png", "./tmp/beibao.png", false)
return model.Reply{
ReplyMsg: "[CQ:image,file=file:///root/qqbot/tmp/beibao.png]",
ReferOriginMsg: true,

View File

@ -4,7 +4,7 @@ type Message struct {
UserId int64 `json:"userId"`
GroupInfo GroupInfo `json:"groupInfo"`
OriginMsgId int32 `json:"originMsgId"`
Msg string `json:"msg"`
RawMsg string `json:"msg"`
UserNickName string `json:"userNickName"`
}

View File

@ -12,10 +12,9 @@ func GetSSPicture(qqId string) (outputImgName string) {
return ""
}
url := fmt.Sprintf("https://scoresaber.com/u/%s", ssId)
// url := fmt.Sprintf("https://scoresaber.com/rankings")
outputImgPath := fmt.Sprintf("./tmp/scoresaber_%s.png", ssId)
outputImgName = fmt.Sprintf("scoresaber_%s.png", ssId)
if err := util.ScreenshotURL(url, outputImgPath, 1620, 3080, 60, 0, 300, 300, "ranking songs"); err != nil {
if err := util.ScreenshotURL(url, outputImgPath, 1420, 2080, 50, 400, 150, 150, ""); err != nil {
fmt.Println(err)
return ""
}

View File

@ -14,7 +14,7 @@ import (
)
// ScreenshotURL 截图函数:传入网址、输出路径、宽高、四个边距和等待的元素 ID
func ScreenshotURL(url, output string, width, height int, marginTop, marginRight, marginBottom, marginLeft int, waitClass string) error {
func ScreenshotURL(url, output string, width, height int, marginTop, marginBottom, marginRight, marginLeft int, waitClass string) error {
// 创建一个上下文,连接到 Docker 中运行的 headless-shell 实例
remoteAllocatorCtx, cancel := chromedp.NewRemoteAllocator(
context.Background(), "ws://127.0.0.1:9222/json/ws",
@ -29,7 +29,7 @@ func ScreenshotURL(url, output string, width, height int, marginTop, marginRight
defer cancel()
// 设置页面的宽高和缩放
if err := chromedp.Run(ctx, setViewportAndUserAgent(width, height, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0")); err != nil {
if err := chromedp.Run(ctx, setViewport(width, height)); err != nil {
return fmt.Errorf("设置页面大小失败: %w", err)
}
@ -45,6 +45,30 @@ func ScreenshotURL(url, output string, width, height int, marginTop, marginRight
if waitClass == "" {
queryAction = chromedp.WaitVisible(`body`, chromedp.ByQuery)
}
// 设置自定义请求头
headers := map[string]interface{}{
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"cache-control": "max-age=0",
"priority": "u=0, i",
"sec-ch-ua": `"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"`,
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": `"Windows"`,
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
}
// 启用网络功能并设置请求头
if err := chromedp.Run(ctx, network.Enable()); err != nil {
return fmt.Errorf("启用网络失败: %w", err)
}
if err := chromedp.Run(ctx, network.SetExtraHTTPHeaders(network.Headers(headers))); err != nil {
return fmt.Errorf("设置请求头失败: %w", err)
}
// 执行任务:打开网页并截图
err := chromedp.Run(ctx,
@ -77,14 +101,13 @@ func ScreenshotURL(url, output string, width, height int, marginTop, marginRight
return nil
}
func setViewportAndUserAgent(width, height int, userAgent string) chromedp.Tasks {
func setViewport(width, height int) chromedp.Tasks {
return chromedp.Tasks{
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}),
emulation.SetUserAgentOverride(userAgent),
}
}

View File

@ -51,7 +51,7 @@ func (c *WebSocketClient) receiveMessages() {
msg := model.Message{
UserId: event.UserID,
OriginMsgId: event.MessageID,
Msg: event.RawMessage,
RawMsg: event.RawMessage,
UserNickName: event.Sender.Nickname,
}
@ -67,20 +67,21 @@ func (c *WebSocketClient) receiveMessages() {
GroupId: event.Sender.GroupID,
}
}
reply := handler.MsgInHandler(msg)
if reply.ReplyMsg != "" {
sendPkg := model.GenSendPkg(reply.FromMsg.UserId, reply.FromMsg.GroupInfo.GroupId, reply.ReplyMsg, false)
sendPkgJson, err := json.Marshal(sendPkg)
if err != nil {
log.Println("Error marshalling sendPkg:", err)
return
go func() {
reply := handler.MsgInHandler(msg)
if reply.ReplyMsg != "" {
sendPkg := model.GenSendPkg(reply.FromMsg.UserId, reply.FromMsg.GroupInfo.GroupId, reply.ReplyMsg, false)
sendPkgJson, err := json.Marshal(sendPkg)
if err != nil {
log.Println("Error marshalling sendPkg:", err)
return
}
if err = c.SendMessage(websocket.TextMessage, sendPkgJson); err != nil {
log.Println("Error sending message:", err)
return
}
}
if err = c.SendMessage(websocket.TextMessage, sendPkgJson); err != nil {
log.Println("Error sending message:", err)
return
}
}
}()
}
}