Skip to content

Commit

Permalink
Add shoutrrr for supporting emails as push channel (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Aug 3, 2020
1 parent 58177f6 commit 403c71f
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 12 deletions.
2 changes: 2 additions & 0 deletions evcc.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ messaging:
token: # bot id
chats:
- # list of chat ids
- type: email
uri: smtp://<user>:<password>@<host>:<port>/?fromAddress=<from>&toAddresses=<to>

# meters
meters:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/asaskevich/EventBus v0.0.0-20200428142821-4fc0642a29f3
github.com/avast/retry-go v2.6.0+incompatible
github.com/benbjohnson/clock v1.0.3
github.com/containrrr/shoutrrr v0.0.0-20200721140131-bafc331a1968
github.com/eclipse/paho.mqtt.golang v1.2.0
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/godbus/dbus/v5 v5.0.3
Expand Down
32 changes: 32 additions & 0 deletions go.sum

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions push/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion push/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
10 changes: 6 additions & 4 deletions push/pushover.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package push

import (
"errors"

"github.com/gregdel/pushover"
)

Expand All @@ -17,21 +19,21 @@ 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{
app: pushover.New(app),
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 {
Expand Down
46 changes: 46 additions & 0 deletions push/shoutrrr.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
9 changes: 5 additions & 4 deletions push/telegram.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package push

import (
"errors"
"sync"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 403c71f

Please sign in to comment.