generated from nakatanakatana/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_workflow_step.go
84 lines (65 loc) · 2.03 KB
/
handler_workflow_step.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
package slackworkflowbot
import (
"encoding/json"
"io"
"log"
"net/http"
"github.com/slack-go/slack/slackevents"
)
//nolint:funlen
func CreateEventsHandler(
workflowStep WorkflowStepFunctions,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// see: https://github.com/slack-go/slack/blob/master/examples/eventsapi/events.go
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
eventsAPIEvent, err := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionNoVerifyToken())
if err != nil {
log.Printf("[ERROR] Failed on parsing event: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
switch eventsAPIEvent.Type {
case slackevents.URLVerification:
// see: https://api.slack.com/apis/connections/events-api#subscriptions
var r *slackevents.ChallengeResponse
err := json.Unmarshal(body, &r)
if err != nil {
log.Printf("[ERROR] Failed to decode json message on event url_verification: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text")
_, _ = w.Write([]byte(r.Challenge))
return
case slackevents.CallbackEvent:
// see: https://api.slack.com/apis/connections/events-api#receiving_events
innerEvent := eventsAPIEvent.InnerEvent
switch ev := innerEvent.Data.(type) {
// see: https://api.slack.com/events/workflow_step_execute
case *slackevents.WorkflowStepExecuteEvent:
callbackFunc, ok := workflowStep[CallbackID(ev.CallbackID)]
if !ok {
log.Printf("[WARN] unknown callbackID: %s", ev.CallbackID)
w.WriteHeader(http.StatusBadRequest)
return
}
go callbackFunc(ev.WorkflowStep)
w.WriteHeader(http.StatusOK)
return
default:
w.WriteHeader(http.StatusBadRequest)
log.Printf("[WARN] unknown inner event type: %s", eventsAPIEvent.InnerEvent.Type)
return
}
}
}
}