-
Notifications
You must be signed in to change notification settings - Fork 812
/
msteams.go
139 lines (112 loc) · 3.69 KB
/
msteams.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package notifications
import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net/http"
t "github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
"io/ioutil"
)
const (
msTeamsType = "msteams"
)
type msTeamsTypeNotifier struct {
webHookURL string
levels []log.Level
data bool
}
func newMsTeamsNotifier(_ *cobra.Command, acceptedLogLevels []log.Level) t.Notifier {
webHookURL := viper.GetString("notification-msteams-hook")
if len(webHookURL) <= 0 {
log.Fatal("Required argument --notification-msteams-hook(cli) or WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL(env) is empty.")
}
withData := viper.GetBool("notification-msteams-data")
n := &msTeamsTypeNotifier{
levels: acceptedLogLevels,
webHookURL: webHookURL,
data: withData,
}
log.AddHook(n)
return n
}
func (n *msTeamsTypeNotifier) StartNotification() {}
func (n *msTeamsTypeNotifier) SendNotification() {}
func (n *msTeamsTypeNotifier) Close() {}
func (n *msTeamsTypeNotifier) Levels() []log.Level {
return n.levels
}
func (n *msTeamsTypeNotifier) Fire(entry *log.Entry) error {
message := "(" + entry.Level.String() + "): " + entry.Message
go func() {
webHookBody := messageCard{
CardType: "MessageCard",
Context: "http://schema.org/extensions",
Markdown: true,
Text: message,
}
if n.data && entry.Data != nil && len(entry.Data) > 0 {
section := messageCardSection{
Facts: make([]messageCardSectionFact, len(entry.Data)),
Text: "",
}
index := 0
for k, v := range entry.Data {
section.Facts[index] = messageCardSectionFact{
Name: k,
Value: fmt.Sprint(v),
}
index++
}
webHookBody.Sections = []messageCardSection{section}
}
jsonBody, err := json.Marshal(webHookBody)
if err != nil {
fmt.Println("Failed to build JSON body for MSTeams notification: ", err)
return
}
resp, err := http.Post(n.webHookURL, "application/json", bytes.NewBuffer(jsonBody))
if err != nil {
fmt.Println("Failed to send MSTeams notification: ", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
fmt.Println("Failed to send MSTeams notification. HTTP RESPONSE STATUS: ", resp.StatusCode)
if resp.Body != nil {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err == nil {
bodyString := string(bodyBytes)
fmt.Println(bodyString)
}
}
}
}()
return nil
}
type messageCard struct {
CardType string `json:"@type"`
Context string `json:"@context"`
CorrelationID string `json:"correlationId,omitempty"`
ThemeColor string `json:"themeColor,omitempty"`
Summary string `json:"summary,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
Markdown bool `json:"markdown,bool"`
Sections []messageCardSection `json:"sections,omitempty"`
}
type messageCardSection struct {
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
ActivityTitle string `json:"activityTitle,omitempty"`
ActivitySubtitle string `json:"activitySubtitle,omitempty"`
ActivityImage string `json:"activityImage,omitempty"`
ActivityText string `json:"activityText,omitempty"`
HeroImage string `json:"heroImage,omitempty"`
Facts []messageCardSectionFact `json:"facts,omitempty"`
}
type messageCardSectionFact struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}