forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdingding.go
More file actions
46 lines (39 loc) · 932 Bytes
/
Copy pathdingding.go
File metadata and controls
46 lines (39 loc) · 932 Bytes
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
package dingding
import (
"context"
"github.com/blinkbean/dingtalk"
"github.com/pkg/errors"
)
// 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 errors.Wrapf(err, "failed to send message")
}
}
return nil
}