Skip to content

Commit

Permalink
[feat] general notify push (#4)
Browse files Browse the repository at this point in the history
* [feat] add notify push impl

* [feat] unit test
  • Loading branch information
ronething authored Jul 2, 2022
1 parent 75824e7 commit d860373
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
48 changes: 48 additions & 0 deletions notify.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
package msgpush

import "fmt"

// NotifyPush 通用接口
type NotifyPush interface {
Send(string) error
String() string
}

type NotifyPushImpl struct {
Notifys []NotifyPush
}

func NewNotifyPushImpl() *NotifyPushImpl {
return &NotifyPushImpl{Notifys: make([]NotifyPush, 0)}
}

func GetNotify(software, token string) (NotifyPush, error) {
switch software {
case "dingtalk": // 钉钉推送
return NewDingTalk(token), nil
case "wecom": // 企业微信推送
return NewWeCom(token), nil
case "slack": // slack 推送
return NewSlack(token), nil
case "pushdeer":
return NewPushDeer(token), nil
case "feishu":
return NewFeiShu(token), nil
default:
return nil, fmt.Errorf("暂时不支持类型 %v", software)
}
}

func (n *NotifyPushImpl) AddNotifyPush(software, token string) error {
push, err := GetNotify(software, token)
if err != nil {
return err
}

n.Notifys = append(n.Notifys, push)
return nil
}

func (n *NotifyPushImpl) Push(content string) {
if n == nil {
return
}
for i := 0; i < len(n.Notifys); i++ {
if err := n.Notifys[i].Send(content); err != nil {
continue
}
}
}
10 changes: 10 additions & 0 deletions notify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package msgpush

import "testing"

func TestNotifyPushImpl_Push(t *testing.T) {
np := NewNotifyPushImpl()
token := "slack token"
t.Log(np.AddNotifyPush("slack", token))
np.Push("msgpush test")
}

0 comments on commit d860373

Please sign in to comment.