-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathslack.go
161 lines (133 loc) · 5.79 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package slack
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
// AttachmentField contains information for an attachment field
// An Attachment can contain multiple of these
type AttachmentField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
// AttachmentAction is a button or menu to be included in the attachment. Required when
// using message buttons or menus and otherwise not useful. A maximum of 5 actions may be
// provided per attachment.
type AttachmentAction struct {
Name string `json:"name"` // Required.
Text string `json:"text"` // Required.
Style string `json:"style,omitempty"` // Optional. Allowed values: "default", "primary", "danger".
Type string `json:"type"` // Required. Must be set to "button" or "select".
Value string `json:"value,omitempty"` // Optional.
DataSource string `json:"data_source,omitempty"` // Optional.
MinQueryLength int `json:"min_query_length,omitempty"` // Optional. Default value is 1.
Options []AttachmentActionOption `json:"options,omitempty"` // Optional. Maximum of 100 options can be provided in each menu.
SelectedOptions []AttachmentActionOption `json:"selected_options,omitempty"` // Optional. The first element of this array will be set as the pre-selected option for this menu.
OptionGroups []AttachmentActionOptionGroup `json:"option_groups,omitempty"` // Optional.
Confirm *ConfirmationField `json:"confirm,omitempty"` // Optional.
URL string `json:"url,omitempty"` // Optional.
}
// AttachmentActionOption the individual option to appear in action menu.
type AttachmentActionOption struct {
Text string `json:"text"` // Required.
Value string `json:"value"` // Required.
Description string `json:"description,omitempty"` // Optional. Up to 30 characters.
}
// AttachmentActionOptionGroup is a semi-hierarchal way to list available options to appear in action menu.
type AttachmentActionOptionGroup struct {
Text string `json:"text"` // Required.
Options []AttachmentActionOption `json:"options"` // Required.
}
// ActionCallback specific fields for the action callback.
type ActionCallback struct {
MessageTs string `json:"message_ts"`
AttachmentID string `json:"attachment_id"`
Actions []AttachmentAction `json:"actions"`
}
// ConfirmationField are used to ask users to confirm actions
type ConfirmationField struct {
Title string `json:"title,omitempty"` // Optional.
Text string `json:"text"` // Required.
OkText string `json:"ok_text,omitempty"` // Optional. Defaults to "Okay"
DismissText string `json:"dismiss_text,omitempty"` // Optional. Defaults to "Cancel"
}
// Attachment contains all the information for an attachment
type Attachment struct {
Color string `json:"color,omitempty"`
Fallback string `json:"fallback"`
CallbackID string `json:"callback_id,omitempty"`
ID int `json:"id,omitempty"`
AuthorID string `json:"author_id,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorSubname string `json:"author_subname,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Pretext string `json:"pretext,omitempty"`
Text string `json:"text"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
Fields []AttachmentField `json:"fields,omitempty"`
Actions []AttachmentAction `json:"actions,omitempty"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`
Footer string `json:"footer,omitempty"`
FooterIcon string `json:"footer_icon,omitempty"`
Ts json.Number `json:"ts,omitempty"`
}
// Message is a Slack message payload.
type Message struct {
ResponseType string `json:"response_tyoe,omitempty"`
Text string `json:"text,omitempty"`
Channel string `json:"channel,omitempty"`
Username string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
UnfurlLinks bool `json:"unfurl_links,omitempty"`
LinkNames string `json:"link_names,omitempty"`
Attachments []*Attachment `json:"attachments,omitempty"`
}
// ReadMessage reads a message from the given file.
func ReadMessage(path string, msg *Message) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = json.Unmarshal(b, msg)
if err != nil {
return errors.Wrap(err, "unmarshaling")
}
return nil
}
// WriteMessage writes a message to the given file.
func WriteMessage(path string, msg *Message) error {
b, err := json.MarshalIndent(msg, "", " ")
if err != nil {
return errors.Wrap(err, "marshaling")
}
err = ioutil.WriteFile(path, b, 0755)
if err != nil {
return err
}
return nil
}
// Send a message to the given webhook url.
func Send(url string, msg *Message) error {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(msg)
if err != nil {
return errors.Wrap(err, "marshaling")
}
res, err := http.Post(url, "application/json", &buf)
if err != nil {
return errors.Wrap(err, "requesting")
}
defer res.Body.Close()
if res.StatusCode >= 300 {
return errors.Errorf("%s response", res.Status)
}
return nil
}