-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [feat] add notify push impl * [feat] unit test
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |