forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dingding.go
47 lines (39 loc) · 907 Bytes
/
dingding.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
package dingding
import (
"context"
"fmt"
"github.com/blinkbean/dingtalk"
)
// Service encapsulates the DingTalk client.
type Service struct {
config Config
client *dingtalk.DingTalk
}
// Config is the Service configuration.
type Config struct {
Token string
Secret string
}
// New returns a new instance of a DingTalk notification service.
func New(cfg *Config) *Service {
dt := dingtalk.InitDingTalkWithSecret(cfg.Token, cfg.Secret)
s := Service{
config: *cfg,
client: dt,
}
return &s
}
// Send takes a message subject and a message content and sends them to all previously set users.
func (s *Service) Send(ctx context.Context, subject, content string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
text := subject + "\n" + content
err := s.client.SendTextMessage(text)
if err != nil {
return fmt.Errorf("send message: %w", err)
}
}
return nil
}