From c3112342b88147d5ee7048cfef616e35de2c508f Mon Sep 17 00:00:00 2001 From: Cole_GG Date: Thu, 28 May 2020 14:16:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feature=20=E9=92=89=E9=92=89@=E4=BA=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/funcs.go | 5 +++-- corp/corp.go | 8 +++++--- cron/sender.go | 19 ++++++++++++----- dataobj/message.go | 51 ++++++++++++++++++++++++++++------------------ 4 files changed, 53 insertions(+), 30 deletions(-) diff --git a/config/funcs.go b/config/funcs.go index 4739875..4380d32 100644 --- a/config/funcs.go +++ b/config/funcs.go @@ -28,17 +28,18 @@ func InitLogger() { func Test(args []string) { c := Get() + var mobile []string dingClient := corp.New(c.DingTalk.Token, c.DingTalk.Mobiles,c.DingTalk.IsAtAll) if len(args) == 0 { - fmt.Println("mail address not given") + fmt.Println("token not given") os.Exit(1) } for i := 0; i < len(args); i++ { token := args[i] - err := dingClient.Send(token, fmt.Sprintf("test message from n9e at %v", time.Now())) + err := dingClient.Send(token, mobile, fmt.Sprintf("test message from n9e at %v", time.Now())) if err != nil { logger.Error("test send to %s fail: %v\n", args[i], err) } else { diff --git a/corp/corp.go b/corp/corp.go index 342fd22..e8e5212 100644 --- a/corp/corp.go +++ b/corp/corp.go @@ -49,9 +49,9 @@ func (c Client) GetToken() string { } // Send 发送信息 -func (c *Client) Send(token, msg string) error { +func (c *Client) Send(token string, mobile []string, msg string) error { - postData := c.generateData(msg) + postData := c.generateData(mobile, msg) if c.GetToken() != "" { // 配置了token 说明采用配置文件的token token = c.GetToken() @@ -112,7 +112,7 @@ func encodeJSON(v interface{}) ([]byte, error) { return buf.Bytes(), nil } -func (c *Client) generateData(msg string) interface{} { +func (c *Client) generateData(mobile []string, msg string) interface{} { postData := make(map[string]interface{}) postData["msgtype"] = "text" sendContext := make(map[string]interface{}) @@ -122,6 +122,8 @@ func (c *Client) generateData(msg string) interface{} { at := make(map[string]interface{}) if !c.IsAtAll && len(c.Mobiles) > 0 && c.token != "" { at["atMobiles"] = c.Mobiles // 根据手机号@指定人 + } else if len(mobile) > 0{ + at["atMobiles"] = mobile // 根据手机号@指定人 } else { c.IsAtAll = true } diff --git a/cron/sender.go b/cron/sender.go index 50a51cd..fc4c3a7 100644 --- a/cron/sender.go +++ b/cron/sender.go @@ -3,18 +3,17 @@ package cron import ( "bytes" "fmt" + "github.com/toolkits/pkg/runner" "path" "strings" "text/template" "time" - "github.com/toolkits/pkg/logger" - "github.com/toolkits/pkg/runner" - "github.com/n9e/dingtalk-sender/config" "github.com/n9e/dingtalk-sender/corp" "github.com/n9e/dingtalk-sender/dataobj" "github.com/n9e/dingtalk-sender/redisc" + "github.com/toolkits/pkg/logger" ) var ( @@ -53,19 +52,20 @@ func sendDing(message *dataobj.Message) { }() content := genContent(message) + mobile := pasteMobile(message) logger.Info("<-- hashid: %v -->", message.Event.HashId) logger.Infof("hashid: %d: endpoint: %s, metric: %s, tags: %s", message.Event.HashId, message.ReadableEndpoint, strings.Join(message.Metrics, ","), message.ReadableTags) if count := len(message.Tos); count > 0 { for _, tk := range message.Tos { - err := dingClient.Send(tk, content) + err := dingClient.Send(tk, mobile, content) if err != nil { logger.Errorf("send to %s fail: %v", message.Tos, err) } } } else if dingClient.GetToken() != "" { - err := dingClient.Send(dingClient.GetToken(), content) + err := dingClient.Send(dingClient.GetToken(),mobile, content) if err != nil { logger.Errorf("send to %s fail: %v", message.Tos, err) } @@ -83,6 +83,15 @@ func parseEtime(etime int64) string { return t.Format("2006-01-02 15:04:05") } +func pasteMobile(message *dataobj.Message) []string { + var MobilesStd []string + for _, v := range message.Event.RecvUser { + fmt.Printf("%s", v) + MobilesStd = append(MobilesStd, string(v.Phone)) + } + return MobilesStd +} + func genContent(message *dataobj.Message) string { fp := path.Join(runner.Cwd, "etc", "dingtalk.tpl") t, err := template.ParseFiles(fp) diff --git a/dataobj/message.go b/dataobj/message.go index 5632de3..c0e0a6b 100644 --- a/dataobj/message.go +++ b/dataobj/message.go @@ -17,27 +17,38 @@ type Message struct { } type Event struct { + Id int64 `json:"id"` + Sid int64 `json:"sid"` + Sname string `json:"sname"` + NodePath string `json:"node_path"` + Endpoint string `json:"endpoint"` + EndpointAlias string `json:"endpoint_alias"` + Priority int `json:"priority"` + EventType string `json:"event_type"` // alert|recovery + Category int `json:"category"` + Status uint16 `json:"status"` + HashId uint64 `json:"hashid" xorm:"hashid"` + Etime int64 `json:"etime"` + Value string `json:"value"` + Info string `json:"info"` + Created time.Time `json:"created" xorm:"created"` + Detail string `json:"detail"` + Users string `json:"users"` + Groups string `json:"groups"` + Nid int64 `json:"nid"` + NeedUpgrade int `json:"need_upgrade"` + AlertUpgrade string `json:"alert_upgrade"` + RecvUser []*RecvUser `json:"Recv_user_objs"` +} + +type RecvUser struct { Id int64 `json:"id"` - Sid int64 `json:"sid"` - Sname string `json:"sname"` - NodePath string `json:"node_path"` - Endpoint string `json:"endpoint"` - EndpointAlias string `json:"endpoint_alias"` - Priority int `json:"priority"` - EventType string `json:"event_type"` // alert|recovery - Category int `json:"category"` - Status uint16 `json:"status"` - HashId uint64 `json:"hashid" xorm:"hashid"` - Etime int64 `json:"etime"` - Value string `json:"value"` - Info string `json:"info"` - Created time.Time `json:"created" xorm:"created"` - Detail string `json:"detail"` - Users string `json:"users"` - Groups string `json:"groups"` - Nid int64 `json:"nid"` - NeedUpgrade int `json:"need_upgrade"` - AlertUpgrade string `json:"alert_upgrade"` + Username string `json:"username"` + Dispname string `json:"dispname"` + Phone string `json:"phone" xorm:"phone"` + Email string `json:"email"` + Im string `json:"im" xorm:"im"` + Is_root int `json:"is_root"` } type EventDetail struct { From cda4625d1003d7063f233a5103f336d23ed7db29 Mon Sep 17 00:00:00 2001 From: Cole_GG Date: Thu, 28 May 2020 14:39:50 +0800 Subject: [PATCH 2/2] feature dingding @ --- config/funcs.go | 5 ++--- corp/corp.go | 8 +++----- cron/sender.go | 19 +++++------------ dataobj/message.go | 51 ++++++++++++++++++---------------------------- 4 files changed, 30 insertions(+), 53 deletions(-) diff --git a/config/funcs.go b/config/funcs.go index 4380d32..4739875 100644 --- a/config/funcs.go +++ b/config/funcs.go @@ -28,18 +28,17 @@ func InitLogger() { func Test(args []string) { c := Get() - var mobile []string dingClient := corp.New(c.DingTalk.Token, c.DingTalk.Mobiles,c.DingTalk.IsAtAll) if len(args) == 0 { - fmt.Println("token not given") + fmt.Println("mail address not given") os.Exit(1) } for i := 0; i < len(args); i++ { token := args[i] - err := dingClient.Send(token, mobile, fmt.Sprintf("test message from n9e at %v", time.Now())) + err := dingClient.Send(token, fmt.Sprintf("test message from n9e at %v", time.Now())) if err != nil { logger.Error("test send to %s fail: %v\n", args[i], err) } else { diff --git a/corp/corp.go b/corp/corp.go index e8e5212..342fd22 100644 --- a/corp/corp.go +++ b/corp/corp.go @@ -49,9 +49,9 @@ func (c Client) GetToken() string { } // Send 发送信息 -func (c *Client) Send(token string, mobile []string, msg string) error { +func (c *Client) Send(token, msg string) error { - postData := c.generateData(mobile, msg) + postData := c.generateData(msg) if c.GetToken() != "" { // 配置了token 说明采用配置文件的token token = c.GetToken() @@ -112,7 +112,7 @@ func encodeJSON(v interface{}) ([]byte, error) { return buf.Bytes(), nil } -func (c *Client) generateData(mobile []string, msg string) interface{} { +func (c *Client) generateData(msg string) interface{} { postData := make(map[string]interface{}) postData["msgtype"] = "text" sendContext := make(map[string]interface{}) @@ -122,8 +122,6 @@ func (c *Client) generateData(mobile []string, msg string) interface{} { at := make(map[string]interface{}) if !c.IsAtAll && len(c.Mobiles) > 0 && c.token != "" { at["atMobiles"] = c.Mobiles // 根据手机号@指定人 - } else if len(mobile) > 0{ - at["atMobiles"] = mobile // 根据手机号@指定人 } else { c.IsAtAll = true } diff --git a/cron/sender.go b/cron/sender.go index fc4c3a7..50a51cd 100644 --- a/cron/sender.go +++ b/cron/sender.go @@ -3,17 +3,18 @@ package cron import ( "bytes" "fmt" - "github.com/toolkits/pkg/runner" "path" "strings" "text/template" "time" + "github.com/toolkits/pkg/logger" + "github.com/toolkits/pkg/runner" + "github.com/n9e/dingtalk-sender/config" "github.com/n9e/dingtalk-sender/corp" "github.com/n9e/dingtalk-sender/dataobj" "github.com/n9e/dingtalk-sender/redisc" - "github.com/toolkits/pkg/logger" ) var ( @@ -52,20 +53,19 @@ func sendDing(message *dataobj.Message) { }() content := genContent(message) - mobile := pasteMobile(message) logger.Info("<-- hashid: %v -->", message.Event.HashId) logger.Infof("hashid: %d: endpoint: %s, metric: %s, tags: %s", message.Event.HashId, message.ReadableEndpoint, strings.Join(message.Metrics, ","), message.ReadableTags) if count := len(message.Tos); count > 0 { for _, tk := range message.Tos { - err := dingClient.Send(tk, mobile, content) + err := dingClient.Send(tk, content) if err != nil { logger.Errorf("send to %s fail: %v", message.Tos, err) } } } else if dingClient.GetToken() != "" { - err := dingClient.Send(dingClient.GetToken(),mobile, content) + err := dingClient.Send(dingClient.GetToken(), content) if err != nil { logger.Errorf("send to %s fail: %v", message.Tos, err) } @@ -83,15 +83,6 @@ func parseEtime(etime int64) string { return t.Format("2006-01-02 15:04:05") } -func pasteMobile(message *dataobj.Message) []string { - var MobilesStd []string - for _, v := range message.Event.RecvUser { - fmt.Printf("%s", v) - MobilesStd = append(MobilesStd, string(v.Phone)) - } - return MobilesStd -} - func genContent(message *dataobj.Message) string { fp := path.Join(runner.Cwd, "etc", "dingtalk.tpl") t, err := template.ParseFiles(fp) diff --git a/dataobj/message.go b/dataobj/message.go index c0e0a6b..5632de3 100644 --- a/dataobj/message.go +++ b/dataobj/message.go @@ -17,38 +17,27 @@ type Message struct { } type Event struct { - Id int64 `json:"id"` - Sid int64 `json:"sid"` - Sname string `json:"sname"` - NodePath string `json:"node_path"` - Endpoint string `json:"endpoint"` - EndpointAlias string `json:"endpoint_alias"` - Priority int `json:"priority"` - EventType string `json:"event_type"` // alert|recovery - Category int `json:"category"` - Status uint16 `json:"status"` - HashId uint64 `json:"hashid" xorm:"hashid"` - Etime int64 `json:"etime"` - Value string `json:"value"` - Info string `json:"info"` - Created time.Time `json:"created" xorm:"created"` - Detail string `json:"detail"` - Users string `json:"users"` - Groups string `json:"groups"` - Nid int64 `json:"nid"` - NeedUpgrade int `json:"need_upgrade"` - AlertUpgrade string `json:"alert_upgrade"` - RecvUser []*RecvUser `json:"Recv_user_objs"` -} - -type RecvUser struct { Id int64 `json:"id"` - Username string `json:"username"` - Dispname string `json:"dispname"` - Phone string `json:"phone" xorm:"phone"` - Email string `json:"email"` - Im string `json:"im" xorm:"im"` - Is_root int `json:"is_root"` + Sid int64 `json:"sid"` + Sname string `json:"sname"` + NodePath string `json:"node_path"` + Endpoint string `json:"endpoint"` + EndpointAlias string `json:"endpoint_alias"` + Priority int `json:"priority"` + EventType string `json:"event_type"` // alert|recovery + Category int `json:"category"` + Status uint16 `json:"status"` + HashId uint64 `json:"hashid" xorm:"hashid"` + Etime int64 `json:"etime"` + Value string `json:"value"` + Info string `json:"info"` + Created time.Time `json:"created" xorm:"created"` + Detail string `json:"detail"` + Users string `json:"users"` + Groups string `json:"groups"` + Nid int64 `json:"nid"` + NeedUpgrade int `json:"need_upgrade"` + AlertUpgrade string `json:"alert_upgrade"` } type EventDetail struct {