-
Notifications
You must be signed in to change notification settings - Fork 9
/
adapter.go
337 lines (281 loc) · 8.54 KB
/
adapter.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
// Package slack implements a slack adapter for the joe bot library.
package slack
import (
"context"
"fmt"
"strings"
"sync"
"github.com/go-joe/joe"
"github.com/go-joe/joe/reactions"
"github.com/slack-go/slack"
"go.uber.org/zap"
)
// BotAdapter implements a joe.Adapter that reads and writes messages to and
// from Slack using the RTM API.
type BotAdapter struct {
context context.Context
logger *zap.Logger
name string
userID string
logUnknownMessageTypes bool
listenPassive bool
sendMsgParams slack.PostMessageParameters
slack slackAPI
rtm slackRTM
events chan slackEvent
usersMu sync.RWMutex
users map[string]joe.User
}
type slackEvent struct {
Type string
Data interface{}
}
type slackAPI interface {
AuthTestContext(context.Context) (*slack.AuthTestResponse, error)
PostMessageContext(ctx context.Context, channelID string, opts ...slack.MsgOption) (respChannel, respTimestamp string, err error)
AddReactionContext(ctx context.Context, name string, item slack.ItemRef) error
GetUserInfo(user string) (*slack.User, error)
}
type slackRTM interface {
Disconnect() error
}
// Adapter returns a new BotAdapter as joe.Module.
//
// Apart from the typical joe.ReceiveMessageEvent event, this adapter also emits
// the joe.UserTypingEvent. The ReceiveMessageEvent.Data field is always a
// pointer to the corresponding github.com/slack-go/slack.MessageEvent instance.
func Adapter(token string, opts ...Option) joe.Module {
return joe.ModuleFunc(func(joeConf *joe.Config) error {
conf, err := newConf(token, joeConf, opts)
if err != nil {
return err
}
a, err := NewAdapter(joeConf.Context, conf)
if err != nil {
return err
}
joeConf.SetAdapter(a)
return nil
})
}
func newConf(token string, joeConf *joe.Config, opts []Option) (Config, error) {
conf := Config{Token: token, Name: joeConf.Name}
conf.SendMsgParams = slack.PostMessageParameters{
LinkNames: 1,
Parse: "full",
AsUser: true,
}
for _, opt := range opts {
err := opt(&conf)
if err != nil {
return conf, err
}
}
if conf.Logger == nil {
conf.Logger = joeConf.Logger("slack")
}
return conf, nil
}
// NewAdapter creates a new *BotAdapter that connects to Slack using the RTM API.
// Note that you will usually configure the slack adapter as joe.Module (i.e.
// using the Adapter function of this package).
//
// You need to close the adapter if it has been created without error in order
// to release the connection to the Slack RTM API.
func NewAdapter(ctx context.Context, conf Config) (*BotAdapter, error) {
client := slack.New(conf.Token, conf.slackOptions()...)
rtm := client.NewRTM()
events := make(chan slackEvent)
a, err := newAdapter(ctx, client, rtm, events, conf)
if err != nil {
return nil, err
}
// Start managing the slack Real Time Messaging (RTM) connection.
// This goroutine is closed when the BotAdapter disconnects from slack in
// BotAdapter.Close()
go rtm.ManageConnection()
// We need to translate the RTMEvent channel into the more generic slackEvent
// channel which is used by the BotAdapter internally.
go func() {
defer close(events)
for evt := range rtm.IncomingEvents {
events <- slackEvent{
Type: evt.Type,
Data: evt.Data,
}
if x, ok := evt.Data.(*slack.DisconnectedEvent); ok && x.Intentional {
return
}
}
}()
return a, nil
}
func newAdapter(ctx context.Context, client slackAPI, rtm slackRTM, events chan slackEvent, conf Config) (*BotAdapter, error) {
a := &BotAdapter{
slack: client,
rtm: rtm, // may be nil
events: events,
context: ctx,
logger: conf.Logger,
name: conf.Name,
sendMsgParams: conf.SendMsgParams,
users: map[string]joe.User{}, // TODO: cache expiration?
listenPassive: conf.ListenPassive,
}
if a.logger == nil {
a.logger = zap.NewNop()
}
resp, err := client.AuthTestContext(ctx)
if err != nil {
return nil, fmt.Errorf("slack auth test failed: %w", err)
}
a.userID = resp.UserID
a.logger.Info("Connected to slack API",
zap.String("url", resp.URL),
zap.String("user", resp.User),
zap.String("user_id", resp.UserID),
zap.String("team", resp.Team),
zap.String("team_id", resp.TeamID),
)
return a, nil
}
// RegisterAt implements the joe.Adapter interface by emitting the slack API
// events to the given brain.
func (a *BotAdapter) RegisterAt(brain *joe.Brain) {
go a.handleSlackEvents(brain)
}
func (a *BotAdapter) handleSlackEvents(brain *joe.Brain) {
for msg := range a.events {
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
a.handleMessageEvent(ev, brain)
case *slack.ReactionAddedEvent:
a.handleReactionAddedEvent(ev, brain)
case *slack.RTMError:
a.logger.Error("Slack Real Time Messaging (RTM) error",
zap.Int("code", ev.Code),
zap.String("msg", ev.Msg),
)
case *slack.UnmarshallingErrorEvent:
a.logger.Error("Slack unmarshalling error", zap.Error(ev.ErrorObj))
case *slack.InvalidAuthEvent:
a.logger.Error("Invalid authentication error", zap.Any("event", ev))
return
case *slack.UserTypingEvent:
brain.Emit(joe.UserTypingEvent{
User: a.userByID(ev.User),
Channel: ev.Channel,
})
case *slack.DisconnectedEvent:
if ev.Intentional {
a.logger.Debug("Disconnected slack adapter")
return
}
default:
if a.logUnknownMessageTypes {
a.logger.Error("Received unknown type from Real Time Messaging (RTM) system",
zap.String("type", msg.Type),
zap.Any("data", msg.Data),
zap.String("go_type", fmt.Sprintf("%T", msg.Data)),
)
}
}
}
}
func (a *BotAdapter) handleMessageEvent(ev *slack.MessageEvent, brain joe.EventEmitter) {
// check if the message comes from ourselves
if ev.User == a.userID {
// msg is from us, ignore it!
return
}
// check if we have a DM, or standard channel post
selfLink := a.userLink(a.userID)
direct := strings.HasPrefix(ev.Msg.Channel, "D")
if !direct && !strings.Contains(ev.Msg.Text, selfLink) && !a.listenPassive {
// msg not for us!
return
}
text := strings.TrimSpace(strings.TrimPrefix(ev.Msg.Text, selfLink))
brain.Emit(joe.ReceiveMessageEvent{
Text: text,
Channel: ev.Channel,
ID: ev.Timestamp, // slack uses the message timestamps as identifiers within the channel
AuthorID: ev.User,
Data: ev,
})
}
// See https://api.slack.com/events/reaction_added
func (a *BotAdapter) handleReactionAddedEvent(ev *slack.ReactionAddedEvent, brain joe.EventEmitter) {
if ev.User == a.userID {
// reaction is from us, ignore it!
return
}
if ev.Item.Type != "message" {
// reactions for other things except messages is not supported by Joe
return
}
brain.Emit(reactions.Event{
Channel: ev.Item.Channel,
MessageID: ev.Item.Timestamp,
AuthorID: ev.User,
Reaction: reactions.Reaction{Shortcode: ev.Reaction},
})
}
func (a *BotAdapter) userByID(userID string) joe.User {
a.usersMu.RLock()
user, ok := a.users[userID]
a.usersMu.RUnlock()
if ok {
return user
}
resp, err := a.slack.GetUserInfo(userID)
if err != nil {
a.logger.Error("Failed to get user info by ID",
zap.String("user_id", userID),
)
return joe.User{ID: userID}
}
user = joe.User{
ID: resp.ID,
Name: resp.Name,
RealName: resp.RealName,
}
a.usersMu.Lock()
a.users[userID] = user
a.usersMu.Unlock()
return user
}
// Send implements joe.Adapter by sending all received text messages to the
// given slack channel ID.
func (a *BotAdapter) Send(text, channelID string) error {
a.logger.Info("Sending message to channel",
zap.String("channel_id", channelID),
// do not leak actual message content since it might be sensitive
)
_, _, err := a.slack.PostMessageContext(a.context, channelID,
slack.MsgOptionText(text, false),
slack.MsgOptionPostMessageParameters(a.sendMsgParams),
slack.MsgOptionUser(a.userID),
slack.MsgOptionUsername(a.name),
)
return err
}
// React implements joe.ReactionAwareAdapter by letting the bot attach the given
// reaction to the message.
func (a *BotAdapter) React(reaction reactions.Reaction, msg joe.Message) error {
ref := slack.NewRefToMessage(msg.Channel, msg.ID)
return a.slack.AddReactionContext(a.context, reaction.Shortcode, ref)
}
// Close disconnects the adapter from the slack API.
func (a *BotAdapter) Close() error {
if a.rtm != nil {
return a.rtm.Disconnect()
}
return nil
}
// As long as github.com/slack-go/slack does not support the "link_names=1"
// argument we have to format the user link ourselves.
// See https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
func (a *BotAdapter) userLink(userID string) string {
return fmt.Sprintf("<@%s>", userID)
}