Debian Codegraph @

socketmode/socketmode_handler.go

raw symbols SCIP
1 package socketmode
2
3 import (
4 "github.com/slack-go/slack"
5 "github.com/slack-go/slack/slackevents"
6 )
7
8 type SocketmodeHandler struct {
9 Client *Client
10
11 //lvl 1 - the most generic type of event
12 EventMap map[EventType][]SocketmodeHandlerFunc
13 //lvl 2 - Manage event by inner type
14 InteractionEventMap map[slack.InteractionType][]SocketmodeHandlerFunc
15 EventApiMap map[slackevents.EventsAPIType][]SocketmodeHandlerFunc
16 //lvl 3 - the most userfriendly way of managing event
17 InteractionBlockActionEventMap map[string]SocketmodeHandlerFunc
18 SlashCommandMap map[string]SocketmodeHandlerFunc
19
20 Default SocketmodeHandlerFunc
21 }
22
23 // Handler have access to the event and socketmode client
24 type SocketmodeHandlerFunc func(*Event, *Client)
25
26 // Middleware accept SocketmodeHandlerFunc, and return SocketmodeHandlerFunc
27 type SocketmodeMiddlewareFunc func(SocketmodeHandlerFunc) SocketmodeHandlerFunc
28
29 // Initialization constructor for SocketmodeHandler
30 func NewSocketmodeHandler(client *Client) *SocketmodeHandler {
31 eventMap := make(map[EventType][]SocketmodeHandlerFunc)
32 interactionEventMap := make(map[slack.InteractionType][]SocketmodeHandlerFunc)
33 eventApiMap := make(map[slackevents.EventsAPIType][]SocketmodeHandlerFunc)
34
35 interactionBlockActionEventMap := make(map[string]SocketmodeHandlerFunc)
36 slackCommandMap := make(map[string]SocketmodeHandlerFunc)
37
38 return &SocketmodeHandler{
39 Client: client,
40 EventMap: eventMap,
41 EventApiMap: eventApiMap,
42 InteractionEventMap: interactionEventMap,
43 InteractionBlockActionEventMap: interactionBlockActionEventMap,
44 SlashCommandMap: slackCommandMap,
45 Default: func(e *Event, c *Client) {
46 c.log.Printf("Unexpected event type received: %v\n", e.Type)
47 },
48 }
49 }
50
51 // Register a middleware or handler for an Event from socketmode
52 // This most general entrypoint
53 func (r *SocketmodeHandler) Handle(et EventType, f SocketmodeHandlerFunc) {
54 r.EventMap[et] = append(r.EventMap[et], f)
55 }
56
57 // Register a middleware or handler for an Interaction
58 // There is several types of interactions, decated functions lets you better handle them
59 // See
60 // * HandleInteractionBlockAction
61 // * (Not Implemented) HandleShortcut
62 // * (Not Implemented) HandleView
63 func (r *SocketmodeHandler) HandleInteraction(et slack.InteractionType, f SocketmodeHandlerFunc) {
64 r.InteractionEventMap[et] = append(r.InteractionEventMap[et], f)
65 }
66
67 // Register a middleware or handler for a Block Action referenced by its ActionID
68 func (r *SocketmodeHandler) HandleInteractionBlockAction(actionID string, f SocketmodeHandlerFunc) {
69 if actionID == "" {
70 panic("invalid command cannot be empty")
71 }
72 if f == nil {
73 panic("invalid handler cannot be nil")
74 }
75 if _, exist := r.InteractionBlockActionEventMap[actionID]; exist {
76 panic("multiple registrations for actionID" + actionID)
77 }
78 r.InteractionBlockActionEventMap[actionID] = f
79 }
80
81 // Register a middleware or handler for an Event (from slackevents)
82 func (r *SocketmodeHandler) HandleEvents(et slackevents.EventsAPIType, f SocketmodeHandlerFunc) {
83 r.EventApiMap[et] = append(r.EventApiMap[et], f)
84 }
85
86 // Register a middleware or handler for a Slash Command
87 func (r *SocketmodeHandler) HandleSlashCommand(command string, f SocketmodeHandlerFunc) {
88 if command == "" {
89 panic("invalid command cannot be empty")
90 }
91 if f == nil {
92 panic("invalid handler cannot be nil")
93 }
94 if _, exist := r.SlashCommandMap[command]; exist {
95 panic("multiple registrations for command" + command)
96 }
97 r.SlashCommandMap[command] = f
98 }
99
100 // Register a middleware or handler to use as a last resort
101 func (r *SocketmodeHandler) HandleDefault(f SocketmodeHandlerFunc) {
102 r.Default = f
103 }
104
105 // RunSlackEventLoop receives the event via the socket
106 func (r *SocketmodeHandler) RunEventLoop() error {
107
108 go r.runEventLoop()
109
110 return r.Client.Run()
111 }
112
113 // Call the dispatcher for each incomming event
114 func (r *SocketmodeHandler) runEventLoop() {
115 for evt := range r.Client.Events {
116 r.dispatcher(evt)
117 }
118 }
119
120 // Dispatch events to the specialized dispatcher
121 func (r *SocketmodeHandler) dispatcher(evt Event) {
122 var ishandled bool
123
124 // Some eventType can be further decomposed
125 switch evt.Type {
126 case EventTypeInteractive:
127 ishandled = r.interactionDispatcher(&evt)
128 case EventTypeEventsAPI:
129 ishandled = r.eventAPIDispatcher(&evt)
130 case EventTypeSlashCommand:
131 ishandled = r.slashCommandDispatcher(&evt)
132 default:
133 ishandled = r.socketmodeDispatcher(&evt)
134 }
135
136 if !ishandled {
137 go r.Default(&evt, r.Client)
138 }
139 }
140
141 // Dispatch socketmode events to the registered middleware
142 func (r *SocketmodeHandler) socketmodeDispatcher(evt *Event) bool {
143 if handlers, ok := r.EventMap[evt.Type]; ok {
144 // If we registered an event
145 for _, f := range handlers {
146 go f(evt, r.Client)
147 }
148
149 return true
150 }
151
152 return false
153 }
154
155 // Dispatch interactions to the registered middleware
156 func (r *SocketmodeHandler) interactionDispatcher(evt *Event) bool {
157 var ishandled bool = false
158
159 interaction, ok := evt.Data.(slack.InteractionCallback)
160 if !ok {
161 r.Client.log.Printf("Ignored %+v\n", evt)
162 return false
163 }
164
165 // Level 1 - socketmode EventType
166 ishandled = r.socketmodeDispatcher(evt)
167
168 // Level 2 - interaction EventType
169 if handlers, ok := r.InteractionEventMap[interaction.Type]; ok {
170 // If we registered an event
171 for _, f := range handlers {
172 go f(evt, r.Client)
173 }
174
175 ishandled = true
176 }
177
178 // Level 3 - interaction with actionID
179 blockActions := interaction.ActionCallback.BlockActions
180 // outmoded approach won`t be implemented
181 // attachments_actions := interaction.ActionCallback.AttachmentActions
182
183 for _, action := range blockActions {
184 if handler, ok := r.InteractionBlockActionEventMap[action.ActionID]; ok {
185
186 go handler(evt, r.Client)
187
188 ishandled = true
189 }
190 }
191 return ishandled
192 }
193
194 // Dispatch eventAPI events to the registered middleware
195 func (r *SocketmodeHandler) eventAPIDispatcher(evt *Event) bool {
196 var ishandled bool = false
197 eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
198 if !ok {
199 r.Client.log.Printf("Ignored %+v\n", evt)
200 return false
201 }
202
203 innerEventType := slackevents.EventsAPIType(eventsAPIEvent.InnerEvent.Type)
204
205 // Level 1 - socketmode EventType
206 ishandled = r.socketmodeDispatcher(evt)
207
208 // Level 2 - EventAPI EventType
209 if handlers, ok := r.EventApiMap[innerEventType]; ok {
210 // If we registered an event
211 for _, f := range handlers {
212 go f(evt, r.Client)
213 }
214
215 ishandled = true
216 }
217
218 return ishandled
219 }
220
221 // Dispatch SlashCommands events to the registered middleware
222 func (r *SocketmodeHandler) slashCommandDispatcher(evt *Event) bool {
223 var ishandled bool = false
224 slashCommandEvent, ok := evt.Data.(slack.SlashCommand)
225 if !ok {
226 r.Client.log.Printf("Ignored %+v\n", evt)
227 return false
228 }
229
230 // Level 1 - socketmode EventType
231 ishandled = r.socketmodeDispatcher(evt)
232
233 // Level 2 - SlackCommand by name
234 if handler, ok := r.SlashCommandMap[slashCommandEvent.Command]; ok {
235
236 go handler(evt, r.Client)
237
238 ishandled = true
239 }
240
241 return ishandled
242
243 }