-
Notifications
You must be signed in to change notification settings - Fork 43
/
client.go
69 lines (60 loc) · 1.46 KB
/
client.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
package bot
import (
"github.com/yaklang/yaklang/common/utils"
"os"
)
type Client struct {
config []*Config
delaySeconds float64
cooldown *utils.CoolDown
}
func New(opts ...ConfigOpt) *Client {
client := &Client{
config: nil,
delaySeconds: 1,
}
for _, p := range opts {
p(client)
}
if client.delaySeconds <= 0 {
client.delaySeconds = 1
}
client.cooldown = utils.NewCoolDown(utils.FloatSecondDuration(client.delaySeconds))
return client
}
func FromEnv() *Client {
var opts []ConfigOpt
if os.Getenv("YAKIT_DINGTALK_WEBHOOK") != "" {
opts = append(opts, WithWebhookWithSecret(os.Getenv("YAKIT_DINGTALK_WEBHOOK"), os.Getenv("YAKIT_DINGTALK_SECRET")))
}
if os.Getenv("YAKIT_WORKWX_WEBHOOK") != "" {
opts = append(opts, WithWebhookWithSecret(os.Getenv("YAKIT_WORKWX_WEBHOOK"), os.Getenv("YAKIT_WORKWX_SECRET")))
}
if os.Getenv("YAKIT_FEISHU_WEBHOOK") != "" {
opts = append(opts, WithWebhookWithSecret(os.Getenv("YAKIT_FEISHU_WEBHOOK"), os.Getenv("YAKIT_FEISHU_SECRET")))
}
return New(opts...)
}
func (s *Client) Configs() []*Config {
return s.config
}
func (c *Client) SendText(text string, items ...interface{}) {
if c == nil || len(c.config) <= 0 {
return
}
c.cooldown.Do(func() {
for _, i := range c.config {
i.SendText(text, items...)
}
})
}
func (c *Client) SendMarkdown(text string) {
if c == nil || len(c.config) <= 0 {
return
}
c.cooldown.Do(func() {
for _, i := range c.config {
i.SendMarkdown(text)
}
})
}