notif

package module
v0.0.0-...-616478d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2025 License: MIT Imports: 6 Imported by: 0

README

notif

A simple, extensible notification library for GO applications using only the standard library.

Installation

go get github.com/cph-dev/notif

Basic Usage

package main

import (
    "context"
    "github.com/cph-dev/notif"
)

func main() {
    // Create a Slack notifier
    slack := notif.NewSlackNotifier("https://hooks.slack.com/services/YOUR/WEBHOOK/URL")

    // Create a message
    msg := notif.Message{
        Title:       "System Alert",
        Description: "Database backup completed",
        Priority:    notif.PriorityNormal,
    }

    // Send notification
    if err := slack.Send(context.Background(), msg); err != nil {
        // Handle error
    }
}

Advanced Usage

With Decorators
import "github.com/cph-dev/notif/decorator"

// Add logging
withLogging := decorator.WithLogging(withRetry, nil)

// Add retry logic
withRetry := decorator.WithRetry(slack, 3, time.Second, 10*time.Second)


// Use the decorated notifier
err := withRetry.Send(ctx, msg)
Multiple Notifiers
// Create multiple notifiers
slack := notif.NewSlackNotifier(slackURL)
email := notif.NewEmailNotifier(emailConfig) // Future

// Send to both
for _, n := range []notif.Notifier{slack, email} {
	if err := n.Send(ctx, msg); err != nil {
		// Handle error
	}
}

Message Format

type Message struct {
    Title       string              // Subject or header
    Content     string              // Main content
    URI         string              // Optional link
    Priority    Priority            // Low, Normal, High, Urgent
    Extra       map[string]any      // Additional fields
}

Extending

Implement the Notifier interface:

type Notifier interface {
    Send(ctx context.Context, msg Message) error
    Name() string
}

Features

  • Composable: Combine with decorators for additional functionality
  • Zero Dependencies: Uses only Go standard library
  • Context Support: Full support for cancellation and timeouts

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Timeout time.Duration
}

Config holds common configuration options

type Message

type Message struct {
	Title    string         // Subject line or header
	Content  string         // Main content body
	URI      string         // Optional link for more details
	Priority Priority       // Message priority level
	Extra    map[string]any // Additional fields for rich notifications
}

Message represents a normalized notification message. All notifiers expect messages in this format.

type Notifier

type Notifier interface {
	Send(ctx context.Context, msg Message) error
	Name() string
}

Notifier is the interface that all notification methods must implement

type Option

type Option func(*Config)

Option is a function that modifies configuration

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout for notifications

type Priority

type Priority int

Priority represents the urgency level of a notification

const (
	PriorityLow Priority = iota
	PriorityNormal
	PriorityHigh
	PriorityUrgent
)

type SlackNotifier

type SlackNotifier struct {
	// contains filtered or unexported fields
}

SlackNotifier sends notifications to Slack via webhook

func NewSlackNotifier

func NewSlackNotifier(webhookURL string, opts ...Option) *SlackNotifier

NewSlackNotifier creates a new Slack notifier

func (*SlackNotifier) Name

func (s *SlackNotifier) Name() string

func (*SlackNotifier) Send

func (s *SlackNotifier) Send(ctx context.Context, msg Message) error

Send sends a notification to Slack

type SlackOption

type SlackOption func(*SlackNotifier)

SlackOption is a function that modifies SlackNotifier configuration

func WithHTTPClient

func WithHTTPClient(client *http.Client) SlackOption

WithHTTPClient sets a custom HTTP client for Slack notifications

Directories

Path Synopsis
examples
advanced command
basic command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL