-
Notifications
You must be signed in to change notification settings - Fork 150
add dingtalk alarm #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maybachv5
wants to merge
1
commit into
710leo:master
Choose a base branch
from
maybachv5:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,44 +25,52 @@ import ( | |
) | ||
|
||
var ( | ||
SmsWorkerChan chan int | ||
MailWorkerChan chan int | ||
WeChatWorkerChan chan int | ||
requestError = errors.New("request error,check url or network") | ||
SmsWorkerChan chan int | ||
MailWorkerChan chan int | ||
WeChatWorkerChan chan int | ||
DingTalkWorkerChan chan int | ||
requestError = errors.New("request error,check url or network") | ||
) | ||
|
||
const ( | ||
// 发送消息使用导的url | ||
sendUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" | ||
// 获取token使用导的url | ||
getToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" | ||
// 钉钉webhook | ||
webHook = "https://oapi.dingtalk.com/robot/send?access_token=" | ||
) | ||
|
||
func Init() { | ||
workerConfig := g.Config.Worker | ||
SmsWorkerChan = make(chan int, workerConfig.Sms) | ||
MailWorkerChan = make(chan int, workerConfig.Mail) | ||
WeChatWorkerChan = make(chan int, workerConfig.WeChat) | ||
DingTalkWorkerChan = make(chan int, workerConfig.DingTalk) | ||
} | ||
|
||
func SendEvent(event *dataobj.Event) { | ||
mail := make([]string, 0) | ||
sms := make([]string, 0) | ||
weChat := make([]string, 0) | ||
dingTalk := make([]string, 0) | ||
users := getUsers(event.StrategyId) | ||
|
||
mailContent := BuildMail(event) | ||
smsContent := BuildSms(event) | ||
weChatContent := BuildWeChat(event) | ||
dingTalkContent := BuildDingTalk(event) | ||
for _, user := range users { | ||
mail = append(mail, user.Email) | ||
sms = append(sms, user.Phone) | ||
weChat = append(weChat, user.Wechat) | ||
dingTalk = append(dingTalk, user.Phone) | ||
} | ||
|
||
WriteSms(sms, smsContent) | ||
WriteMail(mail, smsContent, mailContent) | ||
WriteWeChat(weChat, weChatContent) | ||
WriteDingTalk(dingTalk, dingTalkContent) | ||
} | ||
|
||
func sendSms(phone string, sms string) { | ||
|
@@ -135,6 +143,28 @@ func sendWeChat(weChat *g.WeChat) { | |
} | ||
} | ||
|
||
func sendDingTalk(dingTalk *g.DingTalk) { | ||
defer func() { | ||
<-DingTalkWorkerChan | ||
}() | ||
var msg = dingTalkMsg{ | ||
MsgType: "markdown", | ||
MarkDown: map[string]string{"title": "站点监控", "text": dingTalk.Content}, | ||
} | ||
buf, err := json.Marshal(msg) | ||
if err != nil { | ||
log.Println(err, "marshal dingTalk msg error") | ||
} | ||
err = SendDingTalk(g.Config.DingTalk.AccessToken, buf) | ||
if err != nil { | ||
log.Println(err, "send dingTalk") | ||
} else { | ||
log.Println("==dingTalk==>>>>", dingTalk) | ||
log.Println("<<<<==dingTalk==", string(buf)) | ||
} | ||
|
||
} | ||
|
||
func getUsers(sid int64) []*dataobj.User { | ||
var usersResp api.UsersResponse | ||
var users []*dataobj.User | ||
|
@@ -169,7 +199,13 @@ type weChatMsg struct { | |
Safe int `json:"safe"` | ||
} | ||
|
||
// 定义微信错误返回结构体 | ||
// 定义钉钉markdown消息结构体 | ||
type dingTalkMsg struct { | ||
MsgType string `json:"msgtype"` | ||
MarkDown map[string]string `json:"markdown"` | ||
} | ||
|
||
// 定义微信/钉钉错误返回结构体 | ||
type sendMsgError struct { | ||
ErrCode int `json:"errcode` | ||
ErrMsg string `json:"errmsg"` | ||
|
@@ -219,3 +255,24 @@ func SendMsg(AccessToken string, msgBody []byte) error { | |
} | ||
return nil | ||
} | ||
|
||
// 发送钉钉 | ||
func SendDingTalk(AccessToken string, msgBody []byte) error { | ||
body := bytes.NewBuffer(msgBody) | ||
resp, err := http.Post(webHook+AccessToken, "application/json", body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的 |
||
if resp.StatusCode != 200 { | ||
return requestError | ||
} | ||
buf, _ := ioutil.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
var e sendMsgError | ||
err = json.Unmarshal(buf, &e) | ||
if err != nil { | ||
return err | ||
} | ||
if e.ErrCode != 0 && e.ErrMsg != "ok" { | ||
return errors.New(string(buf)) | ||
} | ||
return nil | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dingtalk.enabled 默认为false比较好