1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-02-03 13:01:26 +02:00
notify/service/dingding/dingding.go

47 lines
932 B
Go
Raw Normal View History

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
}