-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_manager.go
103 lines (86 loc) · 2.36 KB
/
webhook_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package webhook
import (
"fmt"
"github.com/LampardNguyen234/whale-alert/logger"
"github.com/LampardNguyen234/whale-alert/webhook/discord"
"github.com/LampardNguyen234/whale-alert/webhook/telegram"
)
// WebHookManager manages the list of WebHooks and is the main entrance to posting messages.
type WebHookManager struct {
webHooks map[string]WebHook
}
// NewWebHookManager returns a new WebHookManager.
func NewWebHookManager(whs map[string]WebHook) *WebHookManager {
if whs == nil {
whs = make(map[string]WebHook)
}
return &WebHookManager{webHooks: whs}
}
// NewWebHookManagerFromConfig returns a new WebHookManager from the given config.
func NewWebHookManagerFromConfig(cfg WebHookConfig, log logger.Logger) (*WebHookManager, error) {
whs := make(map[string]WebHook)
var err error
if cfg.Telegram != nil {
whs["Telegram"], err = telegram.NewTeleHook(*cfg.Telegram, log)
if err != nil {
fmt.Println("tele", err)
return nil, err
}
}
if cfg.Discord != nil {
whs["Discord"], err = discord.NewDiscordHook(*cfg.Discord, log)
if err != nil {
fmt.Println("discord", err)
return nil, err
}
}
return &WebHookManager{webHooks: whs}, nil
}
// AddWebHook adds a new WebHook to the WebHookManager.
func (m *WebHookManager) AddWebHook(name string, wh WebHook) {
m.webHooks[name] = wh
}
// RemoveWebHook removes a WebHook given its name.
func (m *WebHookManager) RemoveWebHook(name string) {
if m.webHooks[name] != nil {
delete(m.webHooks, name)
}
}
// Start starts all the WebHook of the WebHookManager.
func (m *WebHookManager) Start() error {
var err error
for _, wh := range m.webHooks {
err = wh.Start()
if err != nil {
return err
}
}
return nil
}
// Stop terminates all the processes of the WebHookManager.
func (m *WebHookManager) Stop() {
for _, wh := range m.webHooks {
go wh.Stop()
}
}
// Info pushes the given information message to the WebHook's.
func (m *WebHookManager) Info(msg interface{}) error {
for _, wh := range m.webHooks {
_ = wh.Info(msg)
}
return nil
}
// Error pushes the given error message to the WebHook's.
func (m *WebHookManager) Error(msg interface{}) error {
for _, wh := range m.webHooks {
_ = wh.Error(msg)
}
return nil
}
// Alert pushes the given alert message to the WebHook's.
func (m *WebHookManager) Alert(msg interface{}) error {
for _, wh := range m.webHooks {
_ = wh.Alert(msg)
}
return nil
}