From 889ed75668e767402a2d0c33117f52c8e4441fed Mon Sep 17 00:00:00 2001 From: Mike Cochrane Date: Sat, 15 Aug 2020 04:23:54 +1200 Subject: [PATCH] Notifications: Add support for Microsoft Teams (#812) --- docs/notifications.md | 12 +++++++-- pkg/notifications/teams.go | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 pkg/notifications/teams.go diff --git a/docs/notifications.md b/docs/notifications.md index 29de2257c..24130af7e 100644 --- a/docs/notifications.md +++ b/docs/notifications.md @@ -21,7 +21,8 @@ Notifications are set up in your credentials json file. They will use the `notif ... } , "notifications":{ - "slack_url": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" + "slack_url": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX", + "teams_url": "https://outlook.office.com/webhook/00000000-0000-0000-0000-000000000000@00000000-0000-0000-0000-000000000000/IncomingWebhook/00000000000000000000000000000000/00000000-0000-0000-0000-000000000000" } ``` @@ -36,6 +37,13 @@ Please see the [Slack documentation](https://api.slack.com/messaging/webhooks) o Configure `slack_url` to this webhook. Mattermost works as well, as they share the same api, +### Microsoft Teams + +If you want to use the Teams integration, you need to create a webhook in Teams. +Please see the [Teams documentation](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook#add-an-incoming-webhook-to-a-teams-channel) + +Configure `teams_url` to this webhook. + ### Bonfire This is stack overflow's built in chat system. This is probably not useful for most people. @@ -44,7 +52,7 @@ Configure `bonfire_url` to be the full url including room and api key. ## Future work -Yes, this seems pretty limited right now in what it can do. We didn't want to add a bunch of notification types if nobody was going to use them. The good news is, it should +Yes, this seems pretty limited right now in what it can do. We didn't want to add a bunch of notification types if nobody was going to use them. The good news is, it should be really simple to add more. We gladly welcome any PRs with new notification destinations. Some easy possibilities: - Email diff --git a/pkg/notifications/teams.go b/pkg/notifications/teams.go new file mode 100644 index 000000000..d605566a7 --- /dev/null +++ b/pkg/notifications/teams.go @@ -0,0 +1,52 @@ +package notifications + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "strings" +) + +func init() { + initers = append(initers, func(cfg map[string]string) Notifier { + url, ok := cfg["slack_url"] + if !ok { + return nil + } + + notifier := &slackNotifier{ + URL: url, + } + return notifier + }) +} + +// teamsNotifier sends notifications to teams or mattermost +type teamsNotifier struct { + URL string +} + +func (s *teamsNotifier) Notify(domain, provider, msg string, err error, preview bool) { + var payload struct { + Username string `json:"username"` + Text string `json:"text"` + } + payload.Username = "DnsControl" + + // Format changes as 'preformated' text + msg = strings.ReplaceAll(msg, "\n", "\n ") + + if preview { + payload.Text = fmt.Sprintf("**DnsControl Preview %s**\n%s", domain, msg) + } else if err != nil { + payload.Text = fmt.Sprintf("**DnsControl Error Making Changes %s**\n%s\nError: %s", domain, msg, err) + } else { + payload.Text = fmt.Sprintf("**DnsControl Successfully Changed %s**\n%s", domain, msg) + } + + json, _ := json.Marshal(payload) + http.Post(s.URL, "text/json", bytes.NewReader(json)) +} + +func (s *teamsNotifier) Done() {}