1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-24 08:22:18 +02:00

Merge pull request #39 from nikoksr/refactor/simplify-notifier-interface

refactor(notifier): Simplify notify.Notifier interface
This commit is contained in:
Niko Köser 2021-02-12 11:45:17 +01:00 committed by GitHub
commit c507ab342e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 24 deletions

View File

@ -1,14 +1,9 @@
package notify package notify
// Notifier defines the behavior for notification services. It implements Send and AddReciever // Notifier defines the behavior for notification services.
// //
// The Send command simply sends a message string to the internal destination Notifier. // The Send function simply sends a subject and a message string to the internal destination Notifier.
// E.g for telegram it sends the message to the specified group chat. // E.g for telegram.Telegram it sends the message to the specified group chat.
//
// The AddReceivers takes one or many strings and
// adds these to the list of destinations for receiving messages
// e.g. slack channels, telegram chats, email addresses.
type Notifier interface { type Notifier interface {
Send(string, string) error Send(string, string) error
AddReceivers(...string)
} }

View File

@ -18,7 +18,7 @@ type Notify struct {
// ErrSendNotification signals that the notifier failed to send a notification. // ErrSendNotification signals that the notifier failed to send a notification.
var ErrSendNotification = errors.New("send notification") var ErrSendNotification = errors.New("send notification")
// New returns a new instance of Notify. Defaulting to being not disabled // New returns a new instance of Notify. Defaulting to being not disabled.
func New() *Notify { func New() *Notify {
notifier := &Notify{ notifier := &Notify{
Disabled: defaultDisabled, Disabled: defaultDisabled,

View File

@ -5,7 +5,7 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
// Send calls the underlying notification services to send the given message to their respective endpoints. // Send calls the underlying notification services to send the given subject and message to their respective endpoints.
func (n Notify) Send(subject, message string) error { func (n Notify) Send(subject, message string) error {
if n.Disabled { if n.Disabled {
return nil return nil

View File

@ -1,8 +1,6 @@
package telegram package telegram
import ( import (
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -34,13 +32,8 @@ func New(apiToken string) (*Telegram, error) {
// AddReceivers takes Telegram chat IDs and adds them to the internal chat ID list. The Send method will send // AddReceivers takes Telegram chat IDs and adds them to the internal chat ID list. The Send method will send
// a given message to all those chats. // a given message to all those chats.
func (t *Telegram) AddReceivers(chatIDs ...string) { func (t *Telegram) AddReceivers(chatIDs ...int64) {
for _, v := range chatIDs { t.chatIDs = append(t.chatIDs, chatIDs...)
chatID, err := strconv.ParseInt(v, 10, 64)
if err == nil {
t.chatIDs = append(t.chatIDs, chatID)
}
}
} }
// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports // Send takes a message subject and a message body and sends them to all previously set chats. Message body supports

9
use.go
View File

@ -1,14 +1,13 @@
package notify package notify
// useService adds a given service to the notifiers services list. // useService adds a given service to the Notifier's services list.
func (n *Notify) useService(service Notifier) { func (n *Notify) useService(service Notifier) {
if service == nil { if service != nil {
return
}
n.notifiers = append(n.notifiers, service) n.notifiers = append(n.notifiers, service)
}
} }
// UseServices adds the given service(s) to the notifiers services list. // UseServices adds the given service(s) to the Notifier's services list.
func (n *Notify) UseServices(service ...Notifier) { func (n *Notify) UseServices(service ...Notifier) {
for _, s := range service { for _, s := range service {
n.useService(s) n.useService(s)