优化钉钉@ 方式,允许关闭at (#917)

token_xxx?noat=1
This commit is contained in:
zheng 2022-04-19 15:14:18 +08:00 committed by GitHub
parent 5333fb8eab
commit 9ba1c2c32d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package sender
import (
"net/url"
"strings"
"time"
@ -38,24 +39,38 @@ func SendDingtalk(message DingtalkMessage) {
}
for i := 0; i < len(message.Tokens); i++ {
url := "https://oapi.dingtalk.com/robot/send?access_token=" + message.Tokens[i]
u, err := url.Parse(message.Tokens[i])
if err != nil {
logger.Errorf("dingtalk_sender: failed to parse error=%v", err)
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
logger.Errorf("dingtalk_sender: failed to parse query error=%v", err)
}
ur := "https://oapi.dingtalk.com/robot/send?access_token=" + u.Path
body := dingtalk{
Msgtype: "markdown",
Markdown: dingtalkMarkdown{
Title: message.Title,
Text: message.Text + " " + strings.Join(ats, " "),
},
At: dingtalkAt{
AtMobiles: message.AtMobiles,
IsAtAll: false,
Text: message.Text,
},
}
res, code, err := poster.PostJSON(url, time.Second*5, body)
if v.Get("noat") != "1" {
body.Markdown.Text = message.Text + " " + strings.Join(ats, " ")
body.At = dingtalkAt{
AtMobiles: message.AtMobiles,
IsAtAll: false,
}
}
res, code, err := poster.PostJSON(ur, time.Second*5, body)
if err != nil {
logger.Errorf("dingtalk_sender: result=fail url=%s code=%d error=%v response=%s", url, code, err, string(res))
logger.Errorf("dingtalk_sender: result=fail url=%s code=%d error=%v response=%s", ur, code, err, string(res))
} else {
logger.Infof("dingtalk_sender: result=succ url=%s code=%d response=%s", url, code, string(res))
logger.Infof("dingtalk_sender: result=succ url=%s code=%d response=%s", ur, code, string(res))
}
}
}