分离钉钉消息到通知管理
This commit is contained in:
11
package/message/consts.go
Normal file
11
package/message/consts.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package message
|
||||
|
||||
type MessageType int32
|
||||
|
||||
const (
|
||||
DingTalk MessageType = iota
|
||||
Wechat
|
||||
Email
|
||||
Sms
|
||||
Voice
|
||||
)
|
||||
43
package/message/drive/dingtalk.go
Normal file
43
package/message/drive/dingtalk.go
Normal 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 += "×tamp=" + 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
|
||||
}
|
||||
16
package/message/message.go
Normal file
16
package/message/message.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user