forked from kyma-project/kyma
/
slack_client.go
73 lines (61 loc) · 1.63 KB
/
slack_client.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
package notifier
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// SlackClient sends message to slack channel
type SlackClient struct {
channelID string
webhookURL string
token string
}
// NewSlackClient returns new instance of SlackClient
func NewSlackClient(cfg SlackClientConfig) *SlackClient {
return &SlackClient{
channelID: cfg.ChannelID,
webhookURL: cfg.WebhookURL,
token: cfg.Token,
}
}
// Send sends message with given content to slack channel
func (c *SlackClient) Send(header, body, footer, color string) error {
url := fmt.Sprintf("%s?token=%s", c.webhookURL, c.token)
payload := payload{
Channel: c.channelID,
Text: header,
Attachments: []*attachment{
{
Color: color,
Text: body,
},
{
Text: footer, // if not provided (empty) then this attachment will not be showed in slack channel
},
},
}
dto, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "while marshaling body to send")
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(dto))
if err != nil {
return errors.Wrap(err, "while sending notification to web-hook")
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("expected to get %d status code but got %d status code", http.StatusOK, resp.StatusCode)
}
return nil
}
type attachment struct {
Color string `json:"color,omitempty"`
Text string `json:"text,omitempty"`
Footer string `json:"footer,omitempty"`
}
type payload struct {
Channel string `json:"channel"`
Text string `json:"text"`
Attachments []*attachment `json:"attachments,omitempty"`
}