forked from projectdiscovery/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
76 lines (68 loc) · 1.83 KB
/
notify.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
package notify
import (
"github.com/acarl005/stripansi"
"github.com/projectdiscovery/retryablehttp-go"
)
// Notify handles the notification engine
type Notify struct {
options *Options
client *retryablehttp.Client
slackClient *SlackClient
discordClient *DiscordClient
telegramClient *TelegramClient
}
// New notify instance
func New() (*Notify, error) {
retryhttp := retryablehttp.NewClient(retryablehttp.DefaultOptionsSingle)
return &Notify{client: retryhttp}, nil
}
// NewWithOptions create a new instance of notify with options
func NewWithOptions(options *Options) (*Notify, error) {
notifier, err := New()
if err != nil {
return nil, err
}
SlackClient := &SlackClient{
client: notifier.client,
WebHookURL: options.SlackWebHookURL,
UserName: options.SlackUsername,
Channel: options.SlackUsername,
TimeOut: DefaultSlackTimeout,
}
discordClient := &DiscordClient{
client: notifier.client,
WebHookURL: options.DiscordWebHookURL,
UserName: options.DiscordWebHookUsername,
Avatar: options.DiscordWebHookAvatarURL,
}
telegramClient := &TelegramClient{
client: notifier.client,
apiKEY: options.TelegramAPIKey,
chatID: options.TelegramChatID,
}
return &Notify{options: options, slackClient: SlackClient, discordClient: discordClient, telegramClient: telegramClient}, nil
}
// SendNotification to registered webhooks
func (n *Notify) SendNotification(message string) error {
// strip unsupported color control chars
message = stripansi.Strip(message)
if n.options.Slack {
err := n.slackClient.SendInfo(message)
if err != nil {
return err
}
}
if n.options.Discord {
err := n.discordClient.SendInfo(message)
if err != nil {
return err
}
}
if n.options.Telegram {
err := n.telegramClient.SendInfo(message)
if err != nil {
return err
}
}
return nil
}