|
1 | 1 | package msgpush
|
2 | 2 |
|
| 3 | +import "fmt" |
| 4 | + |
3 | 5 | // NotifyPush 通用接口
|
4 | 6 | type NotifyPush interface {
|
5 | 7 | Send(string) error
|
6 | 8 | String() string
|
7 | 9 | }
|
| 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 | +} |
0 commit comments