Added slack notifications (#749)

* Added slack notification

* Added slack notification to doc.

* Send notifications as single message & updated doc. example

* Remove not needed variable
This commit is contained in:
Jan-Philipp Benecke 2020-05-29 15:26:48 +02:00 committed by GitHub
parent e230c7aab1
commit d3a90f0a2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 2 deletions

View file

@ -21,7 +21,7 @@ Notifications are set up in your credentials json file. They will use the `notif
...
} ,
"notifications":{
"bonfire_url": "https://chat.meta.stackexchange.com/feeds/rooms/123?key=xyz"
"slack_url": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
}
```
@ -29,6 +29,13 @@ You also must run `dnscontrol preview` or `dnscontrol push` with the `-notify` f
## Notification types
### Slack/Mattermost
If you want to use the Slack integration, you need to create a webhook in Slack.
Please see the [Slack documentation](https://api.slack.com/messaging/webhooks) or the [Mattermost documentation](https://docs.mattermost.com/developer/webhooks-incoming.html)
Configure `slack_url` to this webhook. Mattermost works as well, as they share the same api,
### Bonfire
This is stack overflow's built in chat system. This is probably not useful for most people.
@ -41,7 +48,6 @@ Yes, this seems pretty limited right now in what it can do. We didn't want to ad
be really simple to add more. We gladly welcome any PRs with new notification destinations. Some easy possibilities:
- Email
- Slack
- Generic Webhooks
Please update this documentation if you add anything.

View file

@ -0,0 +1,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() {}