mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-21 19:27:30 +08:00
feat: support push notification clients
This commit is contained in:
70
pkg/notifier/clients.go
Normal file
70
pkg/notifier/clients.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/nikoksr/notify/service/dingding"
|
||||
po "github.com/nikoksr/notify/service/pushover"
|
||||
"github.com/nikoksr/notify/service/telegram"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type PushoverConfig struct {
|
||||
UserKey string `json:"user_key"`
|
||||
GroupKey string `json:"group_key"`
|
||||
AppToken string `json:"app_token"`
|
||||
}
|
||||
|
||||
func NewPushoverClient(s string) (NotificationClient, error) {
|
||||
var cfg PushoverConfig
|
||||
if err := json.Unmarshal([]byte(s), &cfg); err != nil {
|
||||
return nil, errors.Wrap(err, "json")
|
||||
}
|
||||
|
||||
c := po.New(cfg.AppToken)
|
||||
if cfg.UserKey != "" {
|
||||
c.AddReceivers(cfg.UserKey)
|
||||
}
|
||||
if cfg.GroupKey != "" {
|
||||
c.AddReceivers(cfg.GroupKey)
|
||||
}
|
||||
|
||||
return &Notifier{service: c}, nil
|
||||
}
|
||||
|
||||
type DingTalkConfig struct {
|
||||
Token string `json:"token"`
|
||||
Secret string `json:"secret"`
|
||||
}
|
||||
|
||||
func NewDingTalkClient(s string) (NotificationClient, error) {
|
||||
var cfg DingTalkConfig
|
||||
if err := json.Unmarshal([]byte(s), &cfg); err != nil {
|
||||
return nil, errors.Wrap(err, "json")
|
||||
}
|
||||
|
||||
svc := dingding.New(&dingding.Config{
|
||||
Token: cfg.Token,
|
||||
Secret: cfg.Secret,
|
||||
})
|
||||
return &Notifier{service: svc}, nil
|
||||
}
|
||||
|
||||
type TelegramConfig struct {
|
||||
Token string `json:"token"`
|
||||
ChatID int64 `json:"chat_id"`
|
||||
}
|
||||
|
||||
func NewTelegramClient(s string) (NotificationClient, error) {
|
||||
var cfg TelegramConfig
|
||||
if err := json.Unmarshal([]byte(s), &cfg); err != nil {
|
||||
return nil, errors.Wrap(err, "json")
|
||||
}
|
||||
|
||||
svc, err := telegram.New(cfg.Token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
svc.AddReceivers(cfg.ChatID)
|
||||
return &Notifier{service: svc}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user