-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
56 lines (49 loc) · 1.18 KB
/
slack.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
package commonUtils
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// Adam's notifications
func NotifyMaster(strToSend, webhookURL string) (err error) {
values := map[string]string{"text": strToSend}
jsonValue, _ := json.Marshal(values)
_, err = http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return
}
return
}
func ServiceStatusUpdate(serviceName string, arr []map[string]string, webhookURL string) (err error) {
values := map[string]string{"blocks": blocksFormatter(serviceName, arr)}
jsonValue, _ := json.Marshal(values)
_, err = http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return
}
return
}
func blocksFormatter(serviceName string, arr []map[string]string) (msg string) {
fieldFormat := `{"type": "mrkdwn", "text": "*%s:*\n%s"},`
for _, a := range arr {
msg += fmt.Sprintf(fieldFormat, a["title"], a["value"])
}
msg = fmt.Sprintf(blockString, serviceName, string(msg[:len(msg)-1]))
return
}
var blockString string = `[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "New Service Update:\n💡 *%s*"
}
},
{
"type": "section",
"fields": [
%s
]
}
]`