Skip to content

Commit

Permalink
update 钉钉@人
Browse files Browse the repository at this point in the history
  • Loading branch information
Cole_GG authored and Cole_GG committed May 28, 2020
1 parent 3e19b0d commit 287fd8c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 30 deletions.
5 changes: 3 additions & 2 deletions config/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions corp/corp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{})
Expand All @@ -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
}
Expand Down
19 changes: 14 additions & 5 deletions cron/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
51 changes: 31 additions & 20 deletions dataobj/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 287fd8c

Please sign in to comment.