Skip to content

Commit

Permalink
feat: exposing request from event in interaction handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Melnik committed Sep 16, 2024
1 parent bd3ee21 commit 00bf779
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
6 changes: 4 additions & 2 deletions executors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package slacker

import "github.com/slack-go/slack/socketmode"

func executeCommand(ctx *CommandContext, handler CommandHandler, middlewares ...CommandMiddlewareHandler) {
if handler == nil {
return
Expand All @@ -12,7 +14,7 @@ func executeCommand(ctx *CommandContext, handler CommandHandler, middlewares ...
handler(ctx)
}

func executeInteraction(ctx *InteractionContext, handler InteractionHandler, middlewares ...InteractionMiddlewareHandler) {
func executeInteraction(ctx *InteractionContext, handler InteractionHandler, request *socketmode.Request, middlewares ...InteractionMiddlewareHandler) {
if handler == nil {
return
}
Expand All @@ -21,7 +23,7 @@ func executeInteraction(ctx *InteractionContext, handler InteractionHandler, mid
handler = middlewares[i](handler)
}

handler(ctx)
handler(ctx, request)
}

func executeJob(ctx *JobContext, handler JobHandler, middlewares ...JobMiddlewareHandler) func() {
Expand Down
4 changes: 3 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package slacker

import "github.com/slack-go/slack/socketmode"

// CommandMiddlewareHandler represents the command middleware handler function
type CommandMiddlewareHandler func(CommandHandler) CommandHandler

Expand All @@ -10,7 +12,7 @@ type CommandHandler func(*CommandContext)
type InteractionMiddlewareHandler func(InteractionHandler) InteractionHandler

// InteractionHandler represents the interaction handler function
type InteractionHandler func(*InteractionContext)
type InteractionHandler func(*InteractionContext, *socketmode.Request)

// JobMiddlewareHandler represents the job middleware handler function
type JobMiddlewareHandler func(JobHandler) JobHandler
Expand Down
9 changes: 5 additions & 4 deletions slacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (s *Slacker) Listen(ctx context.Context) error {
// Acknowledge receiving the request
s.socketModeClient.Ack(*socketEvent.Request)

go s.handleInteractionEvent(ctx, &callback)
go s.handleInteractionEvent(ctx, &callback, *socketEvent.Request)

default:
if s.unsupportedEventHandler != nil {
Expand Down Expand Up @@ -440,7 +440,7 @@ func (s *Slacker) startCronJobs(ctx context.Context) {
s.cronClient.Start()
}

func (s *Slacker) handleInteractionEvent(ctx context.Context, callback *slack.InteractionCallback) {
func (s *Slacker) handleInteractionEvent(ctx context.Context, callback *slack.InteractionCallback, request socketmode.Request) {
middlewares := make([]InteractionMiddlewareHandler, 0)
middlewares = append(middlewares, s.interactionMiddlewares...)

Expand Down Expand Up @@ -482,14 +482,15 @@ func (s *Slacker) handleInteractionEvent(ctx context.Context, callback *slack.In
if interaction != nil {
interactionCtx := newInteractionContext(ctx, s.logger, s.slackClient, callback, definition)
middlewares = append(middlewares, definition.Middlewares...)
executeInteraction(interactionCtx, definition.Handler, middlewares...)
executeInteraction(interactionCtx, definition.Handler, &request, middlewares...)
return
}

s.logger.Debug("unsupported interaction type", "type", callback.Type)

if s.unsupportedInteractionHandler != nil {
interactionCtx := newInteractionContext(ctx, s.logger, s.slackClient, callback, nil)
executeInteraction(interactionCtx, s.unsupportedInteractionHandler, middlewares...)
executeInteraction(interactionCtx, s.unsupportedInteractionHandler, &request, middlewares...)
}
}

Expand Down

0 comments on commit 00bf779

Please sign in to comment.