Skip to content

Commit

Permalink
Merge pull request #39 from nikoksr/refactor/simplify-notifier-interface
Browse files Browse the repository at this point in the history
refactor(notifier): Simplify notify.Notifier interface
  • Loading branch information
nikoksr authored Feb 12, 2021
2 parents 0d9ba9d + ecdfb6f commit c507ab3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 24 deletions.
11 changes: 3 additions & 8 deletions notifier.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
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.
// E.g for 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.
// The Send function simply sends a subject and a message string to the internal destination Notifier.
// E.g for telegram.Telegram it sends the message to the specified group chat.
type Notifier interface {
Send(string, string) error
AddReceivers(...string)
}
2 changes: 1 addition & 1 deletion notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Notify struct {
// ErrSendNotification signals that the notifier failed to send a 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 {
notifier := &Notify{
Disabled: defaultDisabled,
Expand Down
2 changes: 1 addition & 1 deletion send.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"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 {
if n.Disabled {
return nil
Expand Down
11 changes: 2 additions & 9 deletions service/telegram/telegram.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package telegram

import (
"strconv"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -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
// a given message to all those chats.
func (t *Telegram) AddReceivers(chatIDs ...string) {
for _, v := range chatIDs {
chatID, err := strconv.ParseInt(v, 10, 64)
if err == nil {
t.chatIDs = append(t.chatIDs, chatID)
}
}
func (t *Telegram) AddReceivers(chatIDs ...int64) {
t.chatIDs = append(t.chatIDs, chatIDs...)
}

// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
Expand Down
9 changes: 4 additions & 5 deletions use.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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) {
if service == nil {
return
if service != nil {
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) {
for _, s := range service {
n.useService(s)
Expand Down

0 comments on commit c507ab3

Please sign in to comment.