Documentation
¶
Overview ¶
Package slack provides methods to send notifications to Slack channels via webhooks.
Index ¶
- Variables
- func Health() error
- func Init(configs ...Config) error
- func Reset()
- type Attachment
- type BatchResult
- type Block
- type BlockElement
- type Builder
- type CircuitBreaker
- type CircuitBreakerError
- type CircuitState
- type Config
- type Field
- type LogLevel
- type Logger
- type Message
- type MessageOptions
- type Metrics
- type NoOpLimiter
- type RateLimitedError
- type RateLimiter
- type RequestLogger
- type RichMessage
- type Service
- func (s *Service) GetStats() *Stats
- func (s *Service) Health(ctx context.Context) error
- func (s *Service) Ping(ctx context.Context) error
- func (s *Service) PingWithOptions(ctx context.Context, opts *MessageOptions) error
- func (s *Service) Send(message string, opts *MessageOptions) (string, error)
- func (s *Service) SendAlert(message string) (string, error)
- func (s *Service) SendAlertWithContext(ctx context.Context, message string) (string, error)
- func (s *Service) SendAlertWithOptions(message string, opts *MessageOptions) (string, error)
- func (s *Service) SendAlertWithOptionsContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
- func (s *Service) SendBatch(ctx context.Context, messages []string, opts *MessageOptions) []BatchResult
- func (s *Service) SendError(err error) (string, error)
- func (s *Service) SendErrorWithContext(ctx context.Context, err error) (string, error)
- func (s *Service) SendInfo(message string) (string, error)
- func (s *Service) SendInfoWithContext(ctx context.Context, message string) (string, error)
- func (s *Service) SendInfoWithOptions(message string, opts *MessageOptions) (string, error)
- func (s *Service) SendInfoWithOptionsContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
- func (s *Service) SendRichBatch(ctx context.Context, messages []*RichMessage) []BatchResult
- func (s *Service) SendRichMessage(ctx context.Context, msg *RichMessage) (string, error)
- func (s *Service) SendSuccess(message string) (string, error)
- func (s *Service) SendSuccessWithContext(ctx context.Context, message string) (string, error)
- func (s *Service) SendWarning(message string) (string, error)
- func (s *Service) SendWarningWithContext(ctx context.Context, message string) (string, error)
- func (s *Service) SendWarningWithOptions(message string, opts *MessageOptions) (string, error)
- func (s *Service) SendWarningWithOptionsContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
- func (s *Service) SendWithContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
- func (s *Service) SetDebug(debug bool) *Service
- func (s *Service) SetDefaultChannel(channel string) *Service
- func (s *Service) SetDefaultIcon(iconEmoji string) *Service
- func (s *Service) SetDefaultIconURL(iconURL string) *Service
- func (s *Service) SetDefaultUsername(username string) *Service
- func (s *Service) Shutdown(ctx context.Context) error
- type SlidingWindowLimiter
- type Stats
- type TextObject
- type TokenBucketLimiter
Constants ¶
This section is empty.
Variables ¶
var ( // Configuration errors ErrInvalidConfig = errors.New("invalid configuration") ErrWebhookNotConfigured = errors.New("webhook URL not configured") ErrNotInitialized = errors.New("service not initialized") // API errors ErrRateLimited = errors.New("slack rate limit exceeded") ErrInvalidResponse = errors.New("invalid response from Slack") ErrMessageTooLarge = errors.New("message exceeds Slack's size limit") ErrInvalidChannel = errors.New("invalid channel format") ErrWebhookFailed = errors.New("webhook request failed") // Retry errors ErrMaxRetriesExceeded = errors.New("maximum retries exceeded") ErrContextCanceled = errors.New("context canceled") // Security errors ErrInputTooLarge = errors.New("input exceeds maximum size") ErrInvalidInput = errors.New("invalid input detected") ErrSanitizationFailed = errors.New("input sanitization failed") )
Standard errors for the slack package
var ( // ErrCircuitOpen is returned when the circuit is open ErrCircuitOpen = errors.New("circuit breaker is open") )
Functions ¶
Types ¶
type Attachment ¶ added in v0.1.1
type Attachment struct {
Color string `json:"color,omitempty"`
Fallback string `json:"fallback,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text,omitempty"`
Pretext string `json:"pretext,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
Fields []Field `json:"fields,omitempty"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
Blocks []Block `json:"blocks,omitempty"`
}
Attachment represents a Slack message attachment
type BatchResult ¶ added in v0.1.1
BatchResult represents the result of a batch send operation
type Block ¶ added in v0.1.1
type Block struct {
Type string `json:"type"`
Text *TextObject `json:"text,omitempty"`
BlockID string `json:"block_id,omitempty"`
Elements []BlockElement `json:"elements,omitempty"`
Fields []TextObject `json:"fields,omitempty"`
}
Block represents a Slack Block Kit block
func NewContextBlock ¶ added in v0.1.1
func NewContextBlock(elements []BlockElement) Block
NewContextBlock creates a new context block
func NewDividerBlock ¶ added in v0.1.1
func NewDividerBlock() Block
NewDividerBlock creates a new divider block
func NewHeaderBlock ¶ added in v0.1.1
NewHeaderBlock creates a new header block
func NewSectionBlock ¶ added in v0.1.1
NewSectionBlock creates a new section block
type BlockElement ¶ added in v0.1.1
type BlockElement struct {
Type string `json:"type"`
Text *TextObject `json:"text,omitempty"`
Value string `json:"value,omitempty"`
ActionID string `json:"action_id,omitempty"`
URL string `json:"url,omitempty"`
}
BlockElement represents an element within a block
func NewButtonElement ¶ added in v0.1.1
func NewButtonElement(text, actionID, value string) BlockElement
NewButtonElement creates a new button element
func NewLinkButtonElement ¶ added in v0.1.1
func NewLinkButtonElement(text, url string) BlockElement
NewLinkButtonElement creates a new link button element
type Builder ¶ added in v0.1.0
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a way to create Slack service instances with custom prefixes
func WithPrefix ¶ added in v0.1.0
WithPrefix creates a new Builder with the specified prefix
type CircuitBreaker ¶ added in v0.1.1
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker protects against cascading failures
func NewCircuitBreaker ¶ added in v0.1.1
func NewCircuitBreaker(maxFailures int, timeout time.Duration, maxRequests int) *CircuitBreaker
NewCircuitBreaker creates a new circuit breaker
func (*CircuitBreaker) Allow ¶ added in v0.1.1
func (cb *CircuitBreaker) Allow() error
Allow checks if a request is allowed through the circuit
func (*CircuitBreaker) Execute ¶ added in v0.1.1
func (cb *CircuitBreaker) Execute(ctx context.Context, fn func() error) error
Execute runs a function with circuit breaker protection
func (*CircuitBreaker) RecordFailure ¶ added in v0.1.1
func (cb *CircuitBreaker) RecordFailure()
RecordFailure records a failed request
func (*CircuitBreaker) RecordSuccess ¶ added in v0.1.1
func (cb *CircuitBreaker) RecordSuccess()
RecordSuccess records a successful request
func (*CircuitBreaker) Reset ¶ added in v0.1.1
func (cb *CircuitBreaker) Reset()
Reset resets the circuit breaker to closed state
func (*CircuitBreaker) State ¶ added in v0.1.1
func (cb *CircuitBreaker) State() CircuitState
State returns the current state of the circuit breaker
type CircuitBreakerError ¶ added in v0.1.1
type CircuitBreakerError struct {
State CircuitState
Message string
}
CircuitBreakerError represents a circuit breaker error
func (*CircuitBreakerError) Error ¶ added in v0.1.1
func (e *CircuitBreakerError) Error() string
type CircuitState ¶ added in v0.1.1
type CircuitState int
CircuitState represents the state of a circuit breaker
const ( CircuitClosed CircuitState = iota CircuitOpen CircuitHalfOpen )
type Config ¶
type Config struct {
WebhookURL string `env:"WEBHOOK_URL"`
Channel string `env:"CHANNEL"`
Username string `env:"USERNAME,default:Beaver"`
IconEmoji string `env:"ICON_EMOJI"`
IconURL string `env:"ICON_URL"`
Timeout time.Duration `env:"TIMEOUT,default:10s"`
// Retry configuration
MaxRetries int `env:"MAX_RETRIES,default:3"`
RetryDelay time.Duration `env:"RETRY_DELAY,default:1s"`
RetryMaxDelay time.Duration `env:"RETRY_MAX_DELAY,default:30s"`
RetryJitter bool `env:"RETRY_JITTER,default:true"`
// Rate limiting
RateLimit int `env:"RATE_LIMIT,default:1"` // requests per second
RateBurst int `env:"RATE_BURST,default:10"` // burst size
// Circuit breaker
CircuitThreshold int `env:"CIRCUIT_THRESHOLD,default:5"` // failures before opening
CircuitTimeout time.Duration `env:"CIRCUIT_TIMEOUT,default:60s"` // time before half-open
CircuitMaxRequests int `env:"CIRCUIT_MAX_REQUESTS,default:1"` // requests in half-open state
// Security
MaxMessageSize int `env:"MAX_MESSAGE_SIZE,default:40000"` // Slack's limit is 40KB
SanitizeInput bool `env:"SANITIZE_INPUT,default:true"`
RedactErrors bool `env:"REDACT_ERRORS,default:true"`
// Monitoring
EnableMetrics bool `env:"ENABLE_METRICS,default:false"`
EnableLogging bool `env:"ENABLE_LOGGING,default:false"`
LogLevel string `env:"LOG_LEVEL,default:info"`
// Debug configuration
Debug bool `env:"DEBUG,default:false"`
}
Config defines slack configuration
type Field ¶ added in v0.1.1
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short,omitempty"`
}
Field represents a field in an attachment
type Logger ¶ added in v0.1.1
type Logger struct {
// contains filtered or unexported fields
}
Logger provides structured logging for the Slack service
type Message ¶
type Message struct {
Text string `json:"text"`
Channel string `json:"channel,omitempty"`
Username string `json:"username,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
}
Message represents a Slack message to be sent
type MessageOptions ¶
MessageOptions contains optional parameters for Slack messages
type Metrics ¶ added in v0.1.1
type Metrics struct {
// contains filtered or unexported fields
}
Metrics tracks various metrics for the Slack service
func NewMetrics ¶ added in v0.1.1
func NewMetrics() *Metrics
NewMetrics creates a new metrics instance
func (*Metrics) RecordCircuitOpen ¶ added in v0.1.1
func (m *Metrics) RecordCircuitOpen()
RecordCircuitOpen records when circuit breaker opens
func (*Metrics) RecordFailure ¶ added in v0.1.1
RecordFailure records a failed message
func (*Metrics) RecordMessage ¶ added in v0.1.1
func (m *Metrics) RecordMessage()
RecordMessage records a message attempt
func (*Metrics) RecordRateLimit ¶ added in v0.1.1
func (m *Metrics) RecordRateLimit()
RecordRateLimit records a rate limit hit
func (*Metrics) RecordSuccess ¶ added in v0.1.1
RecordSuccess records a successful message
type NoOpLimiter ¶ added in v0.1.1
type NoOpLimiter struct{}
NoOpLimiter is a rate limiter that allows all requests
type RateLimitedError ¶ added in v0.1.1
RateLimitedError wraps an error with rate limit information
func (*RateLimitedError) Error ¶ added in v0.1.1
func (e *RateLimitedError) Error() string
type RateLimiter ¶ added in v0.1.1
RateLimiter provides rate limiting functionality
type RequestLogger ¶ added in v0.1.1
type RequestLogger struct {
// contains filtered or unexported fields
}
RequestLogger logs requests with sensitive data redaction
func NewRequestLogger ¶ added in v0.1.1
func NewRequestLogger(logger *Logger, redact bool) *RequestLogger
NewRequestLogger creates a new request logger
func (*RequestLogger) LogRequest ¶ added in v0.1.1
func (rl *RequestLogger) LogRequest(ctx context.Context, payload string)
LogRequest logs an outgoing request
func (*RequestLogger) LogResponse ¶ added in v0.1.1
func (rl *RequestLogger) LogResponse(ctx context.Context, statusCode int, body string)
LogResponse logs an incoming response
type RichMessage ¶ added in v0.1.1
type RichMessage struct {
Text string `json:"text,omitempty"`
Channel string `json:"channel,omitempty"`
Username string `json:"username,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Blocks []Block `json:"blocks,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
RichMessage represents a rich Slack message with blocks and attachments
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service represents a Slack notification service
func (*Service) PingWithOptions ¶ added in v0.1.1
func (s *Service) PingWithOptions(ctx context.Context, opts *MessageOptions) error
PingWithOptions sends a test message with custom options
func (*Service) Send ¶
func (s *Service) Send(message string, opts *MessageOptions) (string, error)
Send sends a raw message to Slack (deprecated: use SendWithContext)
func (*Service) SendAlertWithContext ¶ added in v0.1.1
SendAlertWithContext sends an alert message to Slack with context
func (*Service) SendAlertWithOptions ¶
func (s *Service) SendAlertWithOptions(message string, opts *MessageOptions) (string, error)
SendAlertWithOptions sends an alert message to Slack with custom options
func (*Service) SendAlertWithOptionsContext ¶ added in v0.1.1
func (s *Service) SendAlertWithOptionsContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
SendAlertWithOptionsContext sends an alert message with context and options
func (*Service) SendBatch ¶ added in v0.1.1
func (s *Service) SendBatch(ctx context.Context, messages []string, opts *MessageOptions) []BatchResult
SendBatch sends multiple messages in parallel
func (*Service) SendError ¶ added in v0.1.1
SendError sends an error message to Slack with proper redaction
func (*Service) SendErrorWithContext ¶ added in v0.1.1
SendErrorWithContext sends an error message to Slack with context
func (*Service) SendInfoWithContext ¶ added in v0.1.1
SendInfoWithContext sends an informational message to Slack with context
func (*Service) SendInfoWithOptions ¶
func (s *Service) SendInfoWithOptions(message string, opts *MessageOptions) (string, error)
SendInfoWithOptions sends an informational message to Slack with custom options
func (*Service) SendInfoWithOptionsContext ¶ added in v0.1.1
func (s *Service) SendInfoWithOptionsContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
SendInfoWithOptionsContext sends an informational message with context and options
func (*Service) SendRichBatch ¶ added in v0.1.1
func (s *Service) SendRichBatch(ctx context.Context, messages []*RichMessage) []BatchResult
SendRichBatch sends multiple rich messages in parallel
func (*Service) SendRichMessage ¶ added in v0.1.1
SendRichMessage sends a rich message with blocks and attachments
func (*Service) SendSuccess ¶ added in v0.1.1
SendSuccess sends a success message to Slack
func (*Service) SendSuccessWithContext ¶ added in v0.1.1
SendSuccessWithContext sends a success message to Slack with context
func (*Service) SendWarning ¶
SendWarning sends a warning message to Slack
func (*Service) SendWarningWithContext ¶ added in v0.1.1
SendWarningWithContext sends a warning message to Slack with context
func (*Service) SendWarningWithOptions ¶
func (s *Service) SendWarningWithOptions(message string, opts *MessageOptions) (string, error)
SendWarningWithOptions sends a warning message to Slack with custom options
func (*Service) SendWarningWithOptionsContext ¶ added in v0.1.1
func (s *Service) SendWarningWithOptionsContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
SendWarningWithOptionsContext sends a warning message with context and options
func (*Service) SendWithContext ¶ added in v0.1.1
func (s *Service) SendWithContext(ctx context.Context, message string, opts *MessageOptions) (string, error)
SendWithContext sends a raw message to Slack with context support
func (*Service) SetDefaultChannel ¶
SetDefaultChannel sets the default channel for all messages sent by this service
func (*Service) SetDefaultIcon ¶
SetDefaultIcon sets the default icon emoji for all messages sent by this service
func (*Service) SetDefaultIconURL ¶
SetDefaultIconURL sets the default icon URL for all messages sent by this service
func (*Service) SetDefaultUsername ¶
SetDefaultUsername sets the default username for all messages sent by this service
type SlidingWindowLimiter ¶ added in v0.1.1
type SlidingWindowLimiter struct {
// contains filtered or unexported fields
}
SlidingWindowLimiter implements sliding window rate limiting
func NewSlidingWindowLimiter ¶ added in v0.1.1
func NewSlidingWindowLimiter(limit int, window time.Duration) *SlidingWindowLimiter
NewSlidingWindowLimiter creates a new sliding window rate limiter
type Stats ¶ added in v0.1.1
type Stats struct {
MessagesSent int64
MessagesSucceeded int64
MessagesFailed int64
RateLimitHits int64
CircuitOpens int64
AverageLatency time.Duration
ErrorCounts map[string]int64
}
Stats represents service statistics
type TextObject ¶ added in v0.1.1
type TextObject struct {
Type string `json:"type"` // "plain_text" or "mrkdwn"
Text string `json:"text"`
Emoji bool `json:"emoji,omitempty"`
}
TextObject represents text in Slack blocks
type TokenBucketLimiter ¶ added in v0.1.1
type TokenBucketLimiter struct {
// contains filtered or unexported fields
}
TokenBucketLimiter implements token bucket rate limiting
func NewTokenBucketLimiter ¶ added in v0.1.1
func NewTokenBucketLimiter(rate int, burst int) *TokenBucketLimiter
NewTokenBucketLimiter creates a new token bucket rate limiter