分离钉钉消息到通知管理

This commit is contained in:
ayflying
2025-03-04 17:24:10 +08:00
parent 6988e99717
commit 6eeb5a57cd
5 changed files with 79 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ package systemCron
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/ayflying/utility_go/package/message"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient" "github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/os/gtime"
@@ -28,14 +29,14 @@ func (s *sSystemCron) Guardian(DingTalkWebHook string) {
defer get.Close() defer get.Close()
if err != nil { if err != nil {
s.DingTalk(DingTalkWebHook, fmt.Sprintf("监控报警:服务端访问失败 (%v 服务器),err=%v", v.Name, err)) message.New(message.DingTalk, DingTalkWebHook).Send(fmt.Sprintf("监控报警:服务端访问失败 (%v 服务器),err=%v", v.Name, err))
} else if get.StatusCode != 200 { } else if get.StatusCode != 200 {
s.DingTalk(DingTalkWebHook, fmt.Sprintf("监控报警:服务端访问失败 (%v 服务器),code=%v,err=%v", v.Name, get.StatusCode, err)) message.New(message.DingTalk, DingTalkWebHook).Send(fmt.Sprintf("监控报警:服务端访问失败 (%v 服务器),code=%v,err=%v", v.Name, get.StatusCode, err))
} else { } else {
var ststus Status var ststus Status
err = json.Unmarshal(get.ReadAll(), &ststus) err = json.Unmarshal(get.ReadAll(), &ststus)
if ststus.Code != 0 { if ststus.Code != 0 {
s.DingTalk(DingTalkWebHook, fmt.Sprintf("监控报警:服务端访问失败 (%v 服务器),msg=%v", v.Name, ststus.Message)) message.New(message.DingTalk, DingTalkWebHook).Send(fmt.Sprintf("监控报警:服务端访问失败 (%v 服务器),msg=%v", v.Name, ststus.Message))
} }
} }
} }
@@ -46,7 +47,11 @@ func (s *sSystemCron) Guardian(DingTalkWebHook string) {
// @Description: 向指定的钉钉机器人发送消息。 // @Description: 向指定的钉钉机器人发送消息。
// @receiver s: 系统定时任务结构体指针。 // @receiver s: 系统定时任务结构体指针。
// @param value: 要发送的消息内容。 // @param value: 要发送的消息内容。
// Deprecated: Use message.New(message.DingTalk, DingTalkWebHook).Send(value)
func (s *sSystemCron) DingTalk(DingTalkWebHook string, value string) (res *gclient.Response) { func (s *sSystemCron) DingTalk(DingTalkWebHook string, value string) (res *gclient.Response) {
message.New(message.DingTalk, DingTalkWebHook).Send(value)
return
// 从配置中获取发送者名称 // 从配置中获取发送者名称
name, _ := g.Cfg().Get(ctx, "name") name, _ := g.Cfg().Get(ctx, "name")

11
package/message/consts.go Normal file
View File

@@ -0,0 +1,11 @@
package message
type MessageType int32
const (
DingTalk MessageType = iota
Wechat
Email
Sms
Voice
)

View File

@@ -0,0 +1,43 @@
package drive
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gtime"
)
type Mod struct {
DingTalkWebHook string
}
func Load(webHook string) *Mod {
return &Mod{
DingTalkWebHook: webHook,
}
}
func (m Mod) Send(value string) {
ctx := gctx.New()
// 从配置中获取发送者名称
name, _ := g.Cfg().Get(ctx, "name")
// 定义钉钉机器人发送消息的URL其中access_token为固定值
url := m.DingTalkWebHook
url += "&timestamp=" + gtime.Now().TimestampMilliStr()
// 使用goroutine异步发送消息
var post = g.Map{
"msgtype": "text",
"text": g.Map{
"content": "通知姬 " + name.String() + "\n" + value + "\n" + gtime.Now().String(),
},
}
// 构建发送的消息体,包含消息类型和内容
_, err := g.Client().Discovery(nil).ContentJson().Post(ctx, url, post)
if err != nil {
g.Log().Info(ctx, err)
}
return
}

View File

@@ -0,0 +1,16 @@
package message
import "github.com/ayflying/utility_go/package/message/drive"
type MessageV1 interface {
Send(value string)
}
func New(typ MessageType, host string) MessageV1 {
switch typ {
case DingTalk:
return drive.Load(host)
}
return nil
}

View File

@@ -18,6 +18,7 @@ type (
// @Description: 向指定的钉钉机器人发送消息。 // @Description: 向指定的钉钉机器人发送消息。
// @receiver s: 系统定时任务结构体指针。 // @receiver s: 系统定时任务结构体指针。
// @param value: 要发送的消息内容。 // @param value: 要发送的消息内容。
// Deprecated: Use message.New(message.DingTalk, DingTalkWebHook).Send(value)
DingTalk(DingTalkWebHook string, value string) (res *gclient.Response) DingTalk(DingTalkWebHook string, value string) (res *gclient.Response)
ReadLog() ReadLog()
// AddCron 添加一个定时任务到相应的调度列表中。 // AddCron 添加一个定时任务到相应的调度列表中。