Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
57018738
Unverified
Commit
57018738
authored
Mar 08, 2019
by
Torkel Ödegaard
Committed by
GitHub
Mar 08, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13825 from athurg/optimize_dingding_alert_message
Optimize dingding alert message
parents
d104ee1c
70b23ab7
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
9 deletions
+58
-9
pkg/services/alerting/notifiers/dingding.go
+58
-9
No files found.
pkg/services/alerting/notifiers/dingding.go
View file @
57018738
package
notifiers
import
(
"fmt"
"net/url"
"strings"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
...
...
@@ -8,19 +12,26 @@ import (
"github.com/grafana/grafana/pkg/services/alerting"
)
const
DefaultDingdingMsgType
=
"link"
const
DingdingOptionsTemplate
=
`
<h3 class="page-heading">DingDing settings</h3>
<div class="gf-form">
<span class="gf-form-label width-10">Url</span>
<input type="text" required class="gf-form-input max-width-70" ng-model="ctrl.model.settings.url" placeholder="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx"></input>
</div>
<div class="gf-form">
<span class="gf-form-label width-10">MessageType</span>
<select class="gf-form-input max-width-14" ng-model="ctrl.model.settings.msgType" ng-options="s for s in ['link','actionCard']" ng-init="ctrl.model.settings.msgType=ctrl.model.settings.msgType || '`
+
DefaultDingdingMsgType
+
`'"></select>
</div>
`
func
init
()
{
alerting
.
RegisterNotifier
(
&
alerting
.
NotifierPlugin
{
Type
:
"dingding"
,
Name
:
"DingDing"
,
Description
:
"Sends HTTP POST request to DingDing"
,
Factory
:
NewDingDingNotifier
,
OptionsTemplate
:
`
<h3 class="page-heading">DingDing settings</h3>
<div class="gf-form">
<span class="gf-form-label width-10">Url</span>
<input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url" placeholder="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx"></input>
</div>
`
,
OptionsTemplate
:
DingdingOptionsTemplate
,
})
}
...
...
@@ -31,8 +42,11 @@ func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error)
return
nil
,
alerting
.
ValidationError
{
Reason
:
"Could not find url property in settings"
}
}
msgType
:=
model
.
Settings
.
Get
(
"msgType"
)
.
MustString
(
DefaultDingdingMsgType
)
return
&
DingDingNotifier
{
NotifierBase
:
NewNotifierBase
(
model
),
MsgType
:
msgType
,
Url
:
url
,
log
:
log
.
New
(
"alerting.notifier.dingding"
),
},
nil
...
...
@@ -40,6 +54,7 @@ func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error)
type
DingDingNotifier
struct
{
NotifierBase
MsgType
string
Url
string
log
log
.
Logger
}
...
...
@@ -52,6 +67,16 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
this
.
log
.
Error
(
"Failed to get messageUrl"
,
"error"
,
err
,
"dingding"
,
this
.
Name
)
messageUrl
=
""
}
q
:=
url
.
Values
{
"pc_slide"
:
{
"false"
},
"url"
:
{
messageUrl
},
}
// Use special link to auto open the message url outside of Dingding
// Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=385&articleId=104972&docType=1#s9
messageUrl
=
"dingtalk://dingtalkclient/page/link?"
+
q
.
Encode
()
this
.
log
.
Info
(
"messageUrl:"
+
messageUrl
)
message
:=
evalContext
.
Rule
.
Message
...
...
@@ -61,7 +86,28 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
message
=
title
}
bodyJSON
,
err
:=
simplejson
.
NewJson
([]
byte
(
`{
for
i
,
match
:=
range
evalContext
.
EvalMatches
{
message
+=
fmt
.
Sprintf
(
"
\\
n%2d. %s: %s"
,
i
+
1
,
match
.
Metric
,
match
.
Value
)
}
var
bodyStr
string
if
this
.
MsgType
==
"actionCard"
{
// Embed the pic into the markdown directly because actionCard doesn't have a picUrl field
if
picUrl
!=
""
{
message
=
"![]("
+
picUrl
+
")
\\
n
\\
n"
+
message
}
bodyStr
=
`{
"msgtype": "actionCard",
"actionCard": {
"text": "`
+
strings
.
Replace
(
message
,
`"`
,
"'"
,
-
1
)
+
`",
"title": "`
+
strings
.
Replace
(
title
,
`"`
,
"'"
,
-
1
)
+
`",
"singleTitle": "More",
"singleURL": "`
+
messageUrl
+
`"
}
}`
}
else
{
bodyStr
=
`{
"msgtype": "link",
"link": {
"text": "`
+
message
+
`",
...
...
@@ -69,7 +115,10 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
"picUrl": "`
+
picUrl
+
`",
"messageUrl": "`
+
messageUrl
+
`"
}
}`
))
}`
}
bodyJSON
,
err
:=
simplejson
.
NewJson
([]
byte
(
bodyStr
))
if
err
!=
nil
{
this
.
log
.
Error
(
"Failed to create Json data"
,
"error"
,
err
,
"dingding"
,
this
.
Name
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment