-
Notifications
You must be signed in to change notification settings - Fork 4
/
ms_teams.go
203 lines (184 loc) · 4.42 KB
/
ms_teams.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package alertnotification
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
)
// MsTeam is Adaptive Card for Team notification
type MsTeam struct {
Type string `json:"type"`
Attachments []attachment `json:"attachments"`
}
type attachment struct {
ContentType string `json:"contentType"`
ContentURL *string `json:"contentUrl"`
Content cardContent `json:"content"`
}
type cardContent struct {
Schema string `json:"$schema"`
Type string `json:"type"`
Version string `json:"version"`
AccentColor string `json:"accentColor"`
Body []interface{} `json:"body"`
Actions []action `json:"actions"`
MSTeams msTeams `json:"msteams"`
}
type textBlock struct {
Type string `json:"type"`
Text string `json:"text"`
ID string `json:"id,omitempty"`
Size string `json:"size,omitempty"`
Weight string `json:"weight,omitempty"`
Color string `json:"color,omitempty"`
}
type fact struct {
Title string `json:"title"`
Value string `json:"value"`
}
type factSet struct {
Type string `json:"type"`
Facts []fact `json:"facts"`
ID string `json:"id"`
}
type codeBlock struct {
Type string `json:"type"`
CodeSnippet string `json:"codeSnippet"`
FontType string `json:"fontType"`
Wrap bool `json:"wrap"`
}
type action struct {
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
}
type msTeams struct {
Width string `json:"width"`
}
// NewMsTeam is used to create MsTeam
func NewMsTeam(err error, expandos *Expandos) MsTeam {
title := os.Getenv("ALERT_CARD_SUBJECT")
summary := os.Getenv("MS_TEAMS_CARD_SUBJECT")
errMsg := fmt.Sprintf("%+v", err)
hostname, err := os.Hostname()
if err != nil {
hostname = "hostname_unknown"
}
hostname += " " + os.Getenv("APP_NAME")
// apply expandos on card
if expandos != nil {
if expandos.MsTeamsAlertCardSubject != "" {
title = expandos.MsTeamsAlertCardSubject
}
if expandos.MsTeamsCardSubject != "" {
summary = expandos.MsTeamsCardSubject
}
if expandos.MsTeamsError != "" {
errMsg = expandos.MsTeamsError
}
}
return MsTeam{
Type: "message",
Attachments: []attachment{
{
ContentType: "application/vnd.microsoft.card.adaptive",
ContentURL: nil,
Content: cardContent{
Schema: "http://adaptivecards.io/schemas/adaptive-card.json",
Type: "AdaptiveCard",
Version: "1.4",
AccentColor: "bf0000",
Body: []interface{}{
textBlock{
Type: "TextBlock",
Text: title,
ID: "title",
Size: "large",
Weight: "bolder",
Color: "accent",
},
factSet{
Type: "FactSet",
Facts: []fact{
{
Title: "Title:",
Value: title,
},
{
Title: "Summary:",
Value: summary,
},
{
Title: "Hostname:",
Value: hostname,
},
},
ID: "acFactSet",
},
codeBlock{
Type: "CodeBlock",
CodeSnippet: errMsg,
FontType: "monospace",
Wrap: true,
},
},
MSTeams: msTeams{
Width: "Full",
},
},
},
},
}
}
// Send is implementation of interface AlertNotification's Send()
func (card *MsTeam) Send() (err error) {
requestBody, err := json.Marshal(card)
if err != nil {
return err
}
var client http.Client
timeout := time.Duration(5 * time.Second)
proxyURL := os.Getenv("MS_TEAMS_PROXY_URL")
if proxyURL != "" {
proxy, err := url.Parse(proxyURL)
if err != nil {
return err
}
transport := &http.Transport{Proxy: http.ProxyURL(proxy)}
client = http.Client{
Transport: transport,
Timeout: timeout,
}
} else {
client = http.Client{
Timeout: timeout,
}
}
wb := os.Getenv("MS_TEAMS_WEBHOOK")
if len(wb) == 0 {
return errors.New("cannot send alert to MSTeams.MS_TEAMS_WEBHOOK is not set in the environment. ")
}
request, err := http.NewRequest("POST", wb, bytes.NewBuffer(requestBody))
request.Header.Set("Content-type", "application/json")
if err != nil {
return err
}
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1024*1024))
if err != nil {
return err
}
return fmt.Errorf("unexpected response from webhook: %s", string(respBody))
}
return
}