Skip to content

Commit c1209cc

Browse files
committed
feat: add pushme message
1 parent 0c80b9c commit c1209cc

9 files changed

Lines changed: 162 additions & 0 deletions

File tree

models/send_ins.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type InsTelegramConfig struct {
5959
type InsBarkConfig struct {
6060
}
6161

62+
// InsPushMeConfig 实例里面的PushMe config
63+
type InsPushMeConfig struct {
64+
}
65+
6266
// ManyAddTaskIns 批量添加实例
6367
func ManyAddTaskIns(taskIns []SendTasksIns) error {
6468
tx := db.Begin()

pkg/constant/constant.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
MessageTypeAliyunSMS = "AliyunSMS"
6767
MessageTypeTelegram = "Telegram"
6868
MessageTypeBark = "Bark"
69+
MessageTypePushMe = "PushMe"
6970
)
7071

7172
// 限制goroutine的最大数量

pkg/message/pushme.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package message
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"net/url"
8+
"strings"
9+
)
10+
11+
type PushMe struct {
12+
PushKey string
13+
URL string
14+
Date string
15+
Type string
16+
}
17+
18+
func (p *PushMe) Request(title, content string) (string, error) {
19+
apiURL := p.URL
20+
if apiURL == "" {
21+
apiURL = "https://push.i-i.me/"
22+
}
23+
24+
data := url.Values{}
25+
data.Set("push_key", p.PushKey)
26+
data.Set("title", title)
27+
data.Set("content", content)
28+
if p.Date != "" {
29+
data.Set("date", p.Date)
30+
}
31+
if p.Type != "" {
32+
data.Set("type", p.Type)
33+
}
34+
35+
resp, err := http.Post(apiURL, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
36+
if err != nil {
37+
return "", err
38+
}
39+
defer resp.Body.Close()
40+
41+
body, err := io.ReadAll(resp.Body)
42+
if err != nil {
43+
return "", err
44+
}
45+
46+
if resp.StatusCode == 200 && string(body) == "success" {
47+
return string(body), nil
48+
}
49+
50+
return string(body), fmt.Errorf("PushMe response error: %s", string(body))
51+
}

service/send_ins_service/send_ins.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
131131
var Config models.InsBarkConfig
132132
return "", Config
133133
}
134+
if ins.WayType == constant.MessageTypePushMe {
135+
var Config models.InsPushMeConfig
136+
return "", Config
137+
}
134138
return "未知的渠道的config校验", empty
135139
}
136140

service/send_message_service/unified/channel_registry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
MessageTypeAliyunSMS = channels.MessageTypeAliyunSMS
2929
MessageTypeTelegram = channels.MessageTypeTelegram
3030
MessageTypeBark = channels.MessageTypeBark
31+
MessageTypePushMe = channels.MessageTypePushMe
3132
)
3233

3334
// ChannelRegistry 渠道注册表
@@ -103,6 +104,7 @@ func GetGlobalChannelRegistry() *ChannelRegistry {
103104
globalChannelRegistry.Register(channels.NewAliyunSMSChannel())
104105
globalChannelRegistry.Register(channels.NewTelegramChannel())
105106
globalChannelRegistry.Register(channels.NewBarkChannel())
107+
globalChannelRegistry.Register(channels.NewPushMeChannel())
106108
})
107109
return globalChannelRegistry
108110
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package channels
2+
3+
import (
4+
"fmt"
5+
"message-nest/models"
6+
"message-nest/pkg/message"
7+
"message-nest/service/send_ins_service"
8+
"message-nest/service/send_way_service"
9+
)
10+
11+
type PushMeChannel struct{ *BaseChannel }
12+
13+
func NewPushMeChannel() *PushMeChannel {
14+
return &PushMeChannel{BaseChannel: NewBaseChannel(MessageTypePushMe, []string{FormatTypeText})}
15+
}
16+
17+
func (c *PushMeChannel) SendUnified(msgObj interface{}, ins models.SendTasksIns, content *UnifiedMessageContent) (string, string) {
18+
auth, ok := msgObj.(*send_way_service.WayDetailPushMe)
19+
if !ok {
20+
return "", "类型转换失败"
21+
}
22+
insService := send_ins_service.SendTaskInsService{}
23+
errStr, configInterface := insService.ValidateDiffIns(ins)
24+
if errStr != "" {
25+
return errStr, ""
26+
}
27+
_, ok = configInterface.(models.InsPushMeConfig)
28+
if !ok {
29+
return "PushMe config校验失败", ""
30+
}
31+
32+
cli := message.PushMe{
33+
PushKey: auth.PushKey,
34+
URL: auth.URL,
35+
Date: auth.Date,
36+
Type: auth.Type,
37+
}
38+
39+
res, err := cli.Request(content.Title, content.Text)
40+
if err != nil {
41+
return string(res), fmt.Sprintf("发送失败:%s", err.Error())
42+
}
43+
return string(res), ""
44+
}

