-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsTeamsNotificationsModule.go
102 lines (89 loc) · 2.64 KB
/
MsTeamsNotificationsModule.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
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
"log"
"net/http"
"os"
"text/template"
)
type MsTeamsNotificationsModule struct {
webhookUrl string
}
func NewMsTeamsNotificationsModule() *MsTeamsNotificationsModule {
return new(MsTeamsNotificationsModule)
}
func (t *MsTeamsNotificationsModule) notify(notifications []Notification) {
fmt.Printf("Sending MS Teams notifications\n")
const jsonBodyTemplate = `
{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"summary": "{{.Title}}",
"sections": [
{
"activityTitle": "{{.Title}}",
"activityImage": "{{.IconUrl}}",
"Text": "{{.Text}}",
"markdown": true
}
]
}
`
bodyTemplate := template.Must(template.New("postBody").Parse(jsonBodyTemplate))
for _, notification := range notifications {
client := &http.Client{}
var tpl bytes.Buffer
if err := bodyTemplate.Execute(&tpl, notification); err != nil {
log.Fatalf("Could not evaluate json template for teams webhook @ %s: %v", t.webhookUrl, err)
}
req, err := http.NewRequest("POST", t.webhookUrl, &tpl)
if err != nil {
log.Fatalf("Could not create POST request for teams webhook @ %s: %v", t.webhookUrl, err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Could not POST notification to teams webhook @ %s: %v", t.webhookUrl, err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Fatalf("Could not close response body: %v", err)
}
}()
if resp.StatusCode != http.StatusOK {
log.Fatalf("Could not POST notification to teams webhook @ %s (HTTP %v)", t.webhookUrl, resp.StatusCode)
}
}
}
func (t *MsTeamsNotificationsModule) Name() string {
return "MsTeamsNotifications"
}
func (t *MsTeamsNotificationsModule) Description() string {
return "Sends notifications about changes in other modules to via MS Teams"
}
func (t *MsTeamsNotificationsModule) CanBeDisabled() bool {
return true
}
func (t *MsTeamsNotificationsModule) UpdateSettings() {
configKey := t.Name() + ".webhook-url"
if !viper.IsSet(configKey) {
log.Fatalf("Missing configuration key `%s` (eg. 'https://my-company.webhook.office.com/webhookb2/webhook-id')", configKey)
}
t.webhookUrl = viper.GetString(configKey)
}
func (t *MsTeamsNotificationsModule) NeedsExternalData() bool {
return false
}
func (t *MsTeamsNotificationsModule) UpdateExternalData() {
// this intentionally empty
}
func (t *MsTeamsNotificationsModule) WriteExternalData(_ *os.File) {
// this intentionally empty
}
func (t *MsTeamsNotificationsModule) CreateActions(_ []Tag) []action {
return []action{}
}
func (t *MsTeamsNotificationsModule) ReadExternalData(_ []byte) error {
return nil
}