notification

package
v0.0.0-...-c9de886 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

notification

A multi-channel notification router with a pluggable provider model. Your application registers one provider per channel type (email, SMS, etc.); the manager dispatches each Request to the correct provider at runtime.

Usage

import "github.com/OpenNSW/core/notification"

manager, err := notification.NewManager(
    notification.Config{Path: "configs/notification.json"},
    myEmailProvider,
    mySMSProvider,
)

err = manager.Send(ctx, notification.Request{
    Channel: notification.ChannelEmail,
    To:      "applicant@example.com",
    Subject: "Application received",
    Body:    "Your application #12345 has been received and is under review.",
    HTMLBody: "<p>Your application <strong>#12345</strong> has been received.</p>",
})

Channels

Constant Value
notification.ChannelEmail "email"
notification.ChannelSMS "sms"

Writing a provider

Implement notification.Provider:

type Provider interface {
    Type()                          ChannelType
    Configure(cfg json.RawMessage)  error
    Send(ctx context.Context, req Request) error
}
  • Type() declares which channel this provider handles.
  • Configure is called at startup with the provider-specific JSON block from notification.json.
  • Send delivers the message.
type MyEmailProvider struct {
    apiKey string
}

func (p *MyEmailProvider) Type() notification.ChannelType { return notification.ChannelEmail }

func (p *MyEmailProvider) Configure(cfg json.RawMessage) error {
    var c struct{ APIKey string `json:"api_key"` }
    if err := json.Unmarshal(cfg, &c); err != nil { return err }
    p.apiKey = c.APIKey
    return nil
}

func (p *MyEmailProvider) Send(ctx context.Context, req notification.Request) error {
    // send via your email API
    return nil
}

Provider config file

notification.json holds provider-specific configuration keyed by channel type:

{
  "providers": {
    "email": {
      "api_key": "sg-xxxxx",
      "from_address": "noreply@example.com"
    },
    "sms": {
      "account_sid": "ACxxxxx",
      "auth_token": "xxxxx",
      "from_number": "+61400000000"
    }
  }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidChannel = errors.New("invalid or missing channel")
	ErrToRequired     = errors.New("to address is required")
	ErrBodyRequired   = errors.New("body or html_body is required")
)
View Source
var ErrConfigPathRequired = errors.New("notification config path is required")

Functions

This section is empty.

Types

type ChannelType

type ChannelType string

ChannelType identifies the delivery channel for a notification.

const (
	ChannelSMS   ChannelType = "sms"
	ChannelEmail ChannelType = "email"
)

type Config

type Config struct {
	Path string
}

Config holds the file-based notifications subsystem configuration.

func (Config) Validate

func (c Config) Validate() error

Validate returns ErrConfigPathRequired when Path is empty.

type Manager

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

Manager routes notification requests to registered providers. It is safe for concurrent use after construction.

func NewManager

func NewManager(cfg Config, providers ...Provider) (*Manager, error)

NewManager loads the provider config from cfg.Path, configures each provider from its JSON blob, and returns a ready Manager. Returns an error if cfg is invalid, the config file cannot be read, a provider's key is missing from the file, or any provider's Configure call fails.

func (*Manager) Send

func (m *Manager) Send(ctx context.Context, req Request) error

Send validates req and dispatches it to the registered provider for req.Channel. Returns an error if the request is invalid, no provider is registered for the channel, or the provider's Send call fails.

type Provider

type Provider interface {
	Type() ChannelType
	Configure(cfg json.RawMessage) error
	Send(ctx context.Context, req Request) error
}

Provider is implemented by each notification channel (email, SMS, etc.).

type Request

type Request struct {
	Channel  ChannelType `json:"channel"`
	To       string      `json:"to"`
	Subject  string      `json:"subject,omitempty"`
	Body     string      `json:"body,omitempty"`
	HTMLBody string      `json:"html_body,omitempty"`
}

func (Request) Validate

func (r Request) Validate() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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