mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat: external notification
This commit is contained in:
parent
241fa4adfe
commit
04de1360c2
42 changed files with 3292 additions and 1393 deletions
33
internal/notification/bark.go
Normal file
33
internal/notification/bark.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/bark"
|
||||
"github.com/uozi-tech/cosy/map2struct"
|
||||
)
|
||||
|
||||
// @external_notifier(Bark)
|
||||
type Bark struct {
|
||||
DeviceKey string `json:"device_key" title:"Device Key"`
|
||||
ServerURL string `json:"server_url" title:"Server URL"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterExternalNotifier("bark", func(ctx context.Context, n *model.ExternalNotify, msg *ExternalMessage) error {
|
||||
barkConfig := &Bark{}
|
||||
err := map2struct.WeakDecode(n.Config, barkConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if barkConfig.DeviceKey == "" && barkConfig.ServerURL == "" {
|
||||
return ErrInvalidNotifierConfig
|
||||
}
|
||||
barkService := bark.NewWithServers(barkConfig.DeviceKey, barkConfig.ServerURL)
|
||||
externalNotify := notify.New()
|
||||
externalNotify.UseServices(barkService)
|
||||
return externalNotify.Send(ctx, msg.GetTitle(n.Language), msg.GetContent(n.Language))
|
||||
})
|
||||
}
|
39
internal/notification/dingding.go
Normal file
39
internal/notification/dingding.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/dingding"
|
||||
"github.com/uozi-tech/cosy/map2struct"
|
||||
)
|
||||
|
||||
// @external_notifier(DingTalk)
|
||||
type DingTalk struct {
|
||||
AccessToken string `json:"access_token" title:"Access Token"`
|
||||
Secret string `json:"secret" title:"Secret (Optional)"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterExternalNotifier("dingding", func(ctx context.Context, n *model.ExternalNotify, msg *ExternalMessage) error {
|
||||
dingTalkConfig := &DingTalk{}
|
||||
err := map2struct.WeakDecode(n.Config, dingTalkConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dingTalkConfig.AccessToken == "" {
|
||||
return ErrInvalidNotifierConfig
|
||||
}
|
||||
|
||||
// Initialize DingTalk service
|
||||
dingTalkService := dingding.New(&dingding.Config{
|
||||
Token: dingTalkConfig.AccessToken,
|
||||
Secret: dingTalkConfig.Secret,
|
||||
})
|
||||
// Use the service
|
||||
externalNotify := notify.New()
|
||||
externalNotify.UseServices(dingTalkService)
|
||||
return externalNotify.Send(ctx, msg.GetTitle(n.Language), msg.GetContent(n.Language))
|
||||
})
|
||||
}
|
9
internal/notification/errors.go
Normal file
9
internal/notification/errors.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package notification
|
||||
|
||||
import "github.com/uozi-tech/cosy"
|
||||
|
||||
var (
|
||||
e = cosy.NewErrorScope("notification")
|
||||
ErrNotifierNotFound = e.New(404001, "notifier not found")
|
||||
ErrInvalidNotifierConfig = e.New(400001, "invalid notifier config")
|
||||
)
|
100
internal/notification/external.go
Normal file
100
internal/notification/external.go
Normal file
|
@ -0,0 +1,100 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/translation"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/query"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
externalNotifierRegistry = make(map[string]ExternalNotifierHandlerFunc)
|
||||
externalNotifierRegistryMutex = &sync.RWMutex{}
|
||||
)
|
||||
|
||||
type ExternalNotifierHandlerFunc func(ctx context.Context, n *model.ExternalNotify, msg *ExternalMessage) error
|
||||
|
||||
func externalNotifierHandler(n *model.ExternalNotify, msg *model.Notification) (ExternalNotifierHandlerFunc, error) {
|
||||
externalNotifierRegistryMutex.RLock()
|
||||
defer externalNotifierRegistryMutex.RUnlock()
|
||||
notifier, ok := externalNotifierRegistry[n.Type]
|
||||
if !ok {
|
||||
return nil, ErrNotifierNotFound
|
||||
}
|
||||
return notifier, nil
|
||||
}
|
||||
|
||||
func RegisterExternalNotifier(name string, handler ExternalNotifierHandlerFunc) {
|
||||
externalNotifierRegistryMutex.Lock()
|
||||
defer externalNotifierRegistryMutex.Unlock()
|
||||
externalNotifierRegistry[name] = handler
|
||||
}
|
||||
|
||||
type ExternalMessage struct {
|
||||
Notification *model.Notification
|
||||
}
|
||||
|
||||
func (n *ExternalMessage) Send() {
|
||||
en := query.ExternalNotify
|
||||
externalNotifies, err := en.Find()
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
ctx := context.Background()
|
||||
for _, externalNotify := range externalNotifies {
|
||||
go func(externalNotify *model.ExternalNotify) {
|
||||
notifier, err := externalNotifierHandler(externalNotify, n.Notification)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
notifier(ctx, externalNotify, n)
|
||||
}(externalNotify)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *ExternalMessage) GetTitle(lang string) string {
|
||||
if n.Notification == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
dict, ok := translation.Dict[lang]
|
||||
if !ok {
|
||||
dict = translation.Dict["en"]
|
||||
}
|
||||
|
||||
title, err := dict.Translate(n.Notification.Title)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return n.Notification.Title
|
||||
}
|
||||
|
||||
return title
|
||||
}
|
||||
|
||||
func (n *ExternalMessage) GetContent(lang string) string {
|
||||
if n.Notification == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if n.Notification.Details == nil {
|
||||
return n.Notification.Content
|
||||
}
|
||||
|
||||
dict, ok := translation.Dict[lang]
|
||||
if !ok {
|
||||
dict = translation.Dict["en"]
|
||||
}
|
||||
|
||||
content, err := dict.Translate(n.Notification.Content, n.Notification.Details)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return n.Notification.Content
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
28
internal/notification/push.go
Normal file
28
internal/notification/push.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/query"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
func push(nType model.NotificationType, title string, content string, details any) {
|
||||
n := query.Notification
|
||||
|
||||
data := &model.Notification{
|
||||
Type: nType,
|
||||
Title: title,
|
||||
Content: content,
|
||||
Details: details,
|
||||
}
|
||||
|
||||
err := n.Create(data)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
broadcast(data)
|
||||
|
||||
extNotify := &ExternalMessage{data}
|
||||
extNotify.Send()
|
||||
}
|
|
@ -4,9 +4,7 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -34,21 +32,3 @@ func broadcast(data *model.Notification) {
|
|||
evtChan <- data
|
||||
}
|
||||
}
|
||||
|
||||
func push(nType model.NotificationType, title string, content string, details any) {
|
||||
n := query.Notification
|
||||
|
||||
data := &model.Notification{
|
||||
Type: nType,
|
||||
Title: title,
|
||||
Content: content,
|
||||
Details: details,
|
||||
}
|
||||
|
||||
err := n.Create(data)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
broadcast(data)
|
||||
}
|
||||
|
|
54
internal/notification/telegram.go
Normal file
54
internal/notification/telegram.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/telegram"
|
||||
"github.com/uozi-tech/cosy/map2struct"
|
||||
)
|
||||
|
||||
// @external_notifier(Telegram)
|
||||
type Telegram struct {
|
||||
BotToken string `json:"bot_token" title:"Bot Token"`
|
||||
ChatID string `json:"chat_id" title:"Chat ID"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterExternalNotifier("telegram", func(ctx context.Context, n *model.ExternalNotify, msg *ExternalMessage) error {
|
||||
telegramConfig := &Telegram{}
|
||||
err := map2struct.WeakDecode(n.Config, telegramConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if telegramConfig.BotToken == "" || telegramConfig.ChatID == "" {
|
||||
return ErrInvalidNotifierConfig
|
||||
}
|
||||
|
||||
telegramService, err := telegram.New(telegramConfig.BotToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ChatID must be an integer for telegram service
|
||||
chatIDInt, err := strconv.ParseInt(telegramConfig.ChatID, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid Telegram Chat ID '%s': %w", telegramConfig.ChatID, err)
|
||||
}
|
||||
|
||||
// Check if chatIDInt is 0, which might indicate an empty or invalid input was parsed
|
||||
if chatIDInt == 0 {
|
||||
return errors.New("invalid Telegram Chat ID: cannot be zero")
|
||||
}
|
||||
|
||||
telegramService.AddReceivers(chatIDInt)
|
||||
|
||||
externalNotify := notify.New()
|
||||
externalNotify.UseServices(telegramService)
|
||||
return externalNotify.Send(ctx, msg.GetTitle(n.Language), msg.GetContent(n.Language))
|
||||
})
|
||||
}
|
|
@ -4,7 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/app"
|
||||
"github.com/0xJacky/pofile/pofile"
|
||||
"github.com/0xJacky/pofile"
|
||||
"github.com/samber/lo"
|
||||
"io"
|
||||
"log"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue