2021-01-25 01:14:21 +02:00
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
const defaultDisabled = false // Notifier is enabled by default
|
2021-01-25 01:14:21 +02:00
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
// Notify is the central struct for managing notification services and sending messages to them.
|
|
|
|
type Notify struct {
|
|
|
|
Disabled bool
|
|
|
|
notifiers []Notifier
|
|
|
|
}
|
2021-01-25 01:14:21 +02:00
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
// ErrSendNotification signals that the notifier failed to send a notification.
|
2021-01-25 01:14:21 +02:00
|
|
|
var ErrSendNotification = errors.New("Send notification")
|
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
// Notifier defines the behavior for notification services. The Send command simply sends a message string to the
|
|
|
|
// internal destination Notifier. E.g for telegram it sends the message to the specified group chat.
|
|
|
|
type Notifier interface {
|
2021-01-25 01:14:21 +02:00
|
|
|
Send(string, string) error
|
|
|
|
}
|
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
// New returns a new instance of Notify. Defaulting to being not disabled and using the pseudo notification
|
|
|
|
// service under the hood.
|
|
|
|
func New() *Notify {
|
|
|
|
notifier := &Notify{
|
2021-01-25 01:14:21 +02:00
|
|
|
Disabled: defaultDisabled,
|
|
|
|
}
|
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
// Use the pseudo Notifier to prevent from nil reference bugs when using the Notify Notifier. In case no notifiers
|
|
|
|
// are provided or the creation of all other notifiers failed, the pseudo Notifier will be used under the hood
|
2021-01-25 01:14:21 +02:00
|
|
|
// doing nothing but preventing nil-reference errors.
|
|
|
|
notifier.usePseudo()
|
|
|
|
|
|
|
|
return notifier
|
|
|
|
}
|