This repository has been archived by the owner on Dec 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
263 lines (225 loc) · 6.46 KB
/
client.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
package conslack
import (
"fmt"
"github.com/nlopes/slack"
)
// Client represents the client interacting with Slack.
type Client struct {
api *slack.Client
cache map[string]string
currentUserID string
channelSubscriptions map[string]chan Message
}
// Message represents a message coming from the Slack API.
type Message struct {
Channel string
From string
Date string // TODO: This should probably be a time.Time.
Text string
}
// Discussion represents a channel or a user
type Discussion struct {
ID string
Name string
}
// New returns a new Client configured with the proper token.
// This function tries to authenticate on Slack. If an error occurs, the error
// is returned to the user.
// A websocket connection is also opened(RTM) to handle updates from Slack.
// There is currently no way to close it.
func New(token string) (*Client, error) {
c := &Client{
api: slack.New(token),
cache: make(map[string]string),
channelSubscriptions: make(map[string]chan Message),
}
// Trying to authenticate on Slack
auth, err := c.api.AuthTest()
if err != nil {
return nil, err
}
// storing the user id on the client. Maybe useful at some point?
c.currentUserID = auth.UserID
// We are getting all the users to store them on an in memory cache.
users, err := c.api.GetUsers()
if err != nil {
return nil, err
}
for _, u := range users {
c.cache[u.ID] = fmt.Sprintf("@%v", u.Name)
}
// We are getting all the channels to store them on an in memory cache.
channels, err := c.api.GetChannels(true)
if err != nil {
return nil, err
}
for _, ch := range channels {
c.cache[ch.ID] = fmt.Sprintf("#%v", ch.Name)
}
// Starting the websocket handling process in a Goroutine.
go c.processEvents()
return c, nil
}
// History returns the discussion history(100 latest messages) for the given channelName.
// channelName can be a #channel or a @user.
func (c *Client) History(channelName string) ([]Message, error) {
// We must convert the channel name to the channel ID.
id, err := c.cacheGetUserIDFromName(channelName)
if err != nil {
return nil, err
}
params := slack.HistoryParameters{
Count: 100,
Inclusive: false,
Unreads: false,
}
var h *slack.History
switch id[0] {
case 'C':
// If the ID belongs to a channel
h, err = c.api.GetChannelHistory(id, params)
if err != nil {
return nil, err
}
case 'U':
// if the ID belongs to a user, we are getting the IM channel(this could also be cached at some point)
_, _, channelID, err := c.api.OpenIMChannel(id)
if err != nil {
return nil, err
}
h, err = c.api.GetIMHistory(channelID, params)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("channel type not handled")
}
var messages []Message
for _, m := range h.Messages {
messages = append([]Message{Message{
From: c.cache[m.User],
Date: formatTimestamp(m.Timestamp),
Text: m.Text, //TODO: should parse text and replace <@U0549KTN8|arkan> with @arkan for example
}}, messages...)
}
return messages, nil
}
// Messages is used to retrieve in realtime messages from a given channelName.
// This method returns 3 parameters:
// - A read-only chan of Message that can be read to get any new message in realtime.
// - A close function that must be closed once you no longer to to get updates. It will close the channel and cleanup event routing locally.
// - And a potential error
func (c *Client) Messages(channelName string) (<-chan Message, func(), error) {
var channelID string
if channelName[0] == '#' {
var err error
channelID, err = c.cacheGetUserIDFromName(channelName)
if err != nil {
return nil, nil, err
}
} else if channelName[0] == '@' {
id, err := c.cacheGetUserIDFromName(channelName)
if err != nil {
return nil, nil, err
}
_, _, channelID, err = c.api.OpenIMChannel(id)
if err != nil {
return nil, nil, err
}
} else {
return nil, nil, fmt.Errorf("unknown id")
}
// TODO: this not not safe - this should be protected by a mutex
ch, ok := c.channelSubscriptions[channelID]
if !ok {
ch = make(chan Message)
c.channelSubscriptions[channelID] = ch
}
close := func() {
ch, ok := c.channelSubscriptions[channelID]
if ok {
close(ch)
delete(c.channelSubscriptions, channelID)
}
}
return ch, close, nil
}
// PostMessage posts a message to channelName.
// channelName can be a #channel or a @user.
func (c *Client) PostMessage(channelName string, message string) error {
// https://godoc.org/github.com/nlopes/slack#PostMessageParameters
postParams := slack.PostMessageParameters{
AsUser: true,
}
id, err := c.cacheGetUserIDFromName(channelName)
if err != nil {
return err
}
// https://godoc.org/github.com/nlopes/slack#Client.PostMessage
_, _, err = c.api.PostMessage(id, message, postParams)
return err
}
// GetDiscussions returns all the discussions the user can talk to.
func (c *Client) GetDiscussions() ([]Discussion, error) {
var discussions []Discussion
channels, err := c.api.GetChannels(false)
if err != nil {
return nil, err
}
for _, c := range channels {
discussions = append(discussions, Discussion{
ID: c.ID,
Name: fmt.Sprintf("#%v", c.Name),
})
}
// TODO: add users too
return discussions, nil
}
// processEvents processes events from Slack in realtime with the websocket(RTM)
func (c *Client) processEvents() {
rtm := c.api.NewRTM()
go rtm.ManageConnection()
for {
select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.HelloEvent:
case *slack.ConnectedEvent:
case *slack.MessageEvent:
//TODO: handle update/delete/etc... here
//TODO: add an ID to the message as well - could be a slack.NewRefMsg
c.dispatchMessage(Message{
Channel: ev.Channel,
From: c.cache[ev.User],
Text: ev.Text,
Date: formatTimestamp(ev.Timestamp),
})
case *slack.PresenceChangeEvent:
case *slack.LatencyReport:
case *slack.RTMError:
fmt.Printf("Error: %s\n", ev.Error())
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
return
default:
}
}
}
}
// dispatchMessage dispatches the message to the subscriber.
func (c *Client) dispatchMessage(m Message) {
ch, ok := c.channelSubscriptions[m.Channel]
if !ok {
return
}
ch <- m
}
// TODO: this must be rewritten
func (c *Client) cacheGetUserIDFromName(n string) (string, error) {
for k, v := range c.cache {
if v == n {
return k, nil
}
}
return "", fmt.Errorf("channel not found in cache")
}