Skip to content

Commit d860373

Browse files
authored
[feat] general notify push (#4)
* [feat] add notify push impl * [feat] unit test
1 parent 75824e7 commit d860373

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

notify.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
package msgpush
22

3+
import "fmt"
4+
35
// NotifyPush 通用接口
46
type NotifyPush interface {
57
Send(string) error
68
String() string
79
}
10+
11+
type NotifyPushImpl struct {
12+
Notifys []NotifyPush
13+
}
14+
15+
func NewNotifyPushImpl() *NotifyPushImpl {
16+
return &NotifyPushImpl{Notifys: make([]NotifyPush, 0)}
17+
}
18+
19+
func GetNotify(software, token string) (NotifyPush, error) {
20+
switch software {
21+
case "dingtalk": // 钉钉推送
22+
return NewDingTalk(token), nil
23+
case "wecom": // 企业微信推送
24+
return NewWeCom(token), nil
25+
case "slack": // slack 推送
26+
return NewSlack(token), nil
27+
case "pushdeer":
28+
return NewPushDeer(token), nil
29+
case "feishu":
30+
return NewFeiShu(token), nil
31+
default:
32+
return nil, fmt.Errorf("暂时不支持类型 %v", software)
33+
}
34+
}
35+
36+
func (n *NotifyPushImpl) AddNotifyPush(software, token string) error {
37+
push, err := GetNotify(software, token)
38+
if err != nil {
39+
return err
40+
}
41+
42+
n.Notifys = append(n.Notifys, push)
43+
return nil
44+
}
45+
46+
func (n *NotifyPushImpl) Push(content string) {
47+
if n == nil {
48+
return
49+
}
50+
for i := 0; i < len(n.Notifys); i++ {
51+
if err := n.Notifys[i].Send(content); err != nil {
52+
continue
53+
}
54+
}
55+
}

notify_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package msgpush
2+
3+
import "testing"
4+
5+
func TestNotifyPushImpl_Push(t *testing.T) {
6+
np := NewNotifyPushImpl()
7+
token := "slack token"
8+
t.Log(np.AddNotifyPush("slack", token))
9+
np.Push("msgpush test")
10+
}

0 commit comments

Comments
 (0)