From 46a82e1fefa92e0b209743607a9e3a07423b67af Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 12 Apr 2025 01:13:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20GetLoginAccoun?= =?UTF-8?q?tInfo=20=E5=87=BD=E6=95=B0=EF=BC=8C=E7=A7=BB=E9=99=A4=20actionM?= =?UTF-8?q?anager=20=E4=BE=9D=E8=B5=96=EF=BC=8C=E5=B9=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E8=B0=83=E7=94=A8=EF=BC=9B=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=20sendAlertMessage=20=E5=87=BD=E6=95=B0=E4=B8=BA=E5=8F=91?= =?UTF-8?q?=E9=80=81=E5=90=88=E5=B9=B6=E8=BD=AC=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- action/group_member.go | 2 +- handler/help/help.go | 2 +- handler/raid/raid.go | 89 +++++++++++++++++------------------------- message/cq_message.go | 2 +- message/forward.go | 24 +++--------- message/text.go | 4 +- 6 files changed, 45 insertions(+), 78 deletions(-) diff --git a/action/group_member.go b/action/group_member.go index 6621323..ea1020c 100644 --- a/action/group_member.go +++ b/action/group_member.go @@ -57,7 +57,7 @@ func GetGroupList() ([]model.Group, error) { return groupListResponse.Data, nil } -func (am *actionManager) GetLoginAccountInfo() (*model.LoginAccountInfoResponse, error) { +func GetLoginAccountInfo() (*model.LoginAccountInfoResponse, error) { fullURL := fmt.Sprintf("http://%s%s", config.ConfigManager.GetConfig().Management.NapcatHttpSrv, constants.GET_LOGIN_INFO) response, err := http.Post(fullURL, "application/json", nil) diff --git a/handler/help/help.go b/handler/help/help.go index fe806ba..c2e827b 100644 --- a/handler/help/help.go +++ b/handler/help/help.go @@ -35,7 +35,7 @@ func help(msg model.Message) *model.Reply { textMsg := message.NewTextMessage() textMsg.Data.Text = helpInfo nodeMsg := message.NewNodeMessage() - loginAccountInfo, err := action.ActionManager.GetLoginAccountInfo() + loginAccountInfo, err := action.GetLoginAccountInfo() if err != nil { log.Println("GetLoginAccountInfo error:", err) return nil diff --git a/handler/raid/raid.go b/handler/raid/raid.go index eed46d3..03bf6a8 100644 --- a/handler/raid/raid.go +++ b/handler/raid/raid.go @@ -12,6 +12,7 @@ import ( "git.lxtend.com/qqbot/config" "git.lxtend.com/qqbot/constants" "git.lxtend.com/qqbot/handler" + "git.lxtend.com/qqbot/message" "git.lxtend.com/qqbot/model" ) @@ -89,23 +90,36 @@ func checkRaidStatus() { } // 发送告警消息到管理群 - sendAlertMessage(sb.String()) + sendAlertMessage([]string{sb.String()}) } else { log.Println("RAID状态检查完成,未发现异常") } } // 发送告警消息 -func sendAlertMessage(alertMsg string) { +func sendAlertMessage(alertMsgs []string) { reportGroupID := config.ConfigManager.GetConfig().Management.ReportGroup if reportGroupID == 0 { - log.Println("未配置管理群,告警消息无法发送:", alertMsg) + log.Println("未配置管理群,告警消息无法发送:", alertMsgs) return } - action.ActionManager.SendMsg(&model.Reply{ - ReplyMsg: alertMsg, + nodes := []message.NodeMessage{} + selfInfo, err := action.GetLoginAccountInfo() + if err != nil { + log.Println("获取登录账号信息失败:", err) + return + } + userId := strconv.FormatInt(int64(selfInfo.Data.UserID), 10) + nickname := selfInfo.Data.Nickname + for _, alertMsg := range alertMsgs { + textMsg := message.NewTextMessage().ParseMessage(alertMsg) + nodes = append(nodes, *message.NewNodeMessage().ParseMessage(userId, nickname, []any{textMsg})) + } + + action.ActionManager.SendForward(&model.Reply{ + ReplyMsg: nodes, ReferOriginMsg: false, FromMsg: model.Message{ GroupInfo: model.GroupInfo{ @@ -128,61 +142,28 @@ func RaidHandler(msg model.Message) (reply *model.Reply) { } } - sb := strings.Builder{} - sb.WriteString("阵列信息:\n") + nodes := []message.NodeMessage{} + selfInfo, err := action.GetLoginAccountInfo() + if err != nil { + log.Println("获取登录账号信息失败:", err) + return nil + } + userId := strconv.FormatInt(int64(selfInfo.Data.UserID), 10) + nickname := selfInfo.Data.Nickname + nodes = append(nodes, *message.NewNodeMessage().ParseMessage(userId, nickname, []any{message.NewTextMessage().ParseMessage("阵列信息:\n")})) for _, diskInfo := range diskInfoList { - sb.WriteString(diskInfo.String()) + textMsg := message.NewTextMessage().ParseMessage(diskInfo.String()) + nodes = append(nodes, *message.NewNodeMessage().ParseMessage(userId, nickname, []any{textMsg})) } - return &model.Reply{ - FromMsg: msg, - ReplyMsg: sb.String(), + action.ActionManager.SendForward(&model.Reply{ + ReplyMsg: nodes, ReferOriginMsg: false, - } + FromMsg: msg, + }) + return nil } -// MegaCli64 -PDList -aALL | grep -E "Slot Number|Drive Temperature|Inquiry Data|Firmware state|S.M.A.R.T alert" | cat -// Slot Number: 0 -// Firmware state: Online, Spun Up -// Inquiry Data: TOSHIBA MG04SCA60EE 010356A0A001FWWB -// Drive Temperature :56C (132.80 F) -// Drive has flagged a S.M.A.R.T alert : No -// Slot Number: 1 -// Firmware state: Online, Spun Up -// Inquiry Data: TOSHIBA MG04SCA60EE 0103X6S0A0LRFWWB -// Drive Temperature :54C (129.20 F) -// Drive has flagged a S.M.A.R.T alert : No -// Slot Number: 2 -// Firmware state: Online, Spun Up -// Inquiry Data: TOSHIBA MG04SCA60EE 010356F0A00AFWWB -// Drive Temperature :51C (123.80 F) -// Drive has flagged a S.M.A.R.T alert : No -// Slot Number: 3 -// Firmware state: Online, Spun Up -// Inquiry Data: TOSHIBA MG04SCA60EE 0103X6Q0A0RZFWWB -// Drive Temperature :46C (114.80 F) -// Drive has flagged a S.M.A.R.T alert : No -// Slot Number: 4 -// Firmware state: Unconfigured(bad) -// Inquiry Data: TOSHIBA MG04SCA60EE 0103X6R0A09VFWWB -// Drive Temperature :49C (120.20 F) -// Drive has flagged a S.M.A.R.T alert : No -// Slot Number: 5 -// Firmware state: Online, Spun Up -// Inquiry Data: TOSHIBA MG04SCA60EE 0103X6R0A0JZFWWB -// Drive Temperature :53C (127.40 F) -// Drive has flagged a S.M.A.R.T alert : No -// Slot Number: 6 -// Firmware state: Online, Spun Up -// Inquiry Data: TOSHIBA MG04SCA60EE 010356F0A004FWWB -// Drive Temperature :56C (132.80 F) -// Drive has flagged a S.M.A.R.T alert : No -// Slot Number: 7 -// Firmware state: Online, Spun Up -// Inquiry Data: TOSHIBA MG04SCA60EE 01034680A01WFWWB -// Drive Temperature :58C (136.40 F) -// Drive has flagged a S.M.A.R.T alert : No - type DiskInfo struct { SlotNumber int DriveTemperature string diff --git a/message/cq_message.go b/message/cq_message.go index afddbf2..1bfb9ce 100644 --- a/message/cq_message.go +++ b/message/cq_message.go @@ -9,7 +9,7 @@ import ( // CQMessage 接口定义了所有 CQ 消息必须实现的方法 type CQMessage interface { ToCQString() string - ParseMessage(data string) error + // ParseMessage(data string) error } // RawMessage 用于初始解析 JSON 的通用结构 diff --git a/message/forward.go b/message/forward.go index f2be6d2..ce18d2f 100644 --- a/message/forward.go +++ b/message/forward.go @@ -78,23 +78,9 @@ func (msg *NodeMessage) ToCQString() string { return fmt.Sprintf("[CQ:node,user_id=%s,nickname=%s]", msg.Data.UserID, msg.Data.Nickname) } -func (msg *NodeMessage) ParseMessage(data string) error { - // 解析已有消息节点 - idRe := regexp.MustCompile(`\[CQ:node,id=(.*?)\]`) - matches := idRe.FindStringSubmatch(data) - if len(matches) == 2 { - msg.Data.ID = matches[1] - return nil - } - - // 解析自定义消息节点 - customRe := regexp.MustCompile(`\[CQ:node,user_id=(.*?),nickname=(.*?)\]`) - matches = customRe.FindStringSubmatch(data) - if len(matches) == 3 { - msg.Data.UserID = matches[1] - msg.Data.Nickname = matches[2] - return nil - } - - return fmt.Errorf("转发消息节点格式不正确") +func (msg *NodeMessage) ParseMessage(userId string, nickname string, content []any) *NodeMessage { + msg.Data.UserID = userId + msg.Data.Nickname = nickname + msg.Data.Content = content + return msg } diff --git a/message/text.go b/message/text.go index 58074de..dd8975e 100644 --- a/message/text.go +++ b/message/text.go @@ -32,7 +32,7 @@ func (msg *TextMessage) ToCQString() string { return msg.Data.Text } -func (msg *TextMessage) ParseMessage(data string) error { +func (msg *TextMessage) ParseMessage(data string) *TextMessage { msg.Data.Text = data - return nil + return msg }