refactor: 重构 GetLoginAccountInfo 函数,移除 actionManager 依赖,并更新相关调用;修改 sendAlertMessage 函数为发送合并转发

This commit is contained in:
lixiangwuxian 2025-04-12 01:13:22 +08:00
parent 8af78f8422
commit 46a82e1fef
6 changed files with 45 additions and 78 deletions

View File

@ -57,7 +57,7 @@ func GetGroupList() ([]model.Group, error) {
return groupListResponse.Data, nil 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) fullURL := fmt.Sprintf("http://%s%s", config.ConfigManager.GetConfig().Management.NapcatHttpSrv, constants.GET_LOGIN_INFO)
response, err := http.Post(fullURL, "application/json", nil) response, err := http.Post(fullURL, "application/json", nil)

View File

@ -35,7 +35,7 @@ func help(msg model.Message) *model.Reply {
textMsg := message.NewTextMessage() textMsg := message.NewTextMessage()
textMsg.Data.Text = helpInfo textMsg.Data.Text = helpInfo
nodeMsg := message.NewNodeMessage() nodeMsg := message.NewNodeMessage()
loginAccountInfo, err := action.ActionManager.GetLoginAccountInfo() loginAccountInfo, err := action.GetLoginAccountInfo()
if err != nil { if err != nil {
log.Println("GetLoginAccountInfo error:", err) log.Println("GetLoginAccountInfo error:", err)
return nil return nil

View File

@ -12,6 +12,7 @@ import (
"git.lxtend.com/qqbot/config" "git.lxtend.com/qqbot/config"
"git.lxtend.com/qqbot/constants" "git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler" "git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/message"
"git.lxtend.com/qqbot/model" "git.lxtend.com/qqbot/model"
) )
@ -89,23 +90,36 @@ func checkRaidStatus() {
} }
// 发送告警消息到管理群 // 发送告警消息到管理群
sendAlertMessage(sb.String()) sendAlertMessage([]string{sb.String()})
} else { } else {
log.Println("RAID状态检查完成未发现异常") log.Println("RAID状态检查完成未发现异常")
} }
} }
// 发送告警消息 // 发送告警消息
func sendAlertMessage(alertMsg string) { func sendAlertMessage(alertMsgs []string) {
reportGroupID := config.ConfigManager.GetConfig().Management.ReportGroup reportGroupID := config.ConfigManager.GetConfig().Management.ReportGroup
if reportGroupID == 0 { if reportGroupID == 0 {
log.Println("未配置管理群,告警消息无法发送:", alertMsg) log.Println("未配置管理群,告警消息无法发送:", alertMsgs)
return return
} }
action.ActionManager.SendMsg(&model.Reply{ nodes := []message.NodeMessage{}
ReplyMsg: alertMsg, 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, ReferOriginMsg: false,
FromMsg: model.Message{ FromMsg: model.Message{
GroupInfo: model.GroupInfo{ GroupInfo: model.GroupInfo{
@ -128,60 +142,27 @@ func RaidHandler(msg model.Message) (reply *model.Reply) {
} }
} }
sb := strings.Builder{} nodes := []message.NodeMessage{}
sb.WriteString("阵列信息:\n") 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 { 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{ action.ActionManager.SendForward(&model.Reply{
FromMsg: msg, ReplyMsg: nodes,
ReplyMsg: sb.String(),
ReferOriginMsg: false, 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 { type DiskInfo struct {
SlotNumber int SlotNumber int

View File

@ -9,7 +9,7 @@ import (
// CQMessage 接口定义了所有 CQ 消息必须实现的方法 // CQMessage 接口定义了所有 CQ 消息必须实现的方法
type CQMessage interface { type CQMessage interface {
ToCQString() string ToCQString() string
ParseMessage(data string) error // ParseMessage(data string) error
} }
// RawMessage 用于初始解析 JSON 的通用结构 // RawMessage 用于初始解析 JSON 的通用结构

View File

@ -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) return fmt.Sprintf("[CQ:node,user_id=%s,nickname=%s]", msg.Data.UserID, msg.Data.Nickname)
} }
func (msg *NodeMessage) ParseMessage(data string) error { func (msg *NodeMessage) ParseMessage(userId string, nickname string, content []any) *NodeMessage {
// 解析已有消息节点 msg.Data.UserID = userId
idRe := regexp.MustCompile(`\[CQ:node,id=(.*?)\]`) msg.Data.Nickname = nickname
matches := idRe.FindStringSubmatch(data) msg.Data.Content = content
if len(matches) == 2 { return msg
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("转发消息节点格式不正确")
} }

View File

@ -32,7 +32,7 @@ func (msg *TextMessage) ToCQString() string {
return msg.Data.Text return msg.Data.Text
} }
func (msg *TextMessage) ParseMessage(data string) error { func (msg *TextMessage) ParseMessage(data string) *TextMessage {
msg.Data.Text = data msg.Data.Text = data
return nil return msg
} }