-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_actions.go
102 lines (90 loc) · 2.7 KB
/
block_actions.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
package slap
import (
"encoding/json"
"net/http"
"github.com/slack-go/slack"
)
// The payload of a Slack block action request
type BlockActionPayload struct {
interactionPayload
Container slack.Container `json:"container"`
Actions []slack.BlockAction `json:"actions"`
Hash string `json:"hash"`
BotAccessToken string `json:"bot_access_token"`
Enterprise *string `json:"enterprise,omitempty"`
Channel *struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"channel,omitempty"`
Message *slack.MessageEvent `json:"message,omitempty"`
View *slack.View `json:"view,omitempty"`
State *slack.BlockActionStates `json:"state,omitempty"`
}
// A block action request
type BlockActionRequest struct {
baseRequest
Payload BlockActionPayload
}
// A function to handle a block action request
type BlockActionHandler func(req *BlockActionRequest) error
func (app *Application) handleBlockActions(w http.ResponseWriter, blob []byte) {
var payload BlockActionPayload
err := json.Unmarshal(blob, &payload)
if err != nil {
app.logger.Error("Could not parse BlockActionPayload", "error", err.Error())
http.Error(w, "Invalid payload", http.StatusBadRequest)
return
}
if len(payload.Actions) == 0 {
http.Error(w, "Invalid payload", http.StatusBadRequest)
return
}
botToken, err := app.botToken(payload.Team.ID)
if err != nil {
app.logger.Error("Could not get bot token", "teamID", payload.Team.ID, "error", err.Error())
http.Error(w, "Could not get bot token", http.StatusInternalServerError)
return
}
actionID := payload.Actions[0].ActionID
handler, ok := app.blockActions[actionID]
if !ok {
// Return 200 for unknown action IDs
w.WriteHeader(http.StatusOK)
return
}
ackChan := make(chan []byte)
errChan := make(chan error)
go func() {
req := &BlockActionRequest{
Payload: payload,
baseRequest: baseRequest{
errChannel: errChan,
ackChannel: ackChan,
ackCalled: false,
writer: w,
Client: slack.New(botToken),
Logger: app.logger,
},
}
err := handler(req)
if err == nil {
return
}
app.logger.Error("A block actions handler failed", "actionID", actionID, "error", err.Error())
_, msgerr := req.Client.PostEphemeral(req.Payload.Channel.ID, req.Payload.User.ID, slack.MsgOptionText(app.errorMessage, false))
if msgerr != nil {
app.logger.Error("Unable to send error message to user", "user", req.Payload.User.ID, "error", msgerr.Error())
}
errChan <- err
}()
select {
case <-ackChan:
w.Write(nil)
case err := <-errChan:
if err == nil {
return
}
http.Error(w, "An error occurred", http.StatusInternalServerError)
return
}
}