feat: add pushme message
This commit is contained in:
@@ -59,6 +59,10 @@ type InsTelegramConfig struct {
|
|||||||
type InsBarkConfig struct {
|
type InsBarkConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InsPushMeConfig 实例里面的PushMe config
|
||||||
|
type InsPushMeConfig struct {
|
||||||
|
}
|
||||||
|
|
||||||
// ManyAddTaskIns 批量添加实例
|
// ManyAddTaskIns 批量添加实例
|
||||||
func ManyAddTaskIns(taskIns []SendTasksIns) error {
|
func ManyAddTaskIns(taskIns []SendTasksIns) error {
|
||||||
tx := db.Begin()
|
tx := db.Begin()
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ const (
|
|||||||
MessageTypeAliyunSMS = "AliyunSMS"
|
MessageTypeAliyunSMS = "AliyunSMS"
|
||||||
MessageTypeTelegram = "Telegram"
|
MessageTypeTelegram = "Telegram"
|
||||||
MessageTypeBark = "Bark"
|
MessageTypeBark = "Bark"
|
||||||
|
MessageTypePushMe = "PushMe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 限制goroutine的最大数量
|
// 限制goroutine的最大数量
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package message
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PushMe struct {
|
||||||
|
PushKey string
|
||||||
|
URL string
|
||||||
|
Date string
|
||||||
|
Type string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PushMe) Request(title, content string) (string, error) {
|
||||||
|
apiURL := p.URL
|
||||||
|
if apiURL == "" {
|
||||||
|
apiURL = "https://push.i-i.me/"
|
||||||
|
}
|
||||||
|
|
||||||
|
data := url.Values{}
|
||||||
|
data.Set("push_key", p.PushKey)
|
||||||
|
data.Set("title", title)
|
||||||
|
data.Set("content", content)
|
||||||
|
if p.Date != "" {
|
||||||
|
data.Set("date", p.Date)
|
||||||
|
}
|
||||||
|
if p.Type != "" {
|
||||||
|
data.Set("type", p.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Post(apiURL, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == 200 && string(body) == "success" {
|
||||||
|
return string(body), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(body), fmt.Errorf("PushMe response error: %s", string(body))
|
||||||
|
}
|
||||||
@@ -131,6 +131,10 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
|
|||||||
var Config models.InsBarkConfig
|
var Config models.InsBarkConfig
|
||||||
return "", Config
|
return "", Config
|
||||||
}
|
}
|
||||||
|
if ins.WayType == constant.MessageTypePushMe {
|
||||||
|
var Config models.InsPushMeConfig
|
||||||
|
return "", Config
|
||||||
|
}
|
||||||
return "未知的渠道的config校验", empty
|
return "未知的渠道的config校验", empty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ const (
|
|||||||
MessageTypeAliyunSMS = channels.MessageTypeAliyunSMS
|
MessageTypeAliyunSMS = channels.MessageTypeAliyunSMS
|
||||||
MessageTypeTelegram = channels.MessageTypeTelegram
|
MessageTypeTelegram = channels.MessageTypeTelegram
|
||||||
MessageTypeBark = channels.MessageTypeBark
|
MessageTypeBark = channels.MessageTypeBark
|
||||||
|
MessageTypePushMe = channels.MessageTypePushMe
|
||||||
)
|
)
|
||||||
|
|
||||||
// ChannelRegistry 渠道注册表
|
// ChannelRegistry 渠道注册表
|
||||||
@@ -103,6 +104,7 @@ func GetGlobalChannelRegistry() *ChannelRegistry {
|
|||||||
globalChannelRegistry.Register(channels.NewAliyunSMSChannel())
|
globalChannelRegistry.Register(channels.NewAliyunSMSChannel())
|
||||||
globalChannelRegistry.Register(channels.NewTelegramChannel())
|
globalChannelRegistry.Register(channels.NewTelegramChannel())
|
||||||
globalChannelRegistry.Register(channels.NewBarkChannel())
|
globalChannelRegistry.Register(channels.NewBarkChannel())
|
||||||
|
globalChannelRegistry.Register(channels.NewPushMeChannel())
|
||||||
})
|
})
|
||||||
return globalChannelRegistry
|
return globalChannelRegistry
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package channels
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"message-nest/models"
|
||||||
|
"message-nest/pkg/message"
|
||||||
|
"message-nest/service/send_ins_service"
|
||||||
|
"message-nest/service/send_way_service"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PushMeChannel struct{ *BaseChannel }
|
||||||
|
|
||||||
|
func NewPushMeChannel() *PushMeChannel {
|
||||||
|
return &PushMeChannel{BaseChannel: NewBaseChannel(MessageTypePushMe, []string{FormatTypeText})}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PushMeChannel) SendUnified(msgObj interface{}, ins models.SendTasksIns, content *UnifiedMessageContent) (string, string) {
|
||||||
|
auth, ok := msgObj.(*send_way_service.WayDetailPushMe)
|
||||||
|
if !ok {
|
||||||
|
return "", "类型转换失败"
|
||||||
|
}
|
||||||
|
insService := send_ins_service.SendTaskInsService{}
|
||||||
|
errStr, configInterface := insService.ValidateDiffIns(ins)
|
||||||
|
if errStr != "" {
|
||||||
|
return errStr, ""
|
||||||
|
}
|
||||||
|
_, ok = configInterface.(models.InsPushMeConfig)
|
||||||
|
if !ok {
|
||||||
|
return "PushMe config校验失败", ""
|
||||||
|
}
|
||||||
|
|
||||||
|
cli := message.PushMe{
|
||||||
|
PushKey: auth.PushKey,
|
||||||
|
URL: auth.URL,
|
||||||
|
Date: auth.Date,
|
||||||
|
Type: auth.Type,
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := cli.Request(content.Title, content.Text)
|
||||||
|
if err != nil {
|
||||||
|
return string(res), fmt.Sprintf("发送失败:%s", err.Error())
|
||||||
|
}
|
||||||
|
return string(res), ""
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ const (
|
|||||||
MessageTypeAliyunSMS = constant.MessageTypeAliyunSMS
|
MessageTypeAliyunSMS = constant.MessageTypeAliyunSMS
|
||||||
MessageTypeTelegram = constant.MessageTypeTelegram
|
MessageTypeTelegram = constant.MessageTypeTelegram
|
||||||
MessageTypeBark = constant.MessageTypeBark
|
MessageTypeBark = constant.MessageTypeBark
|
||||||
|
MessageTypePushMe = constant.MessageTypePushMe
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnifiedMessageContent 统一的消息内容结构
|
// UnifiedMessageContent 统一的消息内容结构
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ var (
|
|||||||
constant.MessageTypeAliyunSMS: func() WayValidator { return &WayDetailAliyunSMS{} },
|
constant.MessageTypeAliyunSMS: func() WayValidator { return &WayDetailAliyunSMS{} },
|
||||||
constant.MessageTypeTelegram: func() WayValidator { return &WayDetailTelegram{} },
|
constant.MessageTypeTelegram: func() WayValidator { return &WayDetailTelegram{} },
|
||||||
constant.MessageTypeBark: func() WayValidator { return &WayDetailBark{} },
|
constant.MessageTypeBark: func() WayValidator { return &WayDetailBark{} },
|
||||||
|
constant.MessageTypePushMe: func() WayValidator { return &WayDetailPushMe{} },
|
||||||
}
|
}
|
||||||
testerRegistry = map[string]func(interface{}) WayTester{
|
testerRegistry = map[string]func(interface{}) WayTester{
|
||||||
constant.MessageTypeEmail: func(m interface{}) WayTester { return m.(*WayDetailEmail) },
|
constant.MessageTypeEmail: func(m interface{}) WayTester { return m.(*WayDetailEmail) },
|
||||||
@@ -59,6 +60,7 @@ var (
|
|||||||
constant.MessageTypeAliyunSMS: func(m interface{}) WayTester { return m.(*WayDetailAliyunSMS) },
|
constant.MessageTypeAliyunSMS: func(m interface{}) WayTester { return m.(*WayDetailAliyunSMS) },
|
||||||
constant.MessageTypeTelegram: func(m interface{}) WayTester { return m.(*WayDetailTelegram) },
|
constant.MessageTypeTelegram: func(m interface{}) WayTester { return m.(*WayDetailTelegram) },
|
||||||
constant.MessageTypeBark: func(m interface{}) WayTester { return m.(*WayDetailBark) },
|
constant.MessageTypeBark: func(m interface{}) WayTester { return m.(*WayDetailBark) },
|
||||||
|
constant.MessageTypePushMe: func(m interface{}) WayTester { return m.(*WayDetailPushMe) },
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -332,6 +334,39 @@ func (w *WayDetailBark) Test() (string, string) {
|
|||||||
return "", string(res)
|
return "", string(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WayDetailPushMe PushMe渠道明细字段
|
||||||
|
type WayDetailPushMe struct {
|
||||||
|
PushKey string `json:"push_key" validate:"required,max=100" label:"PushMe Push Key"`
|
||||||
|
URL string `json:"url" validate:"max=200" label:"自定义API地址"`
|
||||||
|
Date string `json:"date" validate:"max=50" label:"日期"`
|
||||||
|
Type string `json:"type" validate:"max=50" label:"类型"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WayDetailPushMe) Validate(authJson string) (string, interface{}) {
|
||||||
|
var empty interface{}
|
||||||
|
err := json.Unmarshal([]byte(authJson), w)
|
||||||
|
if err != nil {
|
||||||
|
return "PushMe参数反序列化失败!", empty
|
||||||
|
}
|
||||||
|
_, msg := app.CommonPlaygroundValid(*w)
|
||||||
|
return msg, w
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WayDetailPushMe) Test() (string, string) {
|
||||||
|
testMsg := "This is a test message from message-nest."
|
||||||
|
var cli = message.PushMe{
|
||||||
|
PushKey: w.PushKey,
|
||||||
|
URL: w.URL,
|
||||||
|
Date: w.Date,
|
||||||
|
Type: w.Type,
|
||||||
|
}
|
||||||
|
res, err := cli.Request("Test Message", testMsg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("发送失败:%s", err), res
|
||||||
|
}
|
||||||
|
return "", res
|
||||||
|
}
|
||||||
|
|
||||||
func (sw *SendWay) GetByID() (interface{}, error) {
|
func (sw *SendWay) GetByID() (interface{}, error) {
|
||||||
return models.GetWayByID(sw.ID)
|
return models.GetWayByID(sw.ID)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -228,6 +228,26 @@ const CONSTANT = {
|
|||||||
taskInsInputs: [
|
taskInsInputs: [
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: 'PushMe',
|
||||||
|
label: 'PushMe推送',
|
||||||
|
inputs: [
|
||||||
|
{ subLabel: 'Push Key', value: '', col: 'push_key', desc: "PushMe的push_key" },
|
||||||
|
{ subLabel: '自定义API地址', value: '', col: 'url', desc: "默认: https://push.i-i.me/,可选" },
|
||||||
|
{ subLabel: '日期', value: '', col: 'date', desc: "日期,可选" },
|
||||||
|
{ subLabel: '类型', value: '', col: 'type', desc: "类型,可选" },
|
||||||
|
{ subLabel: '渠道名', value: '', col: 'name', desc: "想要设置的渠道名字" },
|
||||||
|
],
|
||||||
|
tips: {
|
||||||
|
text: "PushMe推送说明",
|
||||||
|
desc: "PushMe 是一款推送服务。<br />1. 获取你的 Push Key<br />2. 如果有自建服务,填写自定义 API 地址"
|
||||||
|
},
|
||||||
|
taskInsRadios: [
|
||||||
|
{ subLabel: 'text', content: 'text' },
|
||||||
|
],
|
||||||
|
taskInsInputs: [
|
||||||
|
],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
API_VIEW_DATA: [
|
API_VIEW_DATA: [
|
||||||
{ label: "curl", class: "language-shell line-numbers", code: "", func: ApiStrGenerate.getCurlString },
|
{ label: "curl", class: "language-shell line-numbers", code: "", func: ApiStrGenerate.getCurlString },
|
||||||
|
|||||||
Reference in New Issue
Block a user