1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-01-24 03:16:35 +02:00
notify/service/dingding/dingding.go
Range Liu 5f4cb781ed
feat(service): Add DingTalk (#183)
Co-authored-by: Niko Köser <koeserniko@gmail.com>
2022-02-07 09:07:37 +01:00

47 lines
932 B
Go

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
}