Documentation
¶
Overview ¶
Package slacktonic provides middleware for handling Slack slash commands in Gin applications.
Package slacktonic provides middleware for handling Slack slash commands in Gin applications.
Package slacktonic provides middleware for handling Slack slash commands in Gin applications.
It verifies the authenticity of incoming Slack requests using the signing secret and parses slash commands, making them available in the Gin context.
Example usage:
r := gin.New()
r.Use(slacktonic.Middleware("your-slack-signing-secret"))
r.POST("/slack", func(c *gin.Context) {
cmd, ok := slacktonic.GetSlashCommand(c)
if ok {
// Handle the slash command
}
})
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func GetSlashCommand ¶
func GetSlashCommand(c *gin.Context) (slack.SlashCommand, bool)
GetSlashCommand retrieves the Slack slash command from the Gin context.
This function should be called after the Middleware has processed the request. The Middleware stores the slash command in the context if it was successfully parsed from the request.
Parameters:
- c: The Gin context from which to retrieve the slash command
Returns:
- slack.SlashCommand: The slash command if it exists in the context
- bool: true if the slash command was found, false otherwise
func Middleware ¶
func Middleware(secret string, optFns ...Option) gin.HandlerFunc
Middleware returns a Gin middleware function that verifies Slack requests and parses slash commands.
The middleware: 1. Verifies the request using the provided Verifier (or creates a SlackVerifier with the provided secret) 2. Parses the slash command from the request 3. Validates the command against the expected command (if specified using WithCommand) 4. Stores the slash command in the Gin context for later retrieval
Parameters:
- secret: The Slack signing secret used to verify request authenticity
- optFns: Optional configuration functions for the middleware (WithCommand, WithLogger, WithVerifier)
Returns a Gin HandlerFunc that can be used with the Gin router.
Types ¶
type Logger ¶
type Logger interface {
// InfoContext logs an info message with the given context and key-value pairs.
InfoContext(ctx context.Context, msg string, args ...any)
// WarnContext logs a warning message with the given context and key-value pairs.
WarnContext(ctx context.Context, msg string, args ...any)
// ErrorContext logs an error message with the given context and key-value pairs.
ErrorContext(ctx context.Context, msg string, args ...any)
}
Logger is an interface for logging errors that occur during request processing. It is designed to be compatible with the standard library's log/slog package.
type Option ¶
type Option func(*Options)
func WithCommand ¶
WithCommand specifies the expected slash command. If provided, the middleware will check if the received command matches this value. If the command doesn't match, a 400 Bad Request response will be returned.
func WithLogger ¶
WithLogger sets a logger for the middleware. Logger is used to log errors that occur during request processing. If not provided, a no-op logger will be used.
func WithVerifier ¶
WithVerifier sets a custom verifier for the middleware. Verifier is used to verify Slack requests and parse slash commands. If not provided, a SlackVerifier will be created using the provided secret.
type Options ¶
Options configures the behavior of the middleware. It is configured through option functions passed to the Middleware function.
type SlackVerifier ¶
type SlackVerifier struct {
// contains filtered or unexported fields
}
SlackVerifier is the default implementation of the Verifier interface. It verifies Slack request signatures using the provided signing secret.
func NewVerifier ¶
func NewVerifier(secret string) *SlackVerifier
NewVerifier creates a new SlackVerifier with the provided signing secret.
func (*SlackVerifier) Verify ¶
func (s *SlackVerifier) Verify(req *http.Request) (slack.SlashCommand, error)
Verify verifies the authenticity of a Slack request using the signing secret and parses the slash command from the request. It returns the parsed slash command and nil if verification succeeds, or nil and an error if verification fails.
type Verifier ¶
type Verifier interface {
// Verify verifies the authenticity of a Slack request and parses the slash command.
// It returns the parsed slash command and nil if verification succeeds,
// or nil and an error if verification fails.
Verify(*http.Request) (slack.SlashCommand, error)
}
Verifier is an interface for verifying Slack requests and parsing slash commands. It allows for custom verification logic or mocking during tests.