This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmpims.go
67 lines (61 loc) · 1.57 KB
/
mpims.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
package slack
import (
"encoding/json"
"errors"
)
// API mpim.list: Lists multiparty direct message channels for the calling user.
func (sl *Slack) MpImList() ([]*MpIm, error) {
uv := sl.urlValues()
body, err := sl.GetRequest(mpimListApiEndpoint, uv)
if err != nil {
return nil, err
}
res := new(MpImListAPIResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, err
}
if !res.Ok {
return nil, errors.New(res.Error)
}
return res.MpIms()
}
// slack mpim type
type MpIm struct {
Id string `json:"id"`
Name string `json:"name"`
Created int64 `json:"created"`
Creator string `json:"creator"`
IsArchived bool `json:"is_archived"`
IsMpim bool `json:"is_mpim"`
Members []string `json:"members"`
RawTopic json.RawMessage `json:"topic"`
RawPurpose json.RawMessage `json:"purpose"`
}
// response type for `im.list` api
type MpImListAPIResponse struct {
BaseAPIResponse
RawMpIms json.RawMessage `json:"groups"`
}
// MpIms returns a slice of mpim object from `mpim.list` api.
func (res *MpImListAPIResponse) MpIms() ([]*MpIm, error) {
var mpim []*MpIm
err := json.Unmarshal(res.RawMpIms, &mpim)
if err != nil {
return nil, err
}
return mpim, nil
}
// FindMpIm returns a mpim object that satisfy conditions specified.
func (sl *Slack) FindMpIm(cb func(*MpIm) bool) (*MpIm, error) {
mpims, err := sl.MpImList()
if err != nil {
return nil, err
}
for _, mpim := range mpims {
if cb(mpim) {
return mpim, nil
}
}
return nil, errors.New("No such mpim.")
}