-
Notifications
You must be signed in to change notification settings - Fork 12
/
chan-gr-im.go
444 lines (400 loc) · 11.3 KB
/
chan-gr-im.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package slack
import (
"net/url"
"strconv"
"strings"
)
// ChannelTopicPurpose holds the topic or purpose of a channel
type ChannelTopicPurpose struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet int64 `json:"last_set"`
}
// BaseChannel holds information about channel / group / IM
type BaseChannel struct {
ID string `json:"id"`
Name string `json:"name"`
Created interface{} `json:"created"` // Sometimes a string and sometimes int64
Creator string `json:"creator"`
IsArchived bool `json:"is_archived"`
IsOpen bool `json:"is_open"`
Members []string `json:"members"`
Topic ChannelTopicPurpose `json:"topic"`
Purpose ChannelTopicPurpose `json:"purpose"`
LastRead string `json:"last_read,omitempty"`
Latest Message `json:"latest,omitempty"`
UnreadCount int `json:"unread_count,omitempty"`
UnreadCountDisplay int `json:"unread_count_display,omitempty"`
NumMembers int `json:"num_members,omitempty"`
}
// Channel holds information about the channel
type Channel struct {
BaseChannel
IsGeneral bool `json:"is_general"`
IsChannel bool `json:"is_channel"`
IsMember bool `json:"is_member"`
}
// Group holds information about the group
type Group struct {
BaseChannel
IsGroup bool `json:"is_group"`
IsMPIM bool `json:"is_mpim"`
}
// IM holds information about IM
type IM struct {
BaseChannel
IsIM bool `json:"is_im"`
User string `json:"user"`
IsUserDeleted bool `json:"is_user_deleted"`
}
// ChannelResponse holds a response to a channel request
type ChannelResponse struct {
slackResponse
Channel Channel `json:"channel"`
}
// ChannelCommonResponse holds response to rename request
type ChannelCommonResponse struct {
slackResponse
Channel struct {
ID string `json:"id"`
Name string `json:"name"`
Created int64 `json:"created"`
IsChannel bool `json:"is_channel"`
IsGroup bool `json:"is_group"`
} `json:"channel"`
}
// HistoryResponse holds a response to a history request
type HistoryResponse struct {
slackResponse
Latest string `json:"latest"`
HasMore bool `json:"has_more"`
UnreadCountDisplay int `json:"unread_count_display"`
Messages []Message `json:"messages"`
}
// GroupResponse holds a response to a group request
type GroupResponse struct {
slackResponse
Group Group `json:"group"`
}
// ChannelListResponse holds a response to a channel list request
type ChannelListResponse struct {
slackResponse
Channels []Channel `json:"channels"`
}
// GroupListResponse holds a response to a group list request
type GroupListResponse struct {
slackResponse
Groups []Group `json:"groups"`
}
// IMListResponse holds a response to an IM list request
type IMListResponse struct {
slackResponse
IMs []IM `json:"ims"`
}
// PurposeResponse is the response to setPurpose request
type PurposeResponse struct {
slackResponse
Purpose string `json:"purpose"`
}
// TopicResponse is the response to setTopic request
type TopicResponse struct {
slackResponse
Topic string `json:"topic"`
}
// OpenResponse is returned for open requests
type OpenResponse struct {
slackResponse
NoOp bool `json:"no_op"`
AlreadyOpen bool `json:"already_open"`
}
// OpenIMResponse is returned for open IM requests
type OpenIMResponse struct {
slackResponse
NoOp bool `json:"no_op"`
AlreadyOpen bool `json:"already_open"`
Channel IM `json:"channel"`
}
// CloseResponse is returned for close requests
type CloseResponse struct {
slackResponse
NoOp bool `json:"no_op"`
AlreadyClosed bool `json:"already_closed"`
}
func prefixByID(id string) string {
path := "channels."
switch id[0] {
case 'G':
path = "groups."
case 'D':
path = "im."
}
return path
}
// Archive a channel or a group
func (s *Slack) Archive(channel string) (Response, error) {
params := url.Values{"channel": {channel}}
r := &slackResponse{}
err := s.do(prefixByID(channel)+"archive", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// Unarchive a channel or a group
func (s *Slack) Unarchive(channel string) (Response, error) {
params := url.Values{"channel": {channel}}
r := &slackResponse{}
err := s.do(prefixByID(channel)+"unarchive", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// History retrieves history of channel, group, MPIM and IM
func (s *Slack) History(channel, latest, oldest string, inclusive, unreads bool, count int) (*HistoryResponse, error) {
params := url.Values{"channel": {channel}}
appendNotEmpty("latest", latest, params)
appendNotEmpty("oldest", oldest, params)
if inclusive {
params.Set("inclusive", "1")
}
if unreads {
params.Set("unreads", "1")
}
if count != 0 {
params.Set("count", strconv.Itoa(count))
}
r := &HistoryResponse{}
err := s.do(prefixByID(channel)+"history", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// Kick a user from a channel or group
func (s *Slack) Kick(channel, user string) (Response, error) {
params := url.Values{"channel": {channel}, "user": {user}}
r := &slackResponse{}
err := s.do(prefixByID(channel)+"kick", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// Leave a channel or a group
func (s *Slack) Leave(channel string) (Response, error) {
params := url.Values{"channel": {channel}}
r := &slackResponse{}
err := s.do(prefixByID(channel)+"join", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// Mark marks the given channel as read. Automatically detects channel/group/im
func (s *Slack) Mark(channel, ts string) error {
r := &slackResponse{}
params := url.Values{"channel": {channel}, "ts": {ts}}
path := prefixByID(channel) + "mark"
err := s.do(path, params, r)
if err != nil {
return err
}
return nil
}
// Rename a channel or a group
func (s *Slack) Rename(channel, name string) (*ChannelCommonResponse, error) {
params := url.Values{"channel": {channel}, "name": {name}}
r := &ChannelCommonResponse{}
err := s.do(prefixByID(channel)+"rename", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// SetPurpose of the channel / group
func (s *Slack) SetPurpose(channel, purpose string) (*PurposeResponse, error) {
params := url.Values{"channel": {channel}, "purpose": {purpose}}
r := &PurposeResponse{}
err := s.do(prefixByID(channel)+"setPurpose", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// SetTopic of the channel / group
func (s *Slack) SetTopic(channel, purpose string) (*TopicResponse, error) {
params := url.Values{"channel": {channel}, "topic": {purpose}}
r := &TopicResponse{}
err := s.do(prefixByID(channel)+"setTopic", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// CloseGroupOrIM closes the given id
func (s *Slack) CloseGroupOrIM(id string) (*CloseResponse, error) {
params := url.Values{"channel": {id}}
r := &CloseResponse{}
err := s.do(prefixByID(id)+"close", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// OpenGroup opens a group
func (s *Slack) OpenGroup(id string) (*OpenResponse, error) {
params := url.Values{"channel": {id}}
r := &OpenResponse{}
err := s.do(prefixByID(id)+"open", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// OpenIM opens an IM conversation with the given user ID
func (s *Slack) OpenIM(id string) (*OpenIMResponse, error) {
params := url.Values{"user": {id}, "return_im": {"true"}}
r := &OpenIMResponse{}
err := s.do("im.open", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// OpenMPIM with the given users
func (s *Slack) OpenMPIM(users []string) (*GroupResponse, error) {
params := url.Values{"users": {strings.Join(users, ",")}}
r := &GroupResponse{}
err := s.do("mpim.open", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// ChannelCreate creates a channel
func (s *Slack) ChannelCreate(name string) (*ChannelResponse, error) {
params := url.Values{"name": {name}}
r := &ChannelResponse{}
err := s.do("channels.create", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// ChannelInvite invites a user to a group
func (s *Slack) ChannelInvite(channel, user string) (*ChannelResponse, error) {
params := url.Values{"channel": {channel}, "user": {user}}
r := &ChannelResponse{}
err := s.do("channels.invite", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// ChannelInfo returns info about the channel
func (s *Slack) ChannelInfo(channel string) (*ChannelResponse, error) {
params := url.Values{"channel": {channel}}
r := &ChannelResponse{}
err := s.do("channels.info", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// ChannelList returns the list of channels
func (s *Slack) ChannelList(excludeArchived bool) (*ChannelListResponse, error) {
params := url.Values{}
if excludeArchived {
params.Set("exclude_archived", "1")
}
r := &ChannelListResponse{}
err := s.do("channels.list", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// ChannelJoin joins a channel - notice that this expects channel name and not id
func (s *Slack) ChannelJoin(channel string) (*ChannelResponse, error) {
params := url.Values{"name": {channel}}
r := &ChannelResponse{}
err := s.do("channels.join", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// GroupCreate creates a new group with the given name
func (s *Slack) GroupCreate(name string) (*GroupResponse, error) {
params := url.Values{"name": {name}}
r := &GroupResponse{}
err := s.do("groups.create", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// GroupCreateChild archives existing group and creates a new group with the given name
func (s *Slack) GroupCreateChild(group string) (*GroupResponse, error) {
params := url.Values{"channel": {group}}
r := &GroupResponse{}
err := s.do("groups.createChild", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// GroupInfo returns info about the group
func (s *Slack) GroupInfo(group string) (*GroupResponse, error) {
params := url.Values{"channel": {group}}
r := &GroupResponse{}
err := s.do("groups.info", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// GroupInvite invites a user to a group
func (s *Slack) GroupInvite(channel, user string) (*GroupResponse, error) {
params := url.Values{"channel": {channel}, "user": {user}}
r := &GroupResponse{}
err := s.do("groups.invite", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// GroupList returns the list of groups
func (s *Slack) GroupList(excludeArchived bool) (*GroupListResponse, error) {
params := url.Values{}
if excludeArchived {
params.Set("exclude_archived", "1")
}
r := &GroupListResponse{}
err := s.do("groups.list", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// MPIMList returns the list of MPIMs
func (s *Slack) MPIMList() (*GroupListResponse, error) {
params := url.Values{}
r := &GroupListResponse{}
err := s.do("mpim.list", params, r)
if err != nil {
return nil, err
}
return r, nil
}
// IMList returns the list of IMs
func (s *Slack) IMList() (*IMListResponse, error) {
params := url.Values{}
r := &IMListResponse{}
err := s.do("im.list", params, r)
if err != nil {
return nil, err
}
return r, nil
}