mirror of
https://github.com/nikoksr/notify.git
synced 2024-11-28 08:39:13 +02:00
Merge branch 'main' into feature/TwitterService
This commit is contained in:
commit
931d5cbd2b
@ -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.
|
||||
|
14
notifier.go
Normal file
14
notifier.go
Normal file
@ -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)
|
||||
}
|
17
notify.go
17
notify.go
@ -15,23 +15,12 @@ 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.
|
||||
type Notifier interface {
|
||||
Send(string, string) error
|
||||
}
|
||||
|
||||
// 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,
|
||||
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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -32,8 +34,13 @@ 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 {
|
||||
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
|
||||
|
20
use.go
20
use.go
@ -1,32 +1,14 @@
|
||||
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.
|
||||
// useService adds a given service to the notifiers services list.
|
||||
func (n *Notify) useService(service Notifier) {
|
||||
if service == nil {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user