Documentation
¶
Overview ¶
Package notify provides a unified notification system for sending messages to various channels like webhooks, Slack, Discord, email, and system notifications.
Basic usage:
// Create a notifier
notifier := notify.NewMultiplexer()
// Add providers
notifier.AddProvider(webhook.New("https://hooks.example.com/notify"))
notifier.AddProvider(slack.New(slack.Config{Token: "xoxb-...", Channel: "#alerts"}))
// Send notification
err := notifier.Send(context.Background(), ¬ify.Notification{
Title: "Deployment Complete",
Message: "App v1.2.3 deployed successfully",
Level: notify.LevelInfo,
})
Index ¶
- Variables
- func FormatError(provider string, err error) error
- func IsLevelAtLeast(level, min Level) bool
- type Level
- type Link
- type Multiplexer
- func (m *Multiplexer) AddProvider(provider Provider)
- func (m *Multiplexer) AddProviderWithConfig(provider Provider, config *ProviderConfig)
- func (m *Multiplexer) Close() error
- func (m *Multiplexer) GetProvider(name string) Provider
- func (m *Multiplexer) ListProviders() []string
- func (m *Multiplexer) RemoveProvider(name string) bool
- func (m *Multiplexer) Send(ctx context.Context, notification *Notification) ([]*Result, error)
- func (m *Multiplexer) SendTo(ctx context.Context, providerName string, notification *Notification) (*Result, error)
- func (m *Multiplexer) SetProviderConfig(name string, config ProviderConfig)
- type MultiplexerOption
- type Notification
- type Notifier
- type Provider
- type ProviderConfig
- type Result
Constants ¶
This section is empty.
Variables ¶
var ( // ErrProviderNotFound is returned when a provider is not found ErrProviderNotFound = errors.New("provider not found") // ErrSendFailed is returned when sending a notification fails ErrSendFailed = errors.New("failed to send notification") )
Error types
Functions ¶
func FormatError ¶
FormatError formats an error with provider context
func IsLevelAtLeast ¶
IsLevelAtLeast checks if a level is at least as severe as another
Types ¶
type Level ¶
type Level string
Level represents the severity level of a notification
type Multiplexer ¶
type Multiplexer struct {
// contains filtered or unexported fields
}
Multiplexer sends notifications to multiple providers
func NewMultiplexer ¶
func NewMultiplexer(opts ...MultiplexerOption) *Multiplexer
NewMultiplexer creates a new notification multiplexer
func (*Multiplexer) AddProvider ¶
func (m *Multiplexer) AddProvider(provider Provider)
AddProvider adds a notification provider
func (*Multiplexer) AddProviderWithConfig ¶
func (m *Multiplexer) AddProviderWithConfig(provider Provider, config *ProviderConfig)
AddProviderWithConfig adds a notification provider with specific configuration
func (*Multiplexer) GetProvider ¶
func (m *Multiplexer) GetProvider(name string) Provider
GetProvider returns a provider by name
func (*Multiplexer) ListProviders ¶
func (m *Multiplexer) ListProviders() []string
ListProviders returns all registered provider names
func (*Multiplexer) RemoveProvider ¶
func (m *Multiplexer) RemoveProvider(name string) bool
RemoveProvider removes a provider by name
func (*Multiplexer) Send ¶
func (m *Multiplexer) Send(ctx context.Context, notification *Notification) ([]*Result, error)
Send sends a notification to all configured providers concurrently
func (*Multiplexer) SendTo ¶
func (m *Multiplexer) SendTo(ctx context.Context, providerName string, notification *Notification) (*Result, error)
SendTo sends a notification to a specific provider
func (*Multiplexer) SetProviderConfig ¶
func (m *Multiplexer) SetProviderConfig(name string, config ProviderConfig)
SetProviderConfig sets configuration for a provider
type MultiplexerOption ¶
type MultiplexerOption func(*Multiplexer)
MultiplexerOption configures a Multiplexer
func WithDefaultRetry ¶
func WithDefaultRetry(count int) MultiplexerOption
WithDefaultRetry sets the default retry count for all providers
func WithMinLevel ¶
func WithMinLevel(level Level) MultiplexerOption
WithMinLevel sets the minimum notification level to send
type Notification ¶
type Notification struct {
// Title is the notification title/subject
Title string `json:"title,omitempty"`
// Message is the main notification content
Message string `json:"message"`
// Level indicates the severity (debug, info, warning, error, critical)
Level Level `json:"level,omitempty"`
// Category for grouping related notifications
Category string `json:"category,omitempty"`
// Tags for filtering and routing
Tags []string `json:"tags,omitempty"`
// Timestamp of the notification (defaults to now if not set)
Timestamp time.Time `json:"timestamp,omitempty"`
// Metadata for additional context
Metadata map[string]interface{} `json:"metadata,omitempty"`
// Links to include in the notification
Links []Link `json:"links,omitempty"`
// Image URL to include (supported by some providers)
ImageURL string `json:"image_url,omitempty"`
// Provider-specific overrides
ProviderData map[string]interface{} `json:"provider_data,omitempty"`
}
Notification represents a notification message to be sent
func (*Notification) Validate ¶
func (n *Notification) Validate() error
Validate checks if the notification is valid
type Notifier ¶
type Notifier interface {
// Send sends a notification to all configured providers
Send(ctx context.Context, notification *Notification) ([]*Result, error)
// SendTo sends a notification to a specific provider by name
SendTo(ctx context.Context, providerName string, notification *Notification) (*Result, error)
// AddProvider adds a notification provider
AddProvider(provider Provider)
// RemoveProvider removes a provider by name
RemoveProvider(name string) bool
// GetProvider returns a provider by name
GetProvider(name string) Provider
// ListProviders returns all registered provider names
ListProviders() []string
}
Notifier defines the interface for sending notifications
type Provider ¶
type Provider interface {
// Name returns the provider name
Name() string
// Send sends a notification
Send(ctx context.Context, notification *Notification) (*Result, error)
// Close cleans up provider resources
Close() error
}
Provider defines the interface for notification providers
type ProviderConfig ¶
type ProviderConfig struct {
// Name overrides the default provider name
Name string `json:"name,omitempty"`
// Enabled controls whether the provider is active
Enabled bool `json:"enabled"`
// Timeout for send operations (default: 30 seconds)
Timeout time.Duration `json:"timeout,omitempty"`
// RetryCount for failed sends (default: 0, no retry)
RetryCount int `json:"retry_count,omitempty"`
// RetryDelay between retries (default: 1 second)
RetryDelay time.Duration `json:"retry_delay,omitempty"`
}
ProviderConfig holds common provider configuration
type Result ¶
type Result struct {
// Provider that sent the notification
Provider string `json:"provider"`
// Success indicates if the send was successful
Success bool `json:"success"`
// MessageID returned by the provider (if applicable)
MessageID string `json:"message_id,omitempty"`
// Error if the send failed
Error error `json:"error,omitempty"`
// Latency for the send operation
Latency time.Duration `json:"latency,omitempty"`
// Raw response from provider (for debugging)
Raw interface{} `json:"raw,omitempty"`
}
Result represents the result of sending a notification
Directories
¶
| Path | Synopsis |
|---|---|
|
Example demonstrating the notification system usage
|
Example demonstrating the notification system usage |
|
provider
|
|
|
discord
Package discord provides a Discord notification provider
|
Package discord provides a Discord notification provider |
|
email
Package email provides an SMTP email notification provider
|
Package email provides an SMTP email notification provider |
|
slack
Package slack provides a Slack notification provider
|
Package slack provides a Slack notification provider |
|
system
Package system provides desktop system notification provider using beeep
|
Package system provides desktop system notification provider using beeep |
|
webhook
Package webhook provides an HTTP webhook notification provider
|
Package webhook provides an HTTP webhook notification provider |