-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_handler.go
239 lines (214 loc) · 6.47 KB
/
slack_handler.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
package main
import (
"fmt"
"log"
"strings"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"github.com/tidwall/gjson"
)
type CSPSlackEvtHandler struct {
*CSPSlack
evt socketmode.Event
}
func (h *CSPSlackEvtHandler) handleEventAPIEvent() {
eventsAPIEvent, ok := h.evt.Data.(slackevents.EventsAPIEvent)
if !ok {
fmt.Printf("Ignored %+v\n", h.evt)
return
}
fmt.Printf("Event received: %+v\n", eventsAPIEvent)
h.slackSocket.Ack(*h.evt.Request)
switch eventsAPIEvent.Type {
case slackevents.CallbackEvent:
innerEvent := eventsAPIEvent.InnerEvent
switch ev := innerEvent.Data.(type) {
case *slackevents.PinAddedEvent:
h.shouldUpdate = true
case *slackevents.PinRemovedEvent:
h.shouldUpdate = true
case *slackevents.ReactionRemovedEvent:
if ev.User == config.SlackBotID {
return
}
reaction := ev.Reaction
h.slackSocket.RemoveReaction(reaction, slack.ItemRef{
Channel: config.SlackStatusChannelID,
Timestamp: ev.Item.Timestamp,
})
h.shouldUpdate = true
case *slackevents.ReactionAddedEvent:
h.handleReactionAddedEvent(ev)
case *slackevents.MessageEvent:
h.handleMessageEvent(ev)
default:
log.Println("no handler for event of given type")
}
default:
}
}
func (h *CSPSlackEvtHandler) handleReactionAddedEvent(ev *slackevents.ReactionAddedEvent) {
reaction := ev.Reaction
botMentioned, err := h.isBotMentioned(ev.Item.Timestamp)
if err != nil {
log.Println(err)
return
}
if ev.User == config.SlackBotID || !isRelevantReaction(reaction) || (!botMentioned) {
return
}
// If necessary, remove a conflicting reaction
if isRelevantReaction(reaction) {
h.clearReactions(
ev.Item.Timestamp,
[]string{
config.StatusOKEmoji,
config.StatusWarnEmoji,
config.StatusErrorEmoji,
},
)
}
// Mirror the reaction on the message
h.slackSocket.AddReaction(reaction, slack.NewRefToMessage(
config.SlackStatusChannelID,
ev.Item.Timestamp,
))
h.shouldUpdate = true
}
func (h *CSPSlackEvtHandler) handleMessageEvent(ev *slackevents.MessageEvent) {
// If a message mentioning us gets added or deleted, then
// do something
log.Printf("Message type: %s\n", ev.SubType)
// If the message was deleted, then update the page.
// If LITERALLY ANYTHING ELSE happened, bail
switch ev.SubType {
case "": // continue
case "message_changed":
fallthrough
case "message_deleted":
h.shouldUpdate = true
fallthrough
default:
return
}
// If the bot was mentioned in this message, then we should probably
// re-build the page, and if not, we should bail.
botID := fmt.Sprintf("<@%s>", config.SlackBotID)
if strings.Contains(ev.Text, botID) {
h.shouldUpdate = true
} else {
return
}
// HACK: If we're still here, it means we got mentioned, and should
// do something about it. We do this instead of an AppMention because
// there does not seem to be any way to not fire an AppMentionEvent
// if a message is edited
log.Printf("Got mentioned. Timestamp is: %s. ThreadTimestamp is: %s\n", ev.TimeStamp, ev.ThreadTimeStamp)
channelName, err := h.resolveChannelName(config.SlackForwardChannelID)
if err != nil {
log.Printf("Could not resolve channel name: %s\n", err)
return
}
blocks := CreateUpdateResponseMsg(channelName, ev.User)
_, _, err = h.slackSocket.PostMessage(config.SlackStatusChannelID, slack.MsgOptionTS(ev.TimeStamp), slack.MsgOptionBlocks(blocks...))
if err != nil {
log.Printf("Error posting ephemeral message: %s", err)
}
}
func (h *CSPSlackEvtHandler) handleInteractiveEvent() {
callback, ok := h.evt.Data.(slack.InteractionCallback)
if !ok {
fmt.Printf("Ignored %+v\n", h.evt)
return
}
var payload interface{}
switch callback.Type {
case slack.InteractionTypeBlockActions:
// See https://api.slack.com/apis/connections/socket-implement#button
// Check which button was pressed
for _, action := range callback.ActionCallback.BlockActions {
switch action.ActionID {
case CSPSetOK, CSPSetWarn, CSPSetError, CSPCancel:
h.handlePromptInteraction(callback, action)
}
}
case slack.InteractionTypeShortcut:
log.Printf("Got shortcut: %s", callback.CallbackID)
if callback.CallbackID == CSPUpdateStatusPage {
h.shouldUpdate = true
}
default:
log.Println("no handler for event of given type")
}
h.slackSocket.Ack(*h.evt.Request, payload)
}
func (h *CSPSlackEvtHandler) handlePromptInteraction(callback slack.InteractionCallback, action *slack.BlockAction) {
log.Printf("Block Action Detected: %s\n", action.ActionID)
itemRef := slack.ItemRef{
Channel: callback.Channel.ID,
Timestamp: callback.Container.ThreadTs,
}
selected_options := gjson.Get(string(callback.RawState), "values.options.options.selected_options").Array()
for i, opt := range selected_options {
fmt.Println(i, opt)
option := gjson.Get(opt.String(), "value").String()
switch option {
case CSPPin:
log.Println("Will pin message")
// Get the conversation history
err := h.slackSocket.AddPin(callback.Channel.ID, itemRef)
if err != nil {
log.Println(err)
}
case CSPForward:
log.Println("Will forward message")
messageText, err := h.getSingleMessage(callback.Channel.ID, callback.Container.ThreadTs)
if err != nil {
log.Println(err)
break
}
botID := fmt.Sprintf("<@%s>", config.SlackBotID)
strippedStatusUpdate := strings.Replace(messageText.Text, botID, "", -1)
_, _, err = h.slackSocket.PostMessage(config.SlackForwardChannelID, slack.MsgOptionText(strippedStatusUpdate, false))
}
}
// Clear any old reactions
switch action.ActionID {
case CSPSetOK, CSPSetWarn, CSPSetError:
h.clearReactions(
callback.Container.ThreadTs,
[]string{
config.StatusOKEmoji,
config.StatusWarnEmoji,
config.StatusErrorEmoji,
},
)
}
// Add the reaction we want
switch action.ActionID {
case CSPSetOK:
err := h.slackSocket.AddReaction(config.StatusOKEmoji, itemRef)
if err != nil {
// Handle the error
h.slackSocket.Debugf("Error adding reaction: %v", err)
}
case CSPSetWarn:
err := h.slackSocket.AddReaction(config.StatusWarnEmoji, itemRef)
if err != nil {
// Handle the error
h.slackSocket.Debugf("Error adding reaction: %v", err)
}
case CSPSetError:
err := h.slackSocket.AddReaction(config.StatusErrorEmoji, itemRef)
if err != nil {
// Handle the error
h.slackSocket.Debugf("Error adding reaction: %v", err)
}
case CSPCancel:
}
_, _, err := h.slackSocket.DeleteMessage(config.SlackStatusChannelID, callback.Container.MessageTs)
if err != nil {
log.Println(err)
}
}