feat: add bark message
This commit is contained in:
@@ -127,6 +127,10 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
|
||||
var Config models.InsTelegramConfig
|
||||
return "", Config
|
||||
}
|
||||
if ins.WayType == constant.MessageTypeBark {
|
||||
var Config models.InsBarkConfig
|
||||
return "", Config
|
||||
}
|
||||
return "未知的渠道的config校验", empty
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
|
||||
// 重导出channels包的类型,方便外部使用
|
||||
type (
|
||||
Channel = channels.Channel
|
||||
UnifiedMessageContent = channels.UnifiedMessageContent
|
||||
Channel = channels.Channel
|
||||
UnifiedMessageContent = channels.UnifiedMessageContent
|
||||
)
|
||||
|
||||
// 重导出常量
|
||||
@@ -27,6 +27,7 @@ const (
|
||||
MessageTypeMessageNest = channels.MessageTypeMessageNest
|
||||
MessageTypeAliyunSMS = channels.MessageTypeAliyunSMS
|
||||
MessageTypeTelegram = channels.MessageTypeTelegram
|
||||
MessageTypeBark = channels.MessageTypeBark
|
||||
)
|
||||
|
||||
// ChannelRegistry 渠道注册表
|
||||
@@ -101,6 +102,7 @@ func GetGlobalChannelRegistry() *ChannelRegistry {
|
||||
globalChannelRegistry.Register(channels.NewMessageNestChannel())
|
||||
globalChannelRegistry.Register(channels.NewAliyunSMSChannel())
|
||||
globalChannelRegistry.Register(channels.NewTelegramChannel())
|
||||
globalChannelRegistry.Register(channels.NewBarkChannel())
|
||||
})
|
||||
return globalChannelRegistry
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package channels
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/message"
|
||||
"message-nest/service/send_ins_service"
|
||||
"message-nest/service/send_way_service"
|
||||
)
|
||||
|
||||
type BarkChannel struct{ *BaseChannel }
|
||||
|
||||
func NewBarkChannel() *BarkChannel {
|
||||
return &BarkChannel{BaseChannel: NewBaseChannel(MessageTypeBark, []string{FormatTypeText})}
|
||||
}
|
||||
|
||||
func (c *BarkChannel) SendUnified(msgObj interface{}, ins models.SendTasksIns, content *UnifiedMessageContent) (string, string) {
|
||||
auth, ok := msgObj.(*send_way_service.WayDetailBark)
|
||||
if !ok {
|
||||
return "", "类型转换失败"
|
||||
}
|
||||
insService := send_ins_service.SendTaskInsService{}
|
||||
errStr, configInterface := insService.ValidateDiffIns(ins)
|
||||
if errStr != "" {
|
||||
return errStr, ""
|
||||
}
|
||||
_, ok = configInterface.(models.InsBarkConfig)
|
||||
if !ok {
|
||||
return "Bark config校验失败", ""
|
||||
}
|
||||
|
||||
cli := message.Bark{
|
||||
PushKey: auth.PushKey,
|
||||
Archive: auth.Archive,
|
||||
Group: auth.Group,
|
||||
Sound: auth.Sound,
|
||||
Icon: auth.Icon,
|
||||
Level: auth.Level,
|
||||
URL: auth.URL,
|
||||
}
|
||||
|
||||
res, err := cli.Request(content.Title, content.Text)
|
||||
if err != nil {
|
||||
return string(res), fmt.Sprintf("发送失败:%s", err.Error())
|
||||
}
|
||||
return string(res), ""
|
||||
}
|
||||
@@ -19,6 +19,7 @@ const (
|
||||
MessageTypeMessageNest = constant.MessageTypeMessageNest
|
||||
MessageTypeAliyunSMS = constant.MessageTypeAliyunSMS
|
||||
MessageTypeTelegram = constant.MessageTypeTelegram
|
||||
MessageTypeBark = constant.MessageTypeBark
|
||||
)
|
||||
|
||||
// UnifiedMessageContent 统一的消息内容结构
|
||||
|
||||
@@ -46,6 +46,7 @@ var (
|
||||
constant.MessageTypeMessageNest: func() WayValidator { return &MessageNest{} },
|
||||
constant.MessageTypeAliyunSMS: func() WayValidator { return &WayDetailAliyunSMS{} },
|
||||
constant.MessageTypeTelegram: func() WayValidator { return &WayDetailTelegram{} },
|
||||
constant.MessageTypeBark: func() WayValidator { return &WayDetailBark{} },
|
||||
}
|
||||
testerRegistry = map[string]func(interface{}) WayTester{
|
||||
constant.MessageTypeEmail: func(m interface{}) WayTester { return m.(*WayDetailEmail) },
|
||||
@@ -57,6 +58,7 @@ var (
|
||||
constant.MessageTypeMessageNest: func(m interface{}) WayTester { return m.(*MessageNest) },
|
||||
constant.MessageTypeAliyunSMS: func(m interface{}) WayTester { return m.(*WayDetailAliyunSMS) },
|
||||
constant.MessageTypeTelegram: func(m interface{}) WayTester { return m.(*WayDetailTelegram) },
|
||||
constant.MessageTypeBark: func(m interface{}) WayTester { return m.(*WayDetailBark) },
|
||||
}
|
||||
)
|
||||
|
||||
@@ -286,6 +288,45 @@ func (w *WayDetailTelegram) Test() (string, string) {
|
||||
return "", string(res)
|
||||
}
|
||||
|
||||
// WayDetailBark Bark渠道明细字段
|
||||
type WayDetailBark struct {
|
||||
PushKey string `json:"push_key" validate:"required,max=200" label:"Bark Push Key"`
|
||||
Archive string `json:"archive" validate:"max=10" label:"推送是否存档"`
|
||||
Group string `json:"group" validate:"max=50" label:"推送分组"`
|
||||
Sound string `json:"sound" validate:"max=50" label:"推送声音"`
|
||||
Icon string `json:"icon" validate:"max=200" label:"推送图标"`
|
||||
Level string `json:"level" validate:"max=20" label:"推送时效性"`
|
||||
URL string `json:"url" validate:"max=200" label:"推送跳转URL"`
|
||||
}
|
||||
|
||||
func (w *WayDetailBark) Validate(authJson string) (string, interface{}) {
|
||||
var empty interface{}
|
||||
err := json.Unmarshal([]byte(authJson), w)
|
||||
if err != nil {
|
||||
return "Bark参数反序列化失败!", empty
|
||||
}
|
||||
_, msg := app.CommonPlaygroundValid(*w)
|
||||
return msg, w
|
||||
}
|
||||
|
||||
func (w *WayDetailBark) Test() (string, string) {
|
||||
testMsg := "This is a test message from message-nest."
|
||||
var cli = message.Bark{
|
||||
PushKey: w.PushKey,
|
||||
Archive: w.Archive,
|
||||
Group: w.Group,
|
||||
Sound: w.Sound,
|
||||
Icon: w.Icon,
|
||||
Level: w.Level,
|
||||
URL: w.URL,
|
||||
}
|
||||
res, err := cli.Request("Test Message", testMsg)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("发送失败:%s", err), string(res)
|
||||
}
|
||||
return "", string(res)
|
||||
}
|
||||
|
||||
func (sw *SendWay) GetByID() (interface{}, error) {
|
||||
return models.GetWayByID(sw.ID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user