mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(notifications): add telegram support (#299)
* feat(notifications): add telegram support * feat(notifications): change list view * refactor(notifications): overall setup * feat(notifications): forms add telegram
This commit is contained in:
parent
2ab7133dd0
commit
38addb99e6
15 changed files with 630 additions and 457 deletions
|
@ -2,13 +2,9 @@ package notification
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
|
||||
"github.com/containrrr/shoutrrr"
|
||||
t "github.com/containrrr/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
|
@ -17,20 +13,26 @@ type Service interface {
|
|||
Store(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
||||
Update(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
||||
Delete(ctx context.Context, id int) error
|
||||
Send(event domain.NotificationEvent, msg string) error
|
||||
SendEvent(event domain.EventsReleasePushed) error
|
||||
Send(event domain.NotificationEvent, payload domain.NotificationPayload) error
|
||||
Test(ctx context.Context, notification domain.Notification) error
|
||||
}
|
||||
|
||||
type service struct {
|
||||
log logger.Logger
|
||||
repo domain.NotificationRepo
|
||||
log logger.Logger
|
||||
repo domain.NotificationRepo
|
||||
senders []domain.NotificationSender
|
||||
}
|
||||
|
||||
func NewService(log logger.Logger, repo domain.NotificationRepo) Service {
|
||||
return &service{
|
||||
log: log,
|
||||
repo: repo,
|
||||
s := &service{
|
||||
log: log,
|
||||
repo: repo,
|
||||
senders: []domain.NotificationSender{},
|
||||
}
|
||||
|
||||
s.registerSenders()
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *service) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
|
||||
|
@ -42,109 +44,96 @@ func (s *service) FindByID(ctx context.Context, id int) (*domain.Notification, e
|
|||
}
|
||||
|
||||
func (s *service) Store(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
|
||||
return s.repo.Store(ctx, n)
|
||||
_, err := s.repo.Store(ctx, n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// reset senders
|
||||
s.senders = []domain.NotificationSender{}
|
||||
|
||||
// re register senders
|
||||
s.registerSenders()
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *service) Update(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
|
||||
return s.repo.Update(ctx, n)
|
||||
_, err := s.repo.Update(ctx, n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// reset senders
|
||||
s.senders = []domain.NotificationSender{}
|
||||
|
||||
// re register senders
|
||||
s.registerSenders()
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *service) Delete(ctx context.Context, id int) error {
|
||||
return s.repo.Delete(ctx, id)
|
||||
err := s.repo.Delete(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// reset senders
|
||||
s.senders = []domain.NotificationSender{}
|
||||
|
||||
// re register senders
|
||||
s.registerSenders()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) registerSenders() {
|
||||
senders, err := s.repo.List(context.Background())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, n := range senders {
|
||||
if n.Enabled {
|
||||
switch n.Type {
|
||||
case domain.NotificationTypeDiscord:
|
||||
s.senders = append(s.senders, NewDiscordSender(s.log, n))
|
||||
case domain.NotificationTypeTelegram:
|
||||
s.senders = append(s.senders, NewTelegramSender(s.log, n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Send notifications
|
||||
func (s *service) Send(event domain.NotificationEvent, msg string) error {
|
||||
// find notifications for type X
|
||||
func (s *service) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
s.log.Debug().Msgf("sending notification for %v", string(event))
|
||||
|
||||
notifications, err := s.repo.List(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var urls []string
|
||||
|
||||
for _, n := range notifications {
|
||||
if !n.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
switch n.Type {
|
||||
case domain.NotificationTypeDiscord:
|
||||
urls = append(urls, fmt.Sprintf("discord://%v@%v", n.Token, n.Webhook))
|
||||
default:
|
||||
return nil
|
||||
for _, sender := range s.senders {
|
||||
// check if sender is active and have notification types
|
||||
if sender.CanSend(event) {
|
||||
sender.Send(event, payload)
|
||||
}
|
||||
}
|
||||
|
||||
if len(urls) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
sender, err := shoutrrr.CreateSender(urls...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p := t.Params{"title": "TEST"}
|
||||
items := []t.MessageItem{
|
||||
{
|
||||
Text: "text hello",
|
||||
Fields: []t.Field{
|
||||
{
|
||||
Key: "eventt",
|
||||
Value: "push?",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
//items = append(items, t.MessageItem{
|
||||
// Text: "text hello",
|
||||
// Fields: []t.Field{
|
||||
// {
|
||||
// Key: "eventt",
|
||||
// Value: "push?",
|
||||
// },
|
||||
// },
|
||||
//})
|
||||
|
||||
sender.SendItems(items, p)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) SendEvent(event domain.EventsReleasePushed) error {
|
||||
notifications, err := s.repo.List(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
func (s *service) Test(ctx context.Context, notification domain.Notification) error {
|
||||
var agent domain.NotificationSender
|
||||
|
||||
switch notification.Type {
|
||||
case domain.NotificationTypeDiscord:
|
||||
agent = NewDiscordSender(s.log, notification)
|
||||
case domain.NotificationTypeTelegram:
|
||||
agent = NewTelegramSender(s.log, notification)
|
||||
}
|
||||
|
||||
return s.send(notifications, event)
|
||||
}
|
||||
|
||||
func (s *service) send(notifications []domain.Notification, event domain.EventsReleasePushed) error {
|
||||
// find notifications for type X
|
||||
for _, n := range notifications {
|
||||
if !n.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
if n.Events == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, evt := range n.Events {
|
||||
if evt == string(event.Status) {
|
||||
switch n.Type {
|
||||
case domain.NotificationTypeDiscord:
|
||||
go s.discordNotification(event, n.Webhook)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
return agent.Send(domain.NotificationEventTest, domain.NotificationPayload{
|
||||
Subject: "Test Notification",
|
||||
Message: "autobrr goes brr!!",
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue