From 00bf779b5cda6ef0dd81b1dd4951353beb967481 Mon Sep 17 00:00:00 2001 From: Vlad Melnik Date: Mon, 16 Sep 2024 17:05:15 +0300 Subject: [PATCH] feat: exposing request from event in interaction handler --- executors.go | 6 ++++-- handler.go | 4 +++- slacker.go | 9 +++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/executors.go b/executors.go index e9899ef..6c7d169 100644 --- a/executors.go +++ b/executors.go @@ -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 @@ -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 } @@ -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() { diff --git a/handler.go b/handler.go index 0b56486..b275ef4 100644 --- a/handler.go +++ b/handler.go @@ -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 @@ -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 diff --git a/slacker.go b/slacker.go index 5284e77..dd70935 100644 --- a/slacker.go +++ b/slacker.go @@ -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 { @@ -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...) @@ -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...) } }