-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
107 lines (94 loc) · 3.04 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
package slack
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
// _httpClient is a default http client that is going to be reuse
var _httpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 20,
},
Timeout: 10 * time.Second,
}
// Send sends message (Payload) to the given slack hook URL.
func Send(hookURL string, message Message) error {
bts, err := json.Marshal(message)
if err != nil {
return ErrSerializeMessage
}
req, err := http.NewRequest("POST", hookURL, bytes.NewReader(bts))
if err != nil {
return ErrCreateRequest
}
res, err := _httpClient.Do(req)
if err != nil {
return ErrSendingRequest
}
defer res.Body.Close()
if res.StatusCode >= 400 {
return fmt.Errorf("error sending slack message. Status: %v", res.StatusCode)
}
return nil
}
// Message represent a Slack message.
type Message struct {
Parse string `json:"parse,omitempty"`
Username string `json:"username,omitempty"`
IconUrl string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
LinkNames string `json:"link_names,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
UnfurlLinks bool `json:"unfurl_links,omitempty"`
UnfurlMedia bool `json:"unfurl_media,omitempty"`
Markdown bool `json:"mrkdwn,omitempty"`
}
// Attachment let you add more context to a message, making them more useful and effective.
// See https://api.slack.com/docs/message-attachments
type Attachment struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
PreText string `json:"pretext"`
AuthorName string `json:"author_name"`
AuthorLink string `json:"author_link"`
AuthorIcon string `json:"author_icon"`
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
ImageUrl string `json:"image_url"`
Fields []Field `json:"fields"`
Footer string `json:"footer"`
FooterIcon string `json:"footer_icon"`
Timestamp int64 `json:"ts"`
MarkdownIn []string `json:"mrkdwn_in"`
Actions []Action `json:"actions"`
CallbackID string `json:"callback_id"`
ThumbnailUrl string `json:"thumb_url"`
}
// AddField appends a new field to the Attachment
func (attachment *Attachment) AddField(field Field) *Attachment {
attachment.Fields = append(attachment.Fields, field)
return attachment
}
// AddAction appends a new Action to the Attachment
func (attachment *Attachment) AddAction(action Action) *Attachment {
attachment.Actions = append(attachment.Actions, action)
return attachment
}
// Field is defined as a dictionary with key-value pairs.
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
// Action make message interactive
type Action struct {
Type string `json:"type"`
Text string `json:"text"`
Url string `json:"url"`
Style string `json:"style"`
}