service/send_message_service/unified/channels/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
MessageTypeAliyunSMS = constant.MessageTypeAliyunSMS
2121
MessageTypeTelegram = constant.MessageTypeTelegram
2222
MessageTypeBark = constant.MessageTypeBark
23+
MessageTypePushMe = constant.MessageTypePushMe
2324
)
2425

2526
// UnifiedMessageContent 统一的消息内容结构

service/send_way_service/send_way.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var (
4747
constant.MessageTypeAliyunSMS: func() WayValidator { return &WayDetailAliyunSMS{} },
4848
constant.MessageTypeTelegram: func() WayValidator { return &WayDetailTelegram{} },
4949
constant.MessageTypeBark: func() WayValidator { return &WayDetailBark{} },
50+
constant.MessageTypePushMe: func() WayValidator { return &WayDetailPushMe{} },
5051
}
5152
testerRegistry = map[string]func(interface{}) WayTester{
5253
constant.MessageTypeEmail: func(m interface{}) WayTester { return m.(*WayDetailEmail) },
@@ -59,6 +60,7 @@ var (
5960
constant.MessageTypeAliyunSMS: func(m interface{}) WayTester { return m.(*WayDetailAliyunSMS) },
6061
constant.MessageTypeTelegram: func(m interface{}) WayTester { return m.(*WayDetailTelegram) },
6162
constant.MessageTypeBark: func(m interface{}) WayTester { return m.(*WayDetailBark) },
63+
constant.MessageTypePushMe: func(m interface{}) WayTester { return m.(*WayDetailPushMe) },
6264
}
6365
)
6466

@@ -332,6 +334,39 @@ func (w *WayDetailBark) Test() (string, string) {
332334
return "", string(res)
333335
}
334336

337+
// WayDetailPushMe PushMe渠道明细字段
338+
type WayDetailPushMe struct {
339+
PushKey string `json:"push_key" validate:"required,max=100" label:"PushMe Push Key"`
340+
URL string `json:"url" validate:"max=200" label:"自定义API地址"`
341+
Date string `json:"date" validate:"max=50" label:"日期"`
342+
Type string `json:"type" validate:"max=50" label:"类型"`
343+
}
344+
345+
func (w *WayDetailPushMe) Validate(authJson string) (string, interface{}) {
346+
var empty interface{}
347+
err := json.Unmarshal([]byte(authJson), w)
348+
if err != nil {
349+
return "PushMe参数反序列化失败!", empty
350+
}
351+
_, msg := app.CommonPlaygroundValid(*w)
352+
return msg, w
353+
}
354+
355+
func (w *WayDetailPushMe) Test() (string, string) {
356+
testMsg := "This is a test message from message-nest."
357+
var cli = message.PushMe{
358+
PushKey: w.PushKey,
359+
URL: w.URL,
360+
Date: w.Date,
361+
Type: w.Type,
362+
}
363+
res, err := cli.Request("Test Message", testMsg)
364+
if err != nil {
365+
return fmt.Sprintf("发送失败:%s", err), res
366+
}
367+
return "", res
368+
}
369+
335370
func (sw *SendWay) GetByID() (interface{}, error) {
336371
return models.GetWayByID(sw.ID)
337372
}

web/src/constant.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,26 @@ const CONSTANT = {
228228
taskInsInputs: [
229229
],
230230
},
231+
{
232+
type: 'PushMe',
233+
label: 'PushMe推送',
234+
inputs: [
235+
{ subLabel: 'Push Key', value: '', col: 'push_key', desc: "PushMe的push_key" },
236+
{ subLabel: '自定义API地址', value: '', col: 'url', desc: "默认: https://push.i-i.me/,可选" },
237+
{ subLabel: '日期', value: '', col: 'date', desc: "日期,可选" },
238+
{ subLabel: '类型', value: '', col: 'type', desc: "类型,可选" },
239+
{ subLabel: '渠道名', value: '', col: 'name', desc: "想要设置的渠道名字" },
240+
],
241+
tips: {
242+
text: "PushMe推送说明",
243+
desc: "PushMe 是一款推送服务。<br />1. 获取你的 Push Key<br />2. 如果有自建服务,填写自定义 API 地址"
244+
},
245+
taskInsRadios: [
246+
{ subLabel: 'text', content: 'text' },
247+
],
248+
taskInsInputs: [
249+
],
250+
},
231251
],
232252
API_VIEW_DATA: [
233253
{ label: "curl", class: "language-shell line-numbers", code: "", func: ApiStrGenerate.getCurlString },

0 commit comments

Comments
 (0)