forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threema.go
159 lines (144 loc) · 5.16 KB
/
threema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package notifiers
import (
"fmt"
"net/url"
"strings"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
var (
threemaGwBaseURL = "https://msgapi.threema.ch/%s"
)
func init() {
alerting.RegisterNotifier(&alerting.NotifierPlugin{
Type: "threema",
Name: "Threema Gateway",
Description: "Sends notifications to Threema using the Threema Gateway",
Factory: NewThreemaNotifier,
OptionsTemplate: `
<h3 class="page-heading">Threema Gateway settings</h3>
<p>
Notifications can be configured for any Threema Gateway ID of type
"Basic". End-to-End IDs are not currently supported.
</p>
<p>
The Threema Gateway ID can be set up at
<a href="https://gateway.threema.ch/" target="_blank" rel="noopener noreferrer">https://gateway.threema.ch/</a>.
</p>
<div class="gf-form">
<span class="gf-form-label width-14">Gateway ID</span>
<input type="text" required maxlength="8" pattern="\*[0-9A-Z]{7}"
class="gf-form-input max-width-14"
ng-model="ctrl.model.settings.gateway_id"
placeholder="*3MAGWID">
</input>
<info-popover mode="right-normal">
Your 8 character Threema Gateway ID (starting with a *)
</info-popover>
</div>
<div class="gf-form">
<span class="gf-form-label width-14">Recipient ID</span>
<input type="text" required maxlength="8" pattern="[0-9A-Z]{8}"
class="gf-form-input max-width-14"
ng-model="ctrl.model.settings.recipient_id"
placeholder="YOUR3MID">
</input>
<info-popover mode="right-normal">
The 8 character Threema ID that should receive the alerts
</info-popover>
</div>
<div class="gf-form">
<span class="gf-form-label width-14">API Secret</span>
<input type="text" required
class="gf-form-input max-width-24"
ng-model="ctrl.model.settings.api_secret">
</input>
<info-popover mode="right-normal">
Your Threema Gateway API secret
</info-popover>
</div>
`,
})
}
type ThreemaNotifier struct {
NotifierBase
GatewayID string
RecipientID string
APISecret string
log log.Logger
}
func NewThreemaNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
gatewayID := model.Settings.Get("gateway_id").MustString()
recipientID := model.Settings.Get("recipient_id").MustString()
apiSecret := model.Settings.Get("api_secret").MustString()
// Validation
if gatewayID == "" {
return nil, alerting.ValidationError{Reason: "Could not find Threema Gateway ID in settings"}
}
if !strings.HasPrefix(gatewayID, "*") {
return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must start with a *"}
}
if len(gatewayID) != 8 {
return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must be 8 characters long"}
}
if recipientID == "" {
return nil, alerting.ValidationError{Reason: "Could not find Threema Recipient ID in settings"}
}
if len(recipientID) != 8 {
return nil, alerting.ValidationError{Reason: "Invalid Threema Recipient ID: Must be 8 characters long"}
}
if apiSecret == "" {
return nil, alerting.ValidationError{Reason: "Could not find Threema API secret in settings"}
}
return &ThreemaNotifier{
NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
GatewayID: gatewayID,
RecipientID: recipientID,
APISecret: apiSecret,
log: log.New("alerting.notifier.threema"),
}, nil
}
func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error {
notifier.log.Info("Sending alert notification from", "threema_id", notifier.GatewayID)
notifier.log.Info("Sending alert notification to", "threema_id", notifier.RecipientID)
metrics.M_Alerting_Notification_Sent_Threema.Inc(1)
// Set up basic API request data
data := url.Values{}
data.Set("from", notifier.GatewayID)
data.Set("to", notifier.RecipientID)
data.Set("secret", notifier.APISecret)
// Build message
message := fmt.Sprintf("%s\n\n*State:* %s\n*Message:* %s\n",
evalContext.GetNotificationTitle(), evalContext.Rule.Name, evalContext.Rule.Message)
ruleURL, err := evalContext.GetRuleUrl()
if err == nil {
message = message + fmt.Sprintf("*URL:* %s\n", ruleURL)
}
if evalContext.ImagePublicUrl != "" {
message = message + fmt.Sprintf("*Image:* %s\n", evalContext.ImagePublicUrl)
}
data.Set("text", message)
// Prepare and send request
url := fmt.Sprintf(threemaGwBaseURL, "send_simple")
body := data.Encode()
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}
cmd := &m.SendWebhookSync{
Url: url,
Body: body,
HttpMethod: "POST",
HttpHeader: headers,
}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
notifier.log.Error("Failed to send webhook", "error", err, "webhook", notifier.Name)
return err
}
return nil
}