forked from StackExchange/dnscontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
46 lines (39 loc) · 1.06 KB
/
slack.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
package notifications
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func init() {
initers = append(initers, func(cfg map[string]string) Notifier {
if url, ok := cfg["slack_url"]; ok {
notifier := &slackNotifier{
URL: url,
}
return notifier
}
return nil
})
}
// slackNotifier sends notifications to slack or mattermost
type slackNotifier struct {
URL string
}
func (s *slackNotifier) Notify(domain, provider, msg string, err error, preview bool) {
var payload struct {
Username string `json:"username"`
Text string `json:"text"`
}
payload.Username = "DNSControl"
if preview {
payload.Text = fmt.Sprintf(`**Preview: %s[%s] -** %s`, domain, provider, msg)
} else if err != nil {
payload.Text = fmt.Sprintf(`**ERROR running correction on %s[%s] -** (%s) Error: %s`, domain, provider, msg, err)
} else {
payload.Text = fmt.Sprintf(`Successfully ran correction for **%s[%s]** - %s`, domain, provider, msg)
}
json, _ := json.Marshal(payload)
http.Post(s.URL, "text/json", bytes.NewReader(json))
}
func (s *slackNotifier) Done() {}