forked from src-d/flamingo
/
bot_client.go
206 lines (173 loc) · 4.63 KB
/
bot_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
package slack
import (
"sync"
"time"
"gopkg.in/inconshreveable/log15.v2"
"github.com/mvader/slack"
"github.com/src-d/flamingo"
)
type slackRTM interface {
IncomingEvents() chan slack.RTMEvent
slackAPI
}
type handlerDelegate interface {
ControllerFor(flamingo.Message) (flamingo.Controller, bool)
ActionHandler(string) (flamingo.ActionHandler, bool)
HandleIntro(flamingo.Bot, flamingo.Channel)
Storage() flamingo.Storage
}
type botClient struct {
id string
sync.RWMutex
rtm slackRTM
shutdown chan struct{}
closed chan struct{}
conversations map[string]*botConversation
delegate handlerDelegate
}
func newBotClient(id string, rtm slackRTM, delegate handlerDelegate) *botClient {
cli := &botClient{
id: id,
rtm: rtm,
shutdown: make(chan struct{}, 1),
closed: make(chan struct{}, 1),
conversations: make(map[string]*botConversation),
delegate: delegate,
}
go cli.runRTM()
return cli
}
func (c *botClient) runRTM() {
log15.Info("starting real time", "bot", c.id)
for {
select {
case <-c.shutdown:
for id, convo := range c.conversations {
log15.Debug("shutting down conversation", "channel", id)
convo.stop()
log15.Debug("shut down conversation", "channel", id)
}
c.closed <- struct{}{}
return
case e := <-c.rtm.IncomingEvents():
go c.handleRTMEvent(e)
case <-time.After(50 * time.Millisecond):
}
}
}
func (c *botClient) handleAction(channel string, action slack.AttachmentActionCallback) {
c.Lock()
defer c.Unlock()
conv, ok := c.conversations[channel]
if !ok {
log15.Warn("conversation not found in bot", "channel", channel, "id", c.id)
return
}
conv.actions <- action
}
func (c *botClient) handleJob(job flamingo.Job) {
c.Lock()
var wg sync.WaitGroup
for _, conv := range c.conversations {
wg.Add(1)
go func(conv *botConversation) {
conv.handleJob(job)
wg.Done()
}(conv)
}
c.Unlock()
wg.Wait()
}
func (c *botClient) handleRTMEvent(e slack.RTMEvent) {
log15.Debug("received event of type", "type", e.Type)
switch evt := e.Data.(type) {
case *slack.MessageEvent:
// For now, ignore all messages that are not new messages
switch evt.SubType {
case "", "me_message", "bot_message":
c.handleMessageEvent(evt)
}
case *slack.LatencyReport:
log15.Info("Current latency", "latency", evt.Value)
case *slack.RTMError:
log15.Error("Real Time Error", "error", evt.Error())
case *slack.IMCreatedEvent:
c.handleIMCreatedEvent(evt)
case *slack.GroupJoinedEvent:
c.handleGroupJoinedEvent(evt)
case *slack.InvalidAuthEvent:
log15.Crit("Invalid credentials for bot", "bot", c.id)
}
}
func (c *botClient) handleMessageEvent(evt *slack.MessageEvent) {
if evt.BotID == c.id {
log15.Debug("got message from self, ignoring")
return
}
c.RLock()
conv, ok := c.conversations[evt.Channel]
c.RUnlock()
if !ok {
var err error
conv, err = c.newConversation(evt.Channel)
if err != nil {
log15.Error("unable to create conversation for bot", "channel", evt.Channel, "bot", c.id, "error", err.Error())
return
}
}
log15.Debug("message for channel", "channel", evt.Channel, "text", evt.Text)
conv.messages <- evt
}
func (c *botClient) handleIMCreatedEvent(evt *slack.IMCreatedEvent) {
conv, err := c.newConversation(evt.Channel.ID)
if err != nil {
log15.Error("unable to create IM conversation for bot", "channel", evt.Channel.ID, "bot", c.id, "error", err.Error())
return
}
conv.handleIntro()
}
func (c *botClient) handleGroupJoinedEvent(evt *slack.GroupJoinedEvent) {
conv, err := c.newConversation(evt.Channel.ID)
if err != nil {
log15.Error("unable to create group conversation for bot", "channel", evt.Channel.ID, "bot", c.id, "error", err.Error())
return
}
conv.handleIntro()
}
func (c *botClient) newConversation(channel string) (*botConversation, error) {
c.Lock()
defer c.Unlock()
log15.Debug("conversation does not exist for bot, creating", "channel", channel, "bot", c.id)
conv, err := newBotConversation(c.id, channel, c.rtm, c.delegate)
if err != nil {
return nil, err
}
storage := c.delegate.Storage()
conversation := flamingo.StoredConversation{
ID: channel,
BotID: c.id,
CreatedAt: time.Now(),
}
ok, err := storage.ConversationExists(conversation)
if err != nil {
return nil, err
}
if !ok {
if err := storage.StoreConversation(conversation); err != nil {
return nil, err
}
}
c.conversations[channel] = conv
go conv.run()
return conv, nil
}
func (c *botClient) addConversation(id string) error {
_, err := c.newConversation(id)
return err
}
func (c *botClient) stop() {
c.shutdown <- struct{}{}
close(c.shutdown)
<-c.closed
close(c.closed)
}