Debian Codegraph @

conversation.go

raw symbols SCIP
1 package slack
2
3 import (
4 "context"
5 "net/url"
6 "strconv"
7 "strings"
8 )
9
10 // Conversation is the foundation for IM and BaseGroupConversation
11 type Conversation struct {
12 ID string `json:"id"`
13 Created JSONTime `json:"created"`
14 IsOpen bool `json:"is_open"`
15 LastRead string `json:"last_read,omitempty"`
16 Latest *Message `json:"latest,omitempty"`
17 UnreadCount int `json:"unread_count,omitempty"`
18 UnreadCountDisplay int `json:"unread_count_display,omitempty"`
19 IsGroup bool `json:"is_group"`
20 IsShared bool `json:"is_shared"`
21 IsIM bool `json:"is_im"`
22 IsExtShared bool `json:"is_ext_shared"`
23 IsOrgShared bool `json:"is_org_shared"`
24 IsPendingExtShared bool `json:"is_pending_ext_shared"`
25 IsPrivate bool `json:"is_private"`
26 IsMpIM bool `json:"is_mpim"`
27 Unlinked int `json:"unlinked"`
28 NameNormalized string `json:"name_normalized"`
29 NumMembers int `json:"num_members"`
30 Priority float64 `json:"priority"`
31 User string `json:"user"`
32
33 // TODO support pending_shared
34 // TODO support previous_names
35 }
36
37 // GroupConversation is the foundation for Group and Channel
38 type GroupConversation struct {
39 Conversation
40 Name string `json:"name"`
41 Creator string `json:"creator"`
42 IsArchived bool `json:"is_archived"`
43 Members []string `json:"members"`
44 Topic Topic `json:"topic"`
45 Purpose Purpose `json:"purpose"`
46 }
47
48 // Topic contains information about the topic
49 type Topic struct {
50 Value string `json:"value"`
51 Creator string `json:"creator"`
52 LastSet JSONTime `json:"last_set"`
53 }
54
55 // Purpose contains information about the purpose
56 type Purpose struct {
57 Value string `json:"value"`
58 Creator string `json:"creator"`
59 LastSet JSONTime `json:"last_set"`
60 }
61
62 type GetUsersInConversationParameters struct {
63 ChannelID string
64 Cursor string
65 Limit int
66 }
67
68 type GetConversationsForUserParameters struct {
69 UserID string
70 Cursor string
71 Types []string
72 Limit int
73 ExcludeArchived bool
74 }
75
76 type responseMetaData struct {
77 NextCursor string `json:"next_cursor"`
78 }
79
80 // GetUsersInConversation returns the list of users in a conversation
81 func (api *Client) GetUsersInConversation(params *GetUsersInConversationParameters) ([]string, string, error) {
82 return api.GetUsersInConversationContext(context.Background(), params)
83 }
84
85 // GetUsersInConversationContext returns the list of users in a conversation with a custom context
86 func (api *Client) GetUsersInConversationContext(ctx context.Context, params *GetUsersInConversationParameters) ([]string, string, error) {
87 values := url.Values{
88 "token": {api.token},
89 "channel": {params.ChannelID},
90 }
91 if params.Cursor != "" {
92 values.Add("cursor", params.Cursor)
93 }
94 if params.Limit != 0 {
95 values.Add("limit", strconv.Itoa(params.Limit))
96 }
97 response := struct {
98 Members []string `json:"members"`
99 ResponseMetaData responseMetaData `json:"response_metadata"`
100 SlackResponse
101 }{}
102
103 err := api.postMethod(ctx, "conversations.members", values, &response)
104 if err != nil {
105 return nil, "", err
106 }
107
108 if err := response.Err(); err != nil {
109 return nil, "", err
110 }
111
112 return response.Members, response.ResponseMetaData.NextCursor, nil
113 }
114
115 // GetConversationsForUser returns the list conversations for a given user
116 func (api *Client) GetConversationsForUser(params *GetConversationsForUserParameters) (channels []Channel, nextCursor string, err error) {
117 return api.GetConversationsForUserContext(context.Background(), params)
118 }
119
120 // GetConversationsForUserContext returns the list conversations for a given user with a custom context
121 func (api *Client) GetConversationsForUserContext(ctx context.Context, params *GetConversationsForUserParameters) (channels []Channel, nextCursor string, err error) {
122 values := url.Values{
123 "token": {api.token},
124 }
125 if params.UserID != "" {
126 values.Add("user", params.UserID)
127 }
128 if params.Cursor != "" {
129 values.Add("cursor", params.Cursor)
130 }
131 if params.Limit != 0 {
132 values.Add("limit", strconv.Itoa(params.Limit))
133 }
134 if params.Types != nil {
135 values.Add("types", strings.Join(params.Types, ","))
136 }
137 if params.ExcludeArchived {
138 values.Add("exclude_archived", "true")
139 }
140 response := struct {
141 Channels []Channel `json:"channels"`
142 ResponseMetaData responseMetaData `json:"response_metadata"`
143 SlackResponse
144 }{}
145 err = api.postMethod(ctx, "users.conversations", values, &response)
146 if err != nil {
147 return nil, "", err
148 }
149
150 return response.Channels, response.ResponseMetaData.NextCursor, response.Err()
151 }
152
153 // ArchiveConversation archives a conversation
154 func (api *Client) ArchiveConversation(channelID string) error {
155 return api.ArchiveConversationContext(context.Background(), channelID)
156 }
157
158 // ArchiveConversationContext archives a conversation with a custom context
159 func (api *Client) ArchiveConversationContext(ctx context.Context, channelID string) error {
160 values := url.Values{
161 "token": {api.token},
162 "channel": {channelID},
163 }
164
165 response := SlackResponse{}
166 err := api.postMethod(ctx, "conversations.archive", values, &response)
167 if err != nil {
168 return err
169 }
170
171 return response.Err()
172 }
173
174 // UnArchiveConversation reverses conversation archival
175 func (api *Client) UnArchiveConversation(channelID string) error {
176 return api.UnArchiveConversationContext(context.Background(), channelID)
177 }
178
179 // UnArchiveConversationContext reverses conversation archival with a custom context
180 func (api *Client) UnArchiveConversationContext(ctx context.Context, channelID string) error {
181 values := url.Values{
182 "token": {api.token},
183 "channel": {channelID},
184 }
185 response := SlackResponse{}
186 err := api.postMethod(ctx, "conversations.unarchive", values, &response)
187 if err != nil {
188 return err
189 }
190
191 return response.Err()
192 }
193
194 // SetTopicOfConversation sets the topic for a conversation
195 func (api *Client) SetTopicOfConversation(channelID, topic string) (*Channel, error) {
196 return api.SetTopicOfConversationContext(context.Background(), channelID, topic)
197 }
198
199 // SetTopicOfConversationContext sets the topic for a conversation with a custom context
200 func (api *Client) SetTopicOfConversationContext(ctx context.Context, channelID, topic string) (*Channel, error) {
201 values := url.Values{
202 "token": {api.token},
203 "channel": {channelID},
204 "topic": {topic},
205 }
206 response := struct {
207 SlackResponse
208 Channel *Channel `json:"channel"`
209 }{}
210 err := api.postMethod(ctx, "conversations.setTopic", values, &response)
211 if err != nil {
212 return nil, err
213 }
214
215 return response.Channel, response.Err()
216 }
217
218 // SetPurposeOfConversation sets the purpose for a conversation
219 func (api *Client) SetPurposeOfConversation(channelID, purpose string) (*Channel, error) {
220 return api.SetPurposeOfConversationContext(context.Background(), channelID, purpose)
221 }
222
223 // SetPurposeOfConversationContext sets the purpose for a conversation with a custom context
224 func (api *Client) SetPurposeOfConversationContext(ctx context.Context, channelID, purpose string) (*Channel, error) {
225 values := url.Values{
226 "token": {api.token},
227 "channel": {channelID},
228 "purpose": {purpose},
229 }
230 response := struct {
231 SlackResponse
232 Channel *Channel `json:"channel"`
233 }{}
234
235 err := api.postMethod(ctx, "conversations.setPurpose", values, &response)
236 if err != nil {
237 return nil, err
238 }
239
240 return response.Channel, response.Err()
241 }
242
243 // RenameConversation renames a conversation
244 func (api *Client) RenameConversation(channelID, channelName string) (*Channel, error) {
245 return api.RenameConversationContext(context.Background(), channelID, channelName)
246 }
247
248 // RenameConversationContext renames a conversation with a custom context
249 func (api *Client) RenameConversationContext(ctx context.Context, channelID, channelName string) (*Channel, error) {
250 values := url.Values{
251 "token": {api.token},
252 "channel": {channelID},
253 "name": {channelName},
254 }
255 response := struct {
256 SlackResponse
257 Channel *Channel `json:"channel"`
258 }{}
259
260 err := api.postMethod(ctx, "conversations.rename", values, &response)
261 if err != nil {
262 return nil, err
263 }
264
265 return response.Channel, response.Err()
266 }
267
268 // InviteUsersToConversation invites users to a channel
269 func (api *Client) InviteUsersToConversation(channelID string, users ...string) (*Channel, error) {
270 return api.InviteUsersToConversationContext(context.Background(), channelID, users...)
271 }
272
273 // InviteUsersToConversationContext invites users to a channel with a custom context
274 func (api *Client) InviteUsersToConversationContext(ctx context.Context, channelID string, users ...string) (*Channel, error) {
275 values := url.Values{
276 "token": {api.token},
277 "channel": {channelID},
278 "users": {strings.Join(users, ",")},
279 }
280 response := struct {
281 SlackResponse
282 Channel *Channel `json:"channel"`
283 }{}
284
285 err := api.postMethod(ctx, "conversations.invite", values, &response)
286 if err != nil {
287 return nil, err
288 }
289
290 return response.Channel, response.Err()
291 }
292
293 // KickUserFromConversation removes a user from a conversation
294 func (api *Client) KickUserFromConversation(channelID string, user string) error {
295 return api.KickUserFromConversationContext(context.Background(), channelID, user)
296 }
297
298 // KickUserFromConversationContext removes a user from a conversation with a custom context
299 func (api *Client) KickUserFromConversationContext(ctx context.Context, channelID string, user string) error {
300 values := url.Values{
301 "token": {api.token},
302 "channel": {channelID},
303 "user": {user},
304 }
305
306 response := SlackResponse{}
307 err := api.postMethod(ctx, "conversations.kick", values, &response)
308 if err != nil {
309 return err
310 }
311
312 return response.Err()
313 }
314
315 // CloseConversation closes a direct message or multi-person direct message
316 func (api *Client) CloseConversation(channelID string) (noOp bool, alreadyClosed bool, err error) {
317 return api.CloseConversationContext(context.Background(), channelID)
318 }
319
320 // CloseConversationContext closes a direct message or multi-person direct message with a custom context
321 func (api *Client) CloseConversationContext(ctx context.Context, channelID string) (noOp bool, alreadyClosed bool, err error) {
322 values := url.Values{
323 "token": {api.token},
324 "channel": {channelID},
325 }
326 response := struct {
327 SlackResponse
328 NoOp bool `json:"no_op"`
329 AlreadyClosed bool `json:"already_closed"`
330 }{}
331
332 err = api.postMethod(ctx, "conversations.close", values, &response)
333 if err != nil {
334 return false, false, err
335 }
336
337 return response.NoOp, response.AlreadyClosed, response.Err()
338 }
339
340 // CreateConversation initiates a public or private channel-based conversation
341 func (api *Client) CreateConversation(channelName string, isPrivate bool) (*Channel, error) {
342 return api.CreateConversationContext(context.Background(), channelName, isPrivate)
343 }
344
345 // CreateConversationContext initiates a public or private channel-based conversation with a custom context
346 func (api *Client) CreateConversationContext(ctx context.Context, channelName string, isPrivate bool) (*Channel, error) {
347 values := url.Values{
348 "token": {api.token},
349 "name": {channelName},
350 "is_private": {strconv.FormatBool(isPrivate)},
351 }
352 response, err := api.channelRequest(ctx, "conversations.create", values)
353 if err != nil {
354 return nil, err
355 }
356
357 return &response.Channel, nil
358 }
359
360 // GetConversationInfo retrieves information about a conversation
361 func (api *Client) GetConversationInfo(channelID string, includeLocale bool) (*Channel, error) {
362 return api.GetConversationInfoContext(context.Background(), channelID, includeLocale)
363 }
364
365 // GetConversationInfoContext retrieves information about a conversation with a custom context
366 func (api *Client) GetConversationInfoContext(ctx context.Context, channelID string, includeLocale bool) (*Channel, error) {
367 values := url.Values{
368 "token": {api.token},
369 "channel": {channelID},
370 "include_locale": {strconv.FormatBool(includeLocale)},
371 }
372 response, err := api.channelRequest(ctx, "conversations.info", values)
373 if err != nil {
374 return nil, err
375 }
376
377 return &response.Channel, response.Err()
378 }
379
380 // LeaveConversation leaves a conversation
381 func (api *Client) LeaveConversation(channelID string) (bool, error) {
382 return api.LeaveConversationContext(context.Background(), channelID)
383 }
384
385 // LeaveConversationContext leaves a conversation with a custom context
386 func (api *Client) LeaveConversationContext(ctx context.Context, channelID string) (bool, error) {
387 values := url.Values{
388 "token": {api.token},
389 "channel": {channelID},
390 }
391
392 response, err := api.channelRequest(ctx, "conversations.leave", values)
393 if err != nil {
394 return false, err
395 }
396
397 return response.NotInChannel, err
398 }
399
400 type GetConversationRepliesParameters struct {
401 ChannelID string
402 Timestamp string
403 Cursor string
404 Inclusive bool
405 Latest string
406 Limit int
407 Oldest string
408 }
409
410 // GetConversationReplies retrieves a thread of messages posted to a conversation
411 func (api *Client) GetConversationReplies(params *GetConversationRepliesParameters) (msgs []Message, hasMore bool, nextCursor string, err error) {
412 return api.GetConversationRepliesContext(context.Background(), params)
413 }
414
415 // GetConversationRepliesContext retrieves a thread of messages posted to a conversation with a custom context
416 func (api *Client) GetConversationRepliesContext(ctx context.Context, params *GetConversationRepliesParameters) (msgs []Message, hasMore bool, nextCursor string, err error) {
417 values := url.Values{
418 "token": {api.token},
419 "channel": {params.ChannelID},
420 "ts": {params.Timestamp},
421 }
422 if params.Cursor != "" {
423 values.Add("cursor", params.Cursor)
424 }
425 if params.Latest != "" {
426 values.Add("latest", params.Latest)
427 }
428 if params.Limit != 0 {
429 values.Add("limit", strconv.Itoa(params.Limit))
430 }
431 if params.Oldest != "" {
432 values.Add("oldest", params.Oldest)
433 }
434 if params.Inclusive {
435 values.Add("inclusive", "1")
436 } else {
437 values.Add("inclusive", "0")
438 }
439 response := struct {
440 SlackResponse
441 HasMore bool `json:"has_more"`
442 ResponseMetaData struct {
443 NextCursor string `json:"next_cursor"`
444 } `json:"response_metadata"`
445 Messages []Message `json:"messages"`
446 }{}
447
448 err = api.postMethod(ctx, "conversations.replies", values, &response)
449 if err != nil {
450 return nil, false, "", err
451 }
452
453 return response.Messages, response.HasMore, response.ResponseMetaData.NextCursor, response.Err()
454 }
455
456 type GetConversationsParameters struct {
457 Cursor string
458 ExcludeArchived bool
459 Limit int
460 Types []string
461 TeamID string
462 }
463
464 // GetConversations returns the list of channels in a Slack team
465 func (api *Client) GetConversations(params *GetConversationsParameters) (channels []Channel, nextCursor string, err error) {
466 return api.GetConversationsContext(context.Background(), params)
467 }
468
469 // GetConversationsContext returns the list of channels in a Slack team with a custom context
470 func (api *Client) GetConversationsContext(ctx context.Context, params *GetConversationsParameters) (channels []Channel, nextCursor string, err error) {
471 values := url.Values{
472 "token": {api.token},
473 }
474 if params.Cursor != "" {
475 values.Add("cursor", params.Cursor)
476 }
477 if params.Limit != 0 {
478 values.Add("limit", strconv.Itoa(params.Limit))
479 }
480 if params.Types != nil {
481 values.Add("types", strings.Join(params.Types, ","))
482 }
483 if params.ExcludeArchived {
484 values.Add("exclude_archived", strconv.FormatBool(params.ExcludeArchived))
485 }
486 if params.TeamID != "" {
487 values.Add("team_id", params.TeamID)
488 }
489
490 response := struct {
491 Channels []Channel `json:"channels"`
492 ResponseMetaData responseMetaData `json:"response_metadata"`
493 SlackResponse
494 }{}
495
496 err = api.postMethod(ctx, "conversations.list", values, &response)
497 if err != nil {
498 return nil, "", err
499 }
500
501 return response.Channels, response.ResponseMetaData.NextCursor, response.Err()
502 }
503
504 type OpenConversationParameters struct {
505 ChannelID string
506 ReturnIM bool
507 Users []string
508 }
509
510 // OpenConversation opens or resumes a direct message or multi-person direct message
511 func (api *Client) OpenConversation(params *OpenConversationParameters) (*Channel, bool, bool, error) {
512 return api.OpenConversationContext(context.Background(), params)
513 }
514
515 // OpenConversationContext opens or resumes a direct message or multi-person direct message with a custom context
516 func (api *Client) OpenConversationContext(ctx context.Context, params *OpenConversationParameters) (*Channel, bool, bool, error) {
517 values := url.Values{
518 "token": {api.token},
519 "return_im": {strconv.FormatBool(params.ReturnIM)},
520 }
521 if params.ChannelID != "" {
522 values.Add("channel", params.ChannelID)
523 }
524 if params.Users != nil {
525 values.Add("users", strings.Join(params.Users, ","))
526 }
527 response := struct {
528 Channel *Channel `json:"channel"`
529 NoOp bool `json:"no_op"`
530 AlreadyOpen bool `json:"already_open"`
531 SlackResponse
532 }{}
533
534 err := api.postMethod(ctx, "conversations.open", values, &response)
535 if err != nil {
536 return nil, false, false, err
537 }
538
539 return response.Channel, response.NoOp, response.AlreadyOpen, response.Err()
540 }
541
542 // JoinConversation joins an existing conversation
543 func (api *Client) JoinConversation(channelID string) (*Channel, string, []string, error) {
544 return api.JoinConversationContext(context.Background(), channelID)
545 }
546
547 // JoinConversationContext joins an existing conversation with a custom context
548 func (api *Client) JoinConversationContext(ctx context.Context, channelID string) (*Channel, string, []string, error) {
549 values := url.Values{"token": {api.token}, "channel": {channelID}}
550 response := struct {
551 Channel *Channel `json:"channel"`
552 Warning string `json:"warning"`
553 ResponseMetaData *struct {
554 Warnings []string `json:"warnings"`
555 } `json:"response_metadata"`
556 SlackResponse
557 }{}
558
559 err := api.postMethod(ctx, "conversations.join", values, &response)
560 if err != nil {
561 return nil, "", nil, err
562 }
563 if response.Err() != nil {
564 return nil, "", nil, response.Err()
565 }
566 var warnings []string
567 if response.ResponseMetaData != nil {
568 warnings = response.ResponseMetaData.Warnings
569 }
570 return response.Channel, response.Warning, warnings, nil
571 }
572
573 type GetConversationHistoryParameters struct {
574 ChannelID string
575 Cursor string
576 Inclusive bool
577 Latest string
578 Limit int
579 Oldest string
580 IncludeAllMetadata bool
581 }
582
583 type GetConversationHistoryResponse struct {
584 SlackResponse
585 HasMore bool `json:"has_more"`
586 PinCount int `json:"pin_count"`
587 Latest string `json:"latest"`
588 ResponseMetaData struct {
589 NextCursor string `json:"next_cursor"`
590 } `json:"response_metadata"`
591 Messages []Message `json:"messages"`
592 }
593
594 // GetConversationHistory joins an existing conversation
595 func (api *Client) GetConversationHistory(params *GetConversationHistoryParameters) (*GetConversationHistoryResponse, error) {
596 return api.GetConversationHistoryContext(context.Background(), params)
597 }
598
599 // GetConversationHistoryContext joins an existing conversation with a custom context
600 func (api *Client) GetConversationHistoryContext(ctx context.Context, params *GetConversationHistoryParameters) (*GetConversationHistoryResponse, error) {
601 values := url.Values{"token": {api.token}, "channel": {params.ChannelID}}
602 if params.Cursor != "" {
603 values.Add("cursor", params.Cursor)
604 }
605 if params.Inclusive {
606 values.Add("inclusive", "1")
607 } else {
608 values.Add("inclusive", "0")
609 }
610 if params.Latest != "" {
611 values.Add("latest", params.Latest)
612 }
613 if params.Limit != 0 {
614 values.Add("limit", strconv.Itoa(params.Limit))
615 }
616 if params.Oldest != "" {
617 values.Add("oldest", params.Oldest)
618 }
619 if params.IncludeAllMetadata {
620 values.Add("include_all_metadata", "1")
621 } else {
622 values.Add("include_all_metadata", "0")
623 }
624
625 response := GetConversationHistoryResponse{}
626
627 err := api.postMethod(ctx, "conversations.history", values, &response)
628 if err != nil {
629 return nil, err
630 }
631
632 return &response, response.Err()
633 }
634
635 // MarkConversation sets the read mark of a conversation to a specific point
636 func (api *Client) MarkConversation(channel, ts string) (err error) {
637 return api.MarkConversationContext(context.Background(), channel, ts)
638 }
639
640 // MarkConversationContext sets the read mark of a conversation to a specific point with a custom context
641 func (api *Client) MarkConversationContext(ctx context.Context, channel, ts string) error {
642 values := url.Values{
643 "token": {api.token},
644 "channel": {channel},
645 "ts": {ts},
646 }
647
648 response := &SlackResponse{}
649
650 err := api.postMethod(ctx, "conversations.mark", values, response)
651 if err != nil {
652 return err
653 }
654 return response.Err()
655 }