-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
slack_notifier.go
90 lines (78 loc) · 2.17 KB
/
slack_notifier.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// SlackNotifier type
// Set DryRun to 'true' to print the message to the console for testing (not send it to the Slack channel)
type SlackNotifier struct {
WebhookURL string
DryRun bool
}
// Payload is a Slack message with attachments
type Payload struct {
Attachments []Attachment `json:"attachments"`
LinkNames bool `json:"link_names"`
Mrkdwn bool `json:"mrkdwn"`
IconEmoji string `json:"icon_emoji"`
Username string `json:"username"`
Channel string `json:"channel"`
Thread string `json:"thread_ts,omitempty"`
}
// Attachment for a Slack message
// https://api.slack.com/docs/message-attachments
type Attachment struct {
AuthorIcon string `json:"author_icon"`
AuthorLink string `json:"author_link"`
AuthorName string `json:"author_name"`
Color string `json:"color"`
Fallback string `json:"fallback"`
Fields []Field `json:"fields"`
FooterIcon string `json:"footer_icon"`
Footer string `json:"footer"`
ImageURL string `json:"image_url"`
MrkdwnIn []string `json:"mrkdwn_in"`
Pretext string `json:"pretext"`
Text string `json:"text"`
ThumbURL string `json:"thumb_url"`
TitleLink string `json:"title_link"`
Title string `json:"title"`
Ts int64 `json:"ts"`
}
// Field of an attachment
type Field struct {
Short bool `json:"short"`
Title string `json:"title"`
Value string `json:"value"`
}
// NewSlackNotifier creates a new SlackNotifier
func NewSlackNotifier(webhookURL string) SlackNotifier {
return SlackNotifier{
WebhookURL: webhookURL,
}
}
// Notify sends a message to the Slack channel
func (sn SlackNotifier) Notify(message Payload) error {
data, err := json.Marshal(message)
if err != nil {
return err
}
if sn.DryRun {
fmt.Println(string(data))
return nil
}
body := bytes.NewBuffer(data)
request, err := http.NewRequest("POST", sn.WebhookURL, body)
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
_, err = client.Do(request)
if err != nil {
return err
}
return nil
}