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 pathchannels.go
190 lines (172 loc) · 4.76 KB
/
channels.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
package slack
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"time"
)
// API channels.list: Lists all channels in a Slack team.
func (sl *Slack) ChannelsList() ([]*Channel, error) {
uv := sl.urlValues()
body, err := sl.GetRequest(channelsListApiEndpoint, uv)
if err != nil {
return nil, err
}
res := new(ChannelsListAPIResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, err
}
if !res.Ok {
return nil, errors.New(res.Error)
}
return res.Channels()
}
// response type for `channels.list` api
type ChannelsListAPIResponse struct {
BaseAPIResponse
RawChannels json.RawMessage `json:"channels"`
}
// slack channel type
type Channel struct {
Id string `json:"id"`
Name string `json:"name"`
IsChannel bool `json:"is_channel"`
Created int `json:"created"`
Creator string `json:"creator"`
IsArchived bool `json:"is_archived"`
IsGeneral bool `json:"is_general"`
IsMember bool `json:"is_member"`
Members []string `json:"members"`
RawTopic json.RawMessage `json:"topic"`
RawPurpose json.RawMessage `json:"purpose"`
NumMembers int `json:"num_members"`
}
// Channels returns a slice of channel object from a response of `channels.list` api.
func (res *ChannelsListAPIResponse) Channels() ([]*Channel, error) {
var chs []*Channel
err := json.Unmarshal(res.RawChannels, &chs)
if err != nil {
return nil, err
}
return chs, nil
}
func (ch *Channel) Topic() (*Topic, error) {
tp := new(Topic)
err := json.Unmarshal(ch.RawTopic, tp)
if err != nil {
return nil, err
}
return tp, nil
}
func (ch *Channel) Purpose() (*Purpose, error) {
pp := new(Purpose)
err := json.Unmarshal(ch.RawPurpose, pp)
if err != nil {
return nil, err
}
return pp, nil
}
// FindChannel returns a channel object that satisfy conditions specified.
func (sl *Slack) FindChannel(cb func(*Channel) bool) (*Channel, error) {
channels, err := sl.ChannelsList()
if err != nil {
return nil, err
}
for _, channel := range channels {
if cb(channel) {
return channel, nil
}
}
return nil, errors.New("No such channel.")
}
// FindChannelByName returns a channel object that matches name specified.
func (sl *Slack) FindChannelByName(name string) (*Channel, error) {
return sl.FindChannel(func(channel *Channel) bool {
return channel.Name == name
})
}
// API channels.join: Joins a channel, creating it if needed.
func (sl *Slack) JoinChannel(name string) error {
uv := sl.urlValues()
uv.Add("name", name)
_, err := sl.GetRequest(channelsJoinApiEndpoint, uv)
if err != nil {
return err
}
return nil
}
type Message struct {
Type string `json:"type"`
Ts string `json:"ts"`
UserId string `json:"user"`
Text string `json:"text"`
Subtype string `json:"subtype"`
}
func (msg *Message) Timestamp() *time.Time {
seconds, _ := strconv.ParseInt(msg.Ts[0:10], 10, 64)
microseconds, _ := strconv.ParseInt(msg.Ts[11:17], 10, 64)
ts := time.Unix(seconds, microseconds*1e3)
return &ts
}
// option type for `channels.history` api
type ChannelsHistoryOpt struct {
Channel string `json:"channel"`
Latest float64 `json:"latest"`
Oldest float64 `json:"oldest"`
Inclusive int `json:"inclusive"`
Count int `json:"count"`
UnReads int `json:"unreads,omitempty"`
}
func (opt *ChannelsHistoryOpt) Bind(uv *url.Values) error {
uv.Add("channel", opt.Channel)
if opt.Latest != 0.0 {
uv.Add("lastest", strconv.FormatFloat(opt.Latest, 'f', 6, 64))
}
if opt.Oldest != 0.0 {
uv.Add("oldest", strconv.FormatFloat(opt.Oldest, 'f', 6, 64))
}
uv.Add("inclusive", strconv.Itoa(opt.Inclusive))
if opt.Count != 0 {
uv.Add("count", strconv.Itoa(opt.Count))
}
uv.Add("unreads", strconv.Itoa(opt.UnReads))
return nil
}
// response type for `channels.history` api
type ChannelsHistoryResponse struct {
BaseAPIResponse
Latest float64 `json:"latest"`
Messages []*Message `json:"messages"`
HasMore bool `json:"has_more"`
UnReadCountDisplay int `json:"unread_count_display"`
}
// API channels.history: Fetches history of messages and events from a channel.
func (sl *Slack) ChannelsHistory(opt *ChannelsHistoryOpt) (*ChannelsHistoryResponse, error) {
uv := sl.urlValues()
err := opt.Bind(uv)
if err != nil {
return nil, err
}
body, err := sl.GetRequest(channelsHistoryApiEndpoint, uv)
if err != nil {
return nil, err
}
res := new(ChannelsHistoryResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, err
}
if !res.Ok {
return nil, errors.New(res.Error)
}
return res, nil
}
func (sl *Slack) ChannelsHistoryMessages(opt *ChannelsHistoryOpt) ([]*Message, error) {
res, err := sl.ChannelsHistory(opt)
if err != nil {
return nil, err
}
return res.Messages, nil
}