forked from carlosa8c/acyl
/
slack.go
69 lines (63 loc) · 1.76 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
package notifier
import (
multierror "github.com/hashicorp/go-multierror"
"github.com/nlopes/slack"
"github.com/pkg/errors"
)
// SlackAPIClient describes the methods we use on slack.Client
type SlackAPIClient interface {
PostMessage(channel, text string, params slack.PostMessageParameters) (string, string, error)
}
// SlackBackend is an object that uses API to send notifications to Users and Channels
type SlackBackend struct {
Username, IconURL string
API SlackAPIClient
Users, Channels []string
}
var _ Backend = &SlackBackend{}
// Send broadcasts n to all configured users/channels
func (sb *SlackBackend) Send(n Notification) error {
p, err := sb.render(n)
if err != nil || p == nil {
return errors.Wrap(err, "error rendering notification")
}
p.Username = sb.Username
p.IconURL = sb.IconURL
var merr *multierror.Error
for _, c := range sb.Channels {
if len(c) > 0 {
_, _, err = sb.API.PostMessage(c, p.Text, *p)
if err != nil {
merr = multierror.Append(merr, err)
}
}
}
for _, u := range sb.Users {
if len(u) > 0 {
_, _, err = sb.API.PostMessage("@"+u, p.Text, *p)
if err != nil {
merr = multierror.Append(merr, err)
}
}
}
return merr.ErrorOrNil()
}
func (sb *SlackBackend) render(n Notification) (*slack.PostMessageParameters, error) {
rn, err := n.Template.Render(n.Data)
if err != nil {
return nil, errors.Wrap(err, "error rendering template")
}
out := &slack.PostMessageParameters{Text: rn.Title, Attachments: make([]slack.Attachment, len(rn.Sections))}
for i, s := range rn.Sections {
if s.Title == "" {
s.Title = "<empty>"
}
if s.Text == "" {
s.Text = "<empty>"
}
out.Attachments[i].Title = s.Title
out.Attachments[i].Text = s.Text
out.Attachments[i].Color = s.Style
}
return out, nil
}