-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_api.go
350 lines (296 loc) · 8.88 KB
/
slack_api.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"encoding/json"
"errors"
"github.com/mitchellh/mapstructure"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type Slack struct {
Token string
HttpClient http.Client
}
type SlackError struct {
error
}
func (se *SlackError) Error() string {
return "slack error: " + se.error.Error()
}
type SlackResponseMetadata struct {
NextCursor string `mapstructure:"next_cursor"`
}
type SlackUser struct {
ID string `mapstructure:"id"`
TeamID string `mapstructure:"team_id"`
Name string `mapstructure:"name"`
Deleted bool `mapstructure:"deleted"`
Profile SlackUserProfile `mapstructure:"profile"`
IsAdmin bool `mapstructure:"is_admin"`
IsOwner bool `mapstructure:"is_owner"`
IsBot bool `mapstructure:"is_bot"`
IsAppUser bool `mapstructure:"is_app_user"`
}
type SlackUserProfile struct {
RealName string `mapstructure:"real_name"`
DisplayName string `mapstructure:"display_name"`
}
type SlackChannel struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
IsChannel bool `mapstructure:"is_channel"`
IsGroup bool `mapstructure:"is_group"`
IsIm bool `mapstructure:"is_im"`
Created int `mapstructure:"created"`
Creator string `mapstructure:"creator"`
IsArchived bool `mapstructure:"is_archived"`
IsGeneral bool `mapstructure:"is_general"`
IsMember bool `mapstructure:"is_member"`
IsPrivate bool `mapstructure:"is_private"`
IsMpim bool `mapstructure:"id_mpim"`
NumMembers int `mapstructure:"num_members"`
}
type SlackMessage struct {
Type string `mapstructure:"type"`
User string `mapstructure:"user"`
Text string `mapstructure:"text"`
Timestamp string `mapstructure:"ts"`
}
func (s Slack) request(method string, endpoint string, params map[string]string, contentKey string) (interface{}, *SlackResponseMetadata, error) {
req, err := http.NewRequest(method, "https://slack.com/api/"+endpoint, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("token", s.Token)
for key, value := range params {
q.Add(key, value)
}
req.URL.RawQuery = q.Encode()
req.Header.Add("Content-type", "application/x-www-form-urlencoded")
resp, err := s.HttpClient.Do(req)
if err != nil {
return nil, nil, err
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, nil, err
}
var topLevelJsonResp map[string]interface{}
err = json.Unmarshal(bytes, &topLevelJsonResp)
if err != nil {
return nil, nil, err
}
ok, typeConversionSucceeded := topLevelJsonResp["ok"].(bool)
if !typeConversionSucceeded {
log.Critical("Could not cast \"ok\" to type bool")
return nil, nil, nil
}
if !ok {
errStr, typeConversionSucceeded := topLevelJsonResp["error"].(string)
if !typeConversionSucceeded {
log.Critical("Could not cast \"error\" to type string")
return nil, nil, nil
}
return nil, nil, &SlackError{errors.New(errStr)}
}
var contentJson interface{}
if contentKey != "" {
var present bool
contentJson, present = topLevelJsonResp[contentKey]
if !present {
return nil, nil, errors.New("response is missing " + contentKey)
}
}
metadataJson, present := topLevelJsonResp["response_metadata"]
var metadata *SlackResponseMetadata
if present {
err = mapstructure.Decode(metadataJson, &metadata)
if err != nil {
metadata = nil
}
}
return contentJson, metadata, nil
}
func (s Slack) get(endpoint string, params map[string]string, contentKey string) (interface{}, *SlackResponseMetadata, error) {
return s.request("GET", endpoint, params, contentKey)
}
func (s Slack) post(endpoint string, params map[string]string, contentKey string) (interface{}, *SlackResponseMetadata, error) {
return s.request("POST", endpoint, params, contentKey)
}
func (s Slack) cursorCollect(endpoint string, params map[string]string, contentKey string, initialValue interface{}, collect func(interface{}, interface{}) (interface{}, error)) (interface{}, error) {
cursor := ""
for {
if cursor != "" {
params["cursor"] = cursor
}
jsonContent, metadata, err := s.get(endpoint, params, contentKey)
if err != nil {
return nil, err
}
initialValue, err = collect(initialValue, jsonContent)
if err != nil {
return nil, err
}
if metadata == nil {
break
}
cursor = metadata.NextCursor
if cursor == "" {
break
}
}
return initialValue, nil
}
func (s Slack) getChannels(endpoint string, params map[string]string) ([]SlackChannel, error) {
collected, err := s.cursorCollect(
endpoint,
params,
"channels",
[]SlackChannel{},
func(collected interface{}, json interface{}) (interface{}, error) {
var partial []SlackChannel
err := mapstructure.Decode(json, &partial)
if err != nil {
return nil, err
}
return append(collected.([]SlackChannel), partial...), nil
})
if err != nil {
return nil, err
}
return collected.([]SlackChannel), nil
}
func (s Slack) getMembers(endpoint string, params map[string]string) ([]SlackUser, error) {
collected, err := s.cursorCollect(
endpoint,
params,
"members",
[]SlackUser{},
func(collected interface{}, json interface{}) (interface{}, error) {
var partial []SlackUser
err := mapstructure.Decode(json, &partial)
if err != nil {
return nil, err
}
return append(collected.([]SlackUser), partial...), nil
})
if err != nil {
return nil, err
}
return collected.([]SlackUser), nil
}
func (s Slack) getStrings(endpoint string, params map[string]string, contentKey string) ([]string, error) {
collected, err := s.cursorCollect(
endpoint,
params,
contentKey,
[]string{},
func(collected interface{}, json interface{}) (interface{}, error) {
var partial []string
err := mapstructure.Decode(json, &partial)
if err != nil {
return nil, err
}
return append(collected.([]string), partial...), nil
})
if err != nil {
return nil, err
}
return collected.([]string), nil
}
func (s Slack) ApiTest() error {
_, _, err := s.get("api.test", map[string]string{"foo": "bar"}, "args")
return err
}
func (s Slack) ChatPostMessage(channel string, text string, blocks []interface{}) error {
params := map[string]string{"channel": channel, "text": text}
if blocks != nil {
jsonBlock, err := json.Marshal(blocks)
if err != nil {
return err
}
params["blocks"] = string(jsonBlock)
}
_, _, err := s.post("chat.postMessage", params, "")
return err
}
func (s Slack) ChatUpdate(channel string, ts string, text string, blocks []interface{}) error {
params := map[string]string{"channel": channel, "ts": ts, "text": text}
if blocks != nil {
jsonBlock, err := json.Marshal(blocks)
if err != nil {
return err
}
params["blocks"] = string(jsonBlock)
}
_, _, err := s.post("chat.update", params, "")
return err
}
func (s Slack) ConversationsHistory(channel string, limit int) ([]SlackMessage, error) {
params := map[string]string{"channel": channel, "limit": strconv.Itoa(limit)}
content, _, err := s.get("conversations.history", params, "messages")
if err != nil {
return nil, err
}
var messages []SlackMessage
if err = mapstructure.Decode(content, &messages); err != nil {
return nil, err
}
return messages, err
}
func (s Slack) ConversationsList(excludeArchived bool, types []string) ([]SlackChannel, error) {
params := map[string]string{"exclude_archived": strconv.FormatBool(excludeArchived)}
if len(types) != 0 {
params["types"] = strings.Join(types, ",")
}
return s.getChannels("conversations.list", params)
}
func (s Slack) ConversationsMembers(channel string) ([]string, error) {
params := map[string]string{"channel": channel}
return s.getStrings("conversations.members", params, "members")
}
func (s Slack) ConversationsOpen(users []string) (string, error) {
params := map[string]string{"users": strings.Join(users, ",")}
channelJson, _, err := s.get("conversations.open", params, "channel")
if err != nil {
return "", err
}
return channelJson.(map[string]interface{})["id"].(string), nil
}
func (s Slack) UsersConversations(excludeArchived bool, types []string) ([]SlackChannel, error) {
params := map[string]string{"exclude_archived": strconv.FormatBool(excludeArchived)}
if len(types) != 0 {
params["types"] = strings.Join(types, ",")
}
return s.getChannels("users.conversations", params)
}
func (s Slack) UsersList() ([]SlackUser, error) {
return s.getMembers("users.list", nil)
}
func (s Slack) FindChannel(name, id string) (channel *SlackChannel, err error) {
channels, err := s.UsersConversations(true, []string{"public_channel"})
if err != nil {
return nil, err
}
if name != "" {
for _, channel := range channels {
if strings.EqualFold(name, channel.Name) {
return &channel, nil
}
}
}
if id != "" {
for _, channel := range channels {
if id == channel.ID {
return &channel, nil
}
}
}
return nil, nil
}