From 472ffde83c5110d8da1b50fd6e87ac39bb4bdbec Mon Sep 17 00:00:00 2001 From: KrishanBhalla Date: Sat, 30 Jan 2021 20:39:07 +0000 Subject: [PATCH 1/5] Removed pseudo --- notify.go | 8 ++------ service/pseudo/pseudo.go | 15 --------------- use.go | 17 ----------------- 3 files changed, 2 insertions(+), 38 deletions(-) delete mode 100644 service/pseudo/pseudo.go diff --git a/notify.go b/notify.go index fc50186..4070675 100644 --- a/notify.go +++ b/notify.go @@ -25,13 +25,9 @@ type Notifier interface { // service under the hood. func New() *Notify { notifier := &Notify{ - Disabled: defaultDisabled, + Disabled: defaultDisabled, + notifiers: []Notifier{}, } - // 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 - // doing nothing but preventing nil-reference errors. - notifier.usePseudo() - return notifier } diff --git a/service/pseudo/pseudo.go b/service/pseudo/pseudo.go deleted file mode 100644 index 0505ba8..0000000 --- a/service/pseudo/pseudo.go +++ /dev/null @@ -1,15 +0,0 @@ -package pseudo - -// Pseudo struct represents a dummy notification service. -type Pseudo struct{} - -// New returns a new instance of a Pseudo notification service. This is used internally to initialize -// notification services list and prevent nil-reference errors. -func New() *Pseudo { - return &Pseudo{} -} - -// Send basically does nothing. Just here to conform the notify.Notifier interface. -func (Pseudo) Send(string, string) error { - return nil -} diff --git a/use.go b/use.go index c3acf41..5e9110c 100644 --- a/use.go +++ b/use.go @@ -1,9 +1,5 @@ package notify -import ( - "github.com/nikoksr/notify/service/pseudo" -) - // useService adds a given service to the notifiers services list. If the list still contains // a pseudo service we remove it before adding the 'real' service. func (n *Notify) useService(service Notifier) { @@ -11,22 +7,9 @@ func (n *Notify) useService(service Notifier) { return } - // Remove pseudo service in case a 'real' service will be added - if len(n.notifiers) > 0 { - _, isPseudo := n.notifiers[0].(*pseudo.Pseudo) - if isPseudo { - n.notifiers = n.notifiers[1:] - } - } - n.notifiers = append(n.notifiers, service) } -// usePseudo adds a pseudo notification service to the notifiers services list. -func (n *Notify) usePseudo() { - n.useService(pseudo.New()) -} - // UseService adds a given service to the notifiers services list. func (n *Notify) UseService(service Notifier) { n.useService(service) From 80fca4a6175ff73c6e1e18b74a09b0abc65b4b2d Mon Sep 17 00:00:00 2001 From: KrishanBhalla Date: Sat, 30 Jan 2021 21:07:06 +0000 Subject: [PATCH 2/5] Updating the Notifier Interface --- notify.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/notify.go b/notify.go index fc50186..7967c2f 100644 --- a/notify.go +++ b/notify.go @@ -15,10 +15,17 @@ type Notify struct { // ErrSendNotification signals that the notifier failed to send a notification. var ErrSendNotification = errors.New("Send notification") -// 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. +// Notifier defines the behavior for notification services. It implements Send and AddReciever +// +// 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 AddRecievers method takes one or many interfaces (ints, strings etc.) depending +// on the underlying notifier. When sending, these form the targets. +// e.g. slack channels, telegram chats, email addresses. type Notifier interface { Send(string, string) error + AddRecievers(...interface{}) } // New returns a new instance of Notify. Defaulting to being not disabled and using the pseudo notification From 3d7cc206e13d7149c0e9750d257f43abd276b1f4 Mon Sep 17 00:00:00 2001 From: KrishanBhalla Date: Sat, 30 Jan 2021 21:20:02 +0000 Subject: [PATCH 3/5] Updated Notifier interface, altered telegram AddRecievers --- notifier.go | 14 ++++++++++++++ notify.go | 13 ------------- service/telegram/telegram.go | 10 ++++++++-- 3 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 notifier.go diff --git a/notifier.go b/notifier.go new file mode 100644 index 0000000..6aa1b76 --- /dev/null +++ b/notifier.go @@ -0,0 +1,14 @@ +package notify + +// Notifier defines the behavior for notification services. It implements Send and AddReciever +// +// 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 recieving messages +// e.g. slack channels, telegram chats, email addresses. +type Notifier interface { + Send(string, string) error + AddReceivers(...string) +} diff --git a/notify.go b/notify.go index faabd77..c6fb44e 100644 --- a/notify.go +++ b/notify.go @@ -15,19 +15,6 @@ type Notify struct { // ErrSendNotification signals that the notifier failed to send a notification. var ErrSendNotification = errors.New("Send notification") -// Notifier defines the behavior for notification services. It implements Send and AddReciever -// -// 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 AddRecievers method takes one or many interfaces (ints, strings etc.) depending -// on the underlying notifier. When sending, these form the targets. -// e.g. slack channels, telegram chats, email addresses. -type Notifier interface { - Send(string, string) error - AddRecievers(...interface{}) -} - // New returns a new instance of Notify. Defaulting to being not disabled and using the pseudo notification // service under the hood. func New() *Notify { diff --git a/service/telegram/telegram.go b/service/telegram/telegram.go index 6697f06..93edcf0 100644 --- a/service/telegram/telegram.go +++ b/service/telegram/telegram.go @@ -1,6 +1,8 @@ package telegram import ( + "strconv" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/pkg/errors" ) @@ -33,8 +35,12 @@ 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 ...int64) { - t.chatIDs = append(t.chatIDs, chatIDs...) +func (t *Telegram) AddReceivers(chatIDs ...string) { + for _, v := range chatIDs { + if chatID, err := strconv.ParseInt(v, 10, 64); 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 From 65fd3139a11d273cebc26c5c099391758c959521 Mon Sep 17 00:00:00 2001 From: KrishanBhalla Date: Sat, 30 Jan 2021 21:21:24 +0000 Subject: [PATCH 4/5] Modified README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 79640a6..7ca0ceb 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ telegramService, _ := telegram.New("your_telegram_api_token") // Passing a telegram chat id as receiver for our messages. // Basically where should our message be sent to? -telegramService.AddReceivers(-1234567890) +telegramService.AddReceivers("-1234567890") // Tell our notifier to use the telegram service. You can repeat the above process // for as many services as you like and just tell the notifier to use them. From b07e71dadeb64c5de5f7a22afbe3a04d77d06cb7 Mon Sep 17 00:00:00 2001 From: KrishanBhalla Date: Sun, 31 Jan 2021 12:14:48 +0000 Subject: [PATCH 5/5] Requested Changes --- notify.go | 3 +-- service/telegram/telegram.go | 3 ++- use.go | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/notify.go b/notify.go index 151fdcd..d9497e6 100644 --- a/notify.go +++ b/notify.go @@ -15,8 +15,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 and using the pseudo notification -// service under the hood. +// New returns a new instance of Notify. Defaulting to being not disabled func New() *Notify { notifier := &Notify{ Disabled: defaultDisabled, diff --git a/service/telegram/telegram.go b/service/telegram/telegram.go index 841c8f0..e20d401 100644 --- a/service/telegram/telegram.go +++ b/service/telegram/telegram.go @@ -36,7 +36,8 @@ func New(apiToken string) (*Telegram, error) { // a given message to all those chats. func (t *Telegram) AddReceivers(chatIDs ...string) { for _, v := range chatIDs { - if chatID, err := strconv.ParseInt(v, 10, 64); err != nil { + chatID, err := strconv.ParseInt(v, 10, 64) + if err != nil { t.chatIDs = append(t.chatIDs, chatID) } } diff --git a/use.go b/use.go index 5e9110c..0bb0daf 100644 --- a/use.go +++ b/use.go @@ -1,7 +1,6 @@ package notify -// useService adds a given service to the notifiers services list. If the list still contains -// a pseudo service we remove it before adding the 'real' service. +// useService adds a given service to the notifiers services list. func (n *Notify) useService(service Notifier) { if service == nil { return