-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack.go
125 lines (108 loc) · 3.33 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
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
package main
import (
"bytes"
"github.com/nlopes/slack"
"github.com/spf13/cast"
"strings"
"text/template"
"time"
)
func SlackSendAlertMessage(token, channel string, messageOptions ...slack.MsgOption) (string, string, error) {
api := slack.New(token)
respChannel, respTimestamp, err := api.PostMessage(channel, messageOptions...)
return respChannel, respTimestamp, err
}
func SlackUpdateAlertMessage(token, channel, timestamp string, messageOptions ...slack.MsgOption) (string, string, error) {
api := slack.New(token)
respChannel, respTimestamp, respText, err := api.UpdateMessage(channel, timestamp, messageOptions...)
_ = respText
return respChannel, respTimestamp, err
}
func ComposeResolveUpdateBody(alert Alert, headerTemplate string, images ...SlackImage) (slack.MsgOption, error) {
headerTpl, e := ParseTemplate(headerTemplate, alert)
if e != nil {
return nil, e
}
statusBlock := slack.NewTextBlockObject(
"mrkdwn",
headerTpl.String(),
false,
false,
)
var blocks []slack.Block
blocks = append(blocks, slack.NewSectionBlock(statusBlock, nil, nil))
blocks = append(blocks, slack.NewDividerBlock())
for _, image := range images {
textBlock := slack.NewTextBlockObject("plain_text", image.Title, false, false)
blocks = append(blocks, slack.NewImageBlock(image.Url, "metric graph "+image.Title, "", textBlock))
}
messageBlocks := slack.MsgOptionBlocks(blocks...)
return messageBlocks, nil
}
func ComposeUpdateFooter(alert Alert, footerTemplate string) ([]slack.Block, error) {
footerTpl, e := ParseTemplate(footerTemplate, alert)
if e != nil {
return nil, e
}
footerBlock := slack.NewTextBlockObject(
"mrkdwn",
footerTpl.String(),
false,
false,
)
var blocks []slack.Block
blocks = append(blocks, slack.NewDividerBlock())
blocks = append(blocks, slack.NewContextBlock("", footerBlock))
return blocks, nil
}
func ComposeMessageBody(alert Alert, messageTemplate, headerTemplate string, images ...SlackImage) ([]slack.Block, error) {
tpl, e := ParseTemplate(messageTemplate, alert)
if e != nil {
return nil, e
}
headerTpl, e := ParseTemplate(headerTemplate, alert)
if e != nil {
return nil, e
}
statusBlock := slack.NewTextBlockObject(
"mrkdwn",
headerTpl.String(),
false,
false,
)
textBlockObj := slack.NewTextBlockObject(
"mrkdwn",
tpl.String(),
false,
false,
)
var blocks []slack.Block
blocks = append(blocks, slack.NewSectionBlock(statusBlock, nil, nil))
blocks = append(blocks, slack.NewDividerBlock())
blocks = append(blocks, slack.NewSectionBlock(textBlockObj, nil, nil))
for _, image := range images {
textBlock := slack.NewTextBlockObject("plain_text", image.Title, false, false)
blocks = append(blocks, slack.NewImageBlock(image.Url, "metric graph "+image.Title, "", textBlock))
}
return blocks, nil
}
func ParseTemplate(messageTemplate string, alert Alert) (bytes.Buffer, error) {
funcMap := template.FuncMap{
"toUpper": strings.ToUpper,
"now": time.Now,
"dateFormat": func(layout string, v interface{}) (string, error) {
t, err := cast.ToTimeE(v)
if err != nil {
return "", err
}
return t.Format(layout), nil
},
}
msgTpl, err := template.New("message").Funcs(funcMap).Parse(messageTemplate)
fatal(err, "error in template")
var tpl bytes.Buffer
if err := msgTpl.Execute(&tpl, alert); err != nil {
return bytes.Buffer{}, err
}
return tpl, err
}