diff --git a/docs/notifications.md b/docs/notifications.md index 33dd885b2..29de2257c 100644 --- a/docs/notifications.md +++ b/docs/notifications.md @@ -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. \ No newline at end of file diff --git a/pkg/notifications/slack.go b/pkg/notifications/slack.go new file mode 100644 index 000000000..275c2e44a --- /dev/null +++ b/pkg/notifications/slack.go @@ -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() {}