Add shoutrrr for supporting emails as push channel (#263)
This commit is contained in:
parent
58177f60c1
commit
403c71fccd
8 changed files with 101 additions and 12 deletions
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
// Sender implements message sending
|
||||
type Sender interface {
|
||||
Send(event Event, title, msg string)
|
||||
Send(title, msg string)
|
||||
}
|
||||
|
||||
// EventTemplate is the push message template for an event
|
||||
|
|
@ -25,12 +25,17 @@ func NewMessengerFromConfig(typ string, other map[string]interface{}) (res Sende
|
|||
case "pushover":
|
||||
var cc pushOverConfig
|
||||
if err = util.DecodeOther(other, &cc); err == nil {
|
||||
res = NewPushOverMessenger(cc.App, cc.Recipients)
|
||||
res, err = NewPushOverMessenger(cc.App, cc.Recipients)
|
||||
}
|
||||
case "telegram":
|
||||
var cc telegramConfig
|
||||
if err = util.DecodeOther(other, &cc); err == nil {
|
||||
res = NewTelegramMessenger(cc.Token, cc.Chats)
|
||||
res, err = NewTelegramMessenger(cc.Token, cc.Chats)
|
||||
}
|
||||
case "email", "shout":
|
||||
var cc shoutrrrConfig
|
||||
if err = util.DecodeOther(other, &cc); err == nil {
|
||||
res, err = NewShoutrrrMessenger(cc.URI)
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("unknown messenger type: %s", typ)
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ func (h *Hub) Run(events <-chan Event) {
|
|||
}
|
||||
|
||||
for _, sender := range h.sender {
|
||||
go sender.Send(ev, definition.Title, msg)
|
||||
go sender.Send(definition.Title, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package push
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gregdel/pushover"
|
||||
)
|
||||
|
||||
|
|
@ -17,9 +19,9 @@ type pushOverConfig struct {
|
|||
}
|
||||
|
||||
// NewPushOverMessenger creates new pushover messenger
|
||||
func NewPushOverMessenger(app string, recipients []string) *PushOver {
|
||||
func NewPushOverMessenger(app string, recipients []string) (*PushOver, error) {
|
||||
if app == "" {
|
||||
log.FATAL.Fatal("pushover: missing app name")
|
||||
return nil, errors.New("pushover: missing app name")
|
||||
}
|
||||
|
||||
m := &PushOver{
|
||||
|
|
@ -27,11 +29,11 @@ func NewPushOverMessenger(app string, recipients []string) *PushOver {
|
|||
recipients: recipients,
|
||||
}
|
||||
|
||||
return m
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Send sends to all receivers
|
||||
func (m *PushOver) Send(event Event, title, msg string) {
|
||||
func (m *PushOver) Send(title, msg string) {
|
||||
message := pushover.NewMessageWithTitle(msg, title)
|
||||
|
||||
for _, id := range m.recipients {
|
||||
|
|
|
|||
46
push/shoutrrr.go
Normal file
46
push/shoutrrr.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package push
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/containrrr/shoutrrr"
|
||||
"github.com/containrrr/shoutrrr/pkg/router"
|
||||
"github.com/containrrr/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// Shoutrrr implements the shoutrrr messenging aggregator
|
||||
type Shoutrrr struct {
|
||||
app *router.ServiceRouter
|
||||
}
|
||||
|
||||
type shoutrrrConfig struct {
|
||||
URI string
|
||||
}
|
||||
|
||||
// NewShoutrrrMessenger creates new Shoutrrr messenger
|
||||
func NewShoutrrrMessenger(uri string) (*Shoutrrr, error) {
|
||||
app, err := shoutrrr.CreateSender(uri)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("shoutrrr: %v", err)
|
||||
}
|
||||
|
||||
m := &Shoutrrr{
|
||||
app: app,
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Send sends to all receivers
|
||||
func (m *Shoutrrr) Send(title, msg string) {
|
||||
params := &types.Params{
|
||||
"title": title,
|
||||
"subject": title,
|
||||
}
|
||||
|
||||
for _, err := range m.app.Send(msg, params) {
|
||||
if err != nil {
|
||||
log.ERROR.Printf("shoutrrr: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package push
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
|
|
@ -25,10 +26,10 @@ func init() {
|
|||
}
|
||||
|
||||
// NewTelegramMessenger creates new pushover messenger
|
||||
func NewTelegramMessenger(token string, chats []int64) *Telegram {
|
||||
func NewTelegramMessenger(token string, chats []int64) (*Telegram, error) {
|
||||
bot, err := tgbotapi.NewBotAPI(token)
|
||||
if err != nil {
|
||||
log.FATAL.Fatal("telegram: invalid bot token")
|
||||
return nil, errors.New("telegram: invalid bot token")
|
||||
}
|
||||
|
||||
m := &Telegram{
|
||||
|
|
@ -42,7 +43,7 @@ func NewTelegramMessenger(token string, chats []int64) *Telegram {
|
|||
|
||||
go m.trackChats()
|
||||
|
||||
return m
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// trackChats captures ids of all chats that bot participates in
|
||||
|
|
@ -66,7 +67,7 @@ func (m *Telegram) trackChats() {
|
|||
}
|
||||
|
||||
// Send sends to all receivers
|
||||
func (m *Telegram) Send(event Event, title, msg string) {
|
||||
func (m *Telegram) Send(title, msg string) {
|
||||
m.Lock()
|
||||
for chat := range m.chats {
|
||||
log.TRACE.Printf("telegram: sending to %d", chat)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue