-
Notifications
You must be signed in to change notification settings - Fork 43
/
slack.go
409 lines (337 loc) · 12.2 KB
/
slack.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
package client
//go:generate $GOPATH/bin/mockery --output ../mocks --name SlackClient
import (
"fmt"
"strings"
"sync"
"github.com/innogames/slack-bot/v2/bot/config"
"github.com/innogames/slack-bot/v2/bot/msg"
"github.com/innogames/slack-bot/v2/bot/util"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
)
// InternalMessages is internal queue of internal messages
// @deprecated -> use HandleMessageWithDoneHandler instead
var InternalMessages = make(chan msg.Message, 50)
// HandleMessage will register the given message in the queue...and returns a sync.WaitGroup which can be used to see when the message is handled
func HandleMessage(message msg.Message) {
message.Text = strings.TrimSpace(message.Text)
if message.Text == "" {
return
}
InternalMessages <- message
}
// HandleMessageWithDoneHandler will register the given message in the queue...and returns a sync.WaitGroup which can be used to see when the message is handled
func HandleMessageWithDoneHandler(message msg.Message) *sync.WaitGroup {
done := message.AddDoneHandler()
if message.Text != "" {
HandleMessage(message)
} else {
// if we have no text, mark the message as processed to avoid open lock
done.Done()
}
return done
}
// AuthResponse is holding some basic Slack metadata for the current connection, like Bot-Id, Workspace etc
var AuthResponse slack.AuthTestResponse
// AllUsers is a lookup from user-id to user-name
var AllUsers config.UserMap
// AllChannels is a map of each channelsId and the name
var AllChannels map[string]string
// GetSlackClient establishes a connection to the slack server.
func GetSlackClient(cfg config.Slack) (*Slack, error) {
if !strings.HasPrefix(cfg.Token, "xoxb-") {
return nil, fmt.Errorf("config slack.token needs to start with 'xoxb-'")
} else if !strings.HasPrefix(cfg.SocketToken, "xapp-") {
return nil, fmt.Errorf("config slack.socket_token needs to start with 'xapp-'")
}
options := []slack.Option{
slack.OptionHTTPClient(GetHTTPClient()),
slack.OptionAppLevelToken(cfg.SocketToken),
}
var socketModeOptions []socketmode.Option
if cfg.TestEndpointURL != "" {
options = append(options, slack.OptionAPIURL(cfg.TestEndpointURL))
}
if cfg.Debug {
options = append(options, slack.OptionDebug(true))
socketModeOptions = append(socketModeOptions, socketmode.OptionDebug(true))
}
rawClient := slack.New(cfg.Token, options...)
socket := socketmode.New(
rawClient,
socketModeOptions...,
)
return &Slack{Client: rawClient, Socket: socket, config: cfg}, nil
}
// SlackClient is the main interface which is used for all commands to interact with slack
// for tests we have a mock for it, for production use, we use a slack.Client with some custom logic
type SlackClient interface {
// ReplyError Replies a error to the current channel/user/thread + log it!
ReplyError(ref msg.Ref, err error)
// SendMessage is the extended version of Reply and accepts any slack.MsgOption
SendMessage(ref msg.Ref, text string, options ...slack.MsgOption) string
// SendEphemeralMessage sends a message just visible to the current user
SendEphemeralMessage(ref msg.Ref, text string, options ...slack.MsgOption)
SendBlockMessageToUser(user string, blocks []slack.Block, options ...slack.MsgOption) string
// SendBlockMessage will send Slack Blocks/Sections to the target
SendBlockMessage(ref msg.Ref, blocks []slack.Block, options ...slack.MsgOption) string
// SendToUser sends a simple text message to a user, using "@username or @U12334"
SendToUser(user string, text string)
RemoveReaction(reaction util.Reaction, ref msg.Ref)
AddReaction(reaction util.Reaction, ref msg.Ref)
GetReactions(item slack.ItemRef, params slack.GetReactionsParameters) ([]slack.ItemReaction, error)
// GetConversationHistory loads the message history from slack
GetConversationHistory(*slack.GetConversationHistoryParameters) (*slack.GetConversationHistoryResponse, error)
// GetThreadMessages loads message from a given thread
GetThreadMessages(ref msg.Ref) ([]slack.Message, error)
// GetUserPresence returns the current presence of a user, using the "users.getPresence" API
GetUserPresence(user string) (*slack.UserPresence, error)
// UploadFile uploads a file to Slack
UploadFile(params slack.FileUploadParameters) (*slack.File, error)
}
// Slack is wrapper to the slack.Client which also holds the the socketmode.Client and all needed config
type Slack struct {
*slack.Client
Socket *socketmode.Client
config config.Slack
}
// AddReaction will add a reaction from the given message
func (s *Slack) AddReaction(reaction util.Reaction, ref msg.Ref) {
err := s.Client.AddReaction(reaction.ToSlackReaction(), slack.NewRefToMessage(ref.GetChannel(), ref.GetTimestamp()))
if err != nil {
log.WithError(err).Warnf("Error while adding reaction %s", reaction)
}
}
// RemoveReaction will remove a reaction from the given message
func (s *Slack) RemoveReaction(reaction util.Reaction, ref msg.Ref) {
err := s.Client.RemoveReaction(reaction.ToSlackReaction(), slack.NewRefToMessage(ref.GetChannel(), ref.GetTimestamp()))
if err != nil {
log.WithError(err).Warnf("Error while removing reaction %s", reaction)
}
}
// SendEphemeralMessage sends a message just visible to the current user
// see https://api.slack.com/methods/chat.postEphemeral
func (s *Slack) SendEphemeralMessage(ref msg.Ref, text string, options ...slack.MsgOption) {
options = append(
options,
slack.MsgOptionAsUser(true),
slack.MsgOptionTS(ref.GetThread()), // send in current thread by default
slack.MsgOptionText(text, false),
)
_, err := s.Client.PostEphemeral(
ref.GetChannel(),
ref.GetUser(),
options...,
)
if err != nil {
log.Warn(errors.Wrapf(err, "Error while sending Ephemeral message %s", err))
}
}
// SendMessage is the "slow" reply via POST request, needed for Attachment or MsgRef
func (s *Slack) SendMessage(ref msg.Ref, text string, options ...slack.MsgOption) string {
if ref.GetChannel() == "" {
log.Warnf("no channel given: %s", ref)
return ""
}
if len(options) == 0 && text == "" {
// ignore empty messages
return ""
}
defaultOptions := []slack.MsgOption{
slack.MsgOptionTS(ref.GetThread()), // send in current thread by default
slack.MsgOptionAsUser(true),
slack.MsgOptionText(text, false),
slack.MsgOptionDisableLinkUnfurl(),
}
options = append(defaultOptions, options...)
_, msgTimestamp, err := s.PostMessage(
ref.GetChannel(),
options...,
)
if err != nil {
log.
WithField("user", ref.GetUser()).
Error(err.Error())
}
return msgTimestamp
}
// ReplyError send a error message as a reply to the user and log it in the log + send it to ErrorChannel (if defined)
func (s *Slack) ReplyError(ref msg.Ref, err error) {
log.WithError(err).Warnf("Error while sending reply")
s.SendMessage(ref, err.Error())
if s.config.ErrorChannel != "" {
text := fmt.Sprintf(
"<@%s> Error in command: %s",
ref.GetUser(),
err.Error(),
)
message := msg.Message{}
message.Channel, _ = GetChannelIDAndName(s.config.ErrorChannel)
s.SendMessage(message, text)
}
}
// SendToUser sends a message to any user via IM channel
func (s *Slack) SendToUser(user string, text string) {
channel, err := s.getUserConversation(user)
if err != nil {
log.WithError(err).Errorf("Cannot send Slack message")
return
}
message := msg.Message{}
message.Channel = channel
s.SendMessage(message, text)
}
// SendBlockMessageToUser will send Slack Blocks/Sections to the target
func (s *Slack) SendBlockMessageToUser(user string, blocks []slack.Block, options ...slack.MsgOption) string {
channel, err := s.getUserConversation(user)
if err != nil {
log.WithError(err).Errorf("Cannot send Slack message")
return ""
}
message := msg.Message{}
message.Channel = channel
return s.SendBlockMessage(message, blocks, options...)
}
// internal function to get the conversation id for a direct message user conversation
func (s *Slack) getUserConversation(user string) (string, error) {
// check if a real username was passed -> we need the user-id here
userID, _ := GetUserIDAndName(user)
if userID == "" {
return "", fmt.Errorf("invalid user: %s", user)
}
conversationOptions := &slack.OpenConversationParameters{
Users: []string{userID},
}
channel, _, _, err := s.Client.OpenConversation(conversationOptions)
if err != nil {
return "", fmt.Errorf("cannot open channel with user %s/%s", user, userID)
}
return channel.ID, nil
}
// SendBlockMessage will send Slack Blocks/Sections to the target
func (s *Slack) SendBlockMessage(ref msg.Ref, blocks []slack.Block, options ...slack.MsgOption) string {
allOptions := []slack.MsgOption{
slack.MsgOptionBlocks(blocks...),
}
return s.SendMessage(ref, "", append(allOptions, options...)...)
}
// GetThreadMessages will send Slack Blocks/Sections to the target
func (s *Slack) GetThreadMessages(ref msg.Ref) ([]slack.Message, error) {
allMessages := make([]slack.Message, 0)
var cursor string
var err error
for {
options := &slack.GetConversationRepliesParameters{
ChannelID: ref.GetChannel(),
Timestamp: ref.GetThread(),
Limit: 1000,
Cursor: cursor,
}
var messages []slack.Message
messages, _, cursor, err = s.Client.GetConversationReplies(options)
if err != nil {
return allMessages, err
}
allMessages = append(allMessages, messages...)
if cursor == "" {
break
}
}
return allMessages, nil
}
// GetUserPresence returns the current presence of a user, using the "users.getPresence" API
func (s *Slack) GetUserPresence(user string) (*slack.UserPresence, error) {
return s.Client.GetUserPresence(user)
}
// UploadFile uploads a file to Slack
func (s *Slack) UploadFile(params slack.FileUploadParameters) (*slack.File, error) {
return s.Client.UploadFile(params)
}
// GetUserIDAndName returns the user-id and user-name based on a identifier. If can get a user-id or name
func GetUserIDAndName(identifier string) (id string, name string) {
identifier = strings.TrimPrefix(identifier, "@")
if name, ok := AllUsers[identifier]; ok {
return identifier, name
}
identifier = strings.ToLower(identifier)
for id, name := range AllUsers {
if strings.EqualFold(name, identifier) {
return id, name
}
}
return "", ""
}
// GetChannelIDAndName returns channel-id and channel-name by an identifier which can be an id or a name
func GetChannelIDAndName(identifier string) (id string, name string) {
identifier = strings.TrimPrefix(identifier, "#")
if name, ok := AllChannels[identifier]; ok {
return identifier, name
}
identifier = strings.ToLower(identifier)
for id, name := range AllChannels {
if strings.EqualFold(name, identifier) {
return id, name
}
}
return "", ""
}
// GetSlackLink generates a "link button" as a slack.AttachmentAction which will
// open the given URL in the Slack client (when pressed)
func GetSlackLink(name string, url string, args ...string) slack.AttachmentAction {
style := "default"
if len(args) > 0 {
style = args[0]
}
action := slack.AttachmentAction{}
action.Style = style
action.Type = "button"
action.Text = name
action.URL = url
return action
}
// GetTextBlock wraps a simple text in a Slack Block Section
func GetTextBlock(text string) *slack.SectionBlock {
return slack.NewSectionBlock(
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: text,
},
nil,
nil,
)
}
// GetContextBlock generates a "Context block"
// https://api.slack.com/reference/block-kit/blocks#context
func GetContextBlock(text string) *slack.ContextBlock {
return slack.NewContextBlock(
"",
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: text,
},
)
}
// GetInteractionButton generates a block "Button" which is able to execute the given command once
// https://api.slack.com/reference/block-kit/blocks#actions
func GetInteractionButton(id, text, command string, args ...slack.Style) *slack.ButtonBlockElement {
var style slack.Style
if len(args) > 0 {
style = args[0]
}
buttonText := slack.NewTextBlockObject("plain_text", text, true, false)
button := slack.NewButtonBlockElement(id, command, buttonText)
button.Style = style
return button
}
// GetSlackArchiveLink returns a permalink to the ref which can be shared
func GetSlackArchiveLink(message msg.Ref) string {
return fmt.Sprintf(
"%sarchives/%s/p%s",
AuthResponse.URL,
message.GetChannel(),
strings.ReplaceAll(message.GetTimestamp(), ".", ""),
)
}