From c1209ccee5d21f401b4a06d693b696ddebd31be7 Mon Sep 17 00:00:00 2001 From: engigu Date: Wed, 4 Feb 2026 19:30:34 +0800 Subject: [PATCH] feat: add pushme message --- models/send_ins.go | 4 ++ pkg/constant/constant.go | 1 + pkg/message/pushme.go | 51 +++++++++++++++++++ service/send_ins_service/send_ins.go | 4 ++ .../unified/channel_registry.go | 2 + .../unified/channels/pushme.go | 44 ++++++++++++++++ .../unified/channels/types.go | 1 + service/send_way_service/send_way.go | 35 +++++++++++++ web/src/constant.js | 20 ++++++++ 9 files changed, 162 insertions(+) create mode 100644 pkg/message/pushme.go create mode 100644 service/send_message_service/unified/channels/pushme.go diff --git a/models/send_ins.go b/models/send_ins.go index ffd3018..9a0f223 100644 --- a/models/send_ins.go +++ b/models/send_ins.go @@ -59,6 +59,10 @@ type InsTelegramConfig struct { type InsBarkConfig struct { } +// InsPushMeConfig 实例里面的PushMe config +type InsPushMeConfig struct { +} + // ManyAddTaskIns 批量添加实例 func ManyAddTaskIns(taskIns []SendTasksIns) error { tx := db.Begin() diff --git a/pkg/constant/constant.go b/pkg/constant/constant.go index 3b16443..5555566 100644 --- a/pkg/constant/constant.go +++ b/pkg/constant/constant.go @@ -66,6 +66,7 @@ const ( MessageTypeAliyunSMS = "AliyunSMS" MessageTypeTelegram = "Telegram" MessageTypeBark = "Bark" + MessageTypePushMe = "PushMe" ) // 限制goroutine的最大数量 diff --git a/pkg/message/pushme.go b/pkg/message/pushme.go new file mode 100644 index 0000000..680742f --- /dev/null +++ b/pkg/message/pushme.go @@ -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)) +} diff --git a/service/send_ins_service/send_ins.go b/service/send_ins_service/send_ins.go index 442e20f..5c94079 100644 --- a/service/send_ins_service/send_ins.go +++ b/service/send_ins_service/send_ins.go @@ -131,6 +131,10 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string, var Config models.InsBarkConfig return "", Config } + if ins.WayType == constant.MessageTypePushMe { + var Config models.InsPushMeConfig + return "", Config + } return "未知的渠道的config校验", empty } diff --git a/service/send_message_service/unified/channel_registry.go b/service/send_message_service/unified/channel_registry.go index 8973934..5395111 100644 --- a/service/send_message_service/unified/channel_registry.go +++ b/service/send_message_service/unified/channel_registry.go @@ -28,6 +28,7 @@ const ( MessageTypeAliyunSMS = channels.MessageTypeAliyunSMS MessageTypeTelegram = channels.MessageTypeTelegram MessageTypeBark = channels.MessageTypeBark + MessageTypePushMe = channels.MessageTypePushMe ) // ChannelRegistry 渠道注册表 @@ -103,6 +104,7 @@ func GetGlobalChannelRegistry() *ChannelRegistry { globalChannelRegistry.Register(channels.NewAliyunSMSChannel()) globalChannelRegistry.Register(channels.NewTelegramChannel()) globalChannelRegistry.Register(channels.NewBarkChannel()) + globalChannelRegistry.Register(channels.NewPushMeChannel()) }) return globalChannelRegistry } diff --git a/service/send_message_service/unified/channels/pushme.go b/service/send_message_service/unified/channels/pushme.go new file mode 100644 index 0000000..ea628ed --- /dev/null +++ b/service/send_message_service/unified/channels/pushme.go @@ -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), "" +} diff --git a/service/send_message_service/unified/channels/types.go b/service/send_message_service/unified/channels/types.go index 83d65c9..d8d037a 100644 --- a/service/send_message_service/unified/channels/types.go +++ b/service/send_message_service/unified/channels/types.go @@ -20,6 +20,7 @@ const ( MessageTypeAliyunSMS = constant.MessageTypeAliyunSMS MessageTypeTelegram = constant.MessageTypeTelegram MessageTypeBark = constant.MessageTypeBark + MessageTypePushMe = constant.MessageTypePushMe ) // UnifiedMessageContent 统一的消息内容结构 diff --git a/service/send_way_service/send_way.go b/service/send_way_service/send_way.go index a603155..c5207b1 100644 --- a/service/send_way_service/send_way.go +++ b/service/send_way_service/send_way.go @@ -47,6 +47,7 @@ var ( constant.MessageTypeAliyunSMS: func() WayValidator { return &WayDetailAliyunSMS{} }, constant.MessageTypeTelegram: func() WayValidator { return &WayDetailTelegram{} }, constant.MessageTypeBark: func() WayValidator { return &WayDetailBark{} }, + constant.MessageTypePushMe: func() WayValidator { return &WayDetailPushMe{} }, } testerRegistry = map[string]func(interface{}) WayTester{ constant.MessageTypeEmail: func(m interface{}) WayTester { return m.(*WayDetailEmail) }, @@ -59,6 +60,7 @@ var ( 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) }, + constant.MessageTypePushMe: func(m interface{}) WayTester { return m.(*WayDetailPushMe) }, } ) @@ -332,6 +334,39 @@ func (w *WayDetailBark) Test() (string, string) { 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) { return models.GetWayByID(sw.ID) } diff --git a/web/src/constant.js b/web/src/constant.js index c8bed96..52c5937 100644 --- a/web/src/constant.js +++ b/web/src/constant.js @@ -228,6 +228,26 @@ const CONSTANT = { 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 是一款推送服务。
1. 获取你的 Push Key
2. 如果有自建服务,填写自定义 API 地址" + }, + taskInsRadios: [ + { subLabel: 'text', content: 'text' }, + ], + taskInsInputs: [ + ], + }, ], API_VIEW_DATA: [ { label: "curl", class: "language-shell line-numbers", code: "", func: ApiStrGenerate.getCurlString },