Skip to content

Commit

Permalink
Exposing more sending methods of alert channel (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanliqun authored Jun 4, 2024
1 parent 856293d commit 7446e94
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 19 deletions.
9 changes: 4 additions & 5 deletions alert/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ type DingTalkConfig struct {

// DingTalkChannel DingTalk notification channel
type DingTalkChannel struct {
Formatter Formatter // message formatter
*dingtalk.Robot
ID string // channel id
Config DingTalkConfig // channel config

bot *dingtalk.Robot
Formatter Formatter // message formatter
}

func NewDingTalkChannel(chID string, fmt Formatter, conf DingTalkConfig) *DingTalkChannel {
return &DingTalkChannel{
ID: chID, Formatter: fmt, Config: conf,
bot: dingtalk.NewRobot(conf.Webhook, conf.Secret),
Robot: dingtalk.NewRobot(conf.Webhook, conf.Secret),
}
}

Expand All @@ -48,5 +47,5 @@ func (dtc *DingTalkChannel) Send(ctx context.Context, note *Notification) error
return errors.WithMessage(err, "failed to format alert msg")
}

return dtc.bot.SendMarkdown(ctx, note.Title, msg, dtc.Config.AtMobiles, dtc.Config.IsAtAll)
return dtc.SendMarkdown(ctx, note.Title, msg, dtc.Config.AtMobiles, dtc.Config.IsAtAll)
}
14 changes: 14 additions & 0 deletions alert/dingtalk/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func NewRobot(webhook, secrect string) *Robot {
return &Robot{webHook: webhook, secret: secrect}
}

// SendText send a text type message.
func (r Robot) SendText(ctx context.Context, content string, atMobiles []string, isAtAll bool) error {
return r.send(ctx, &textMessage{
MsgType: msgTypeText,
Text: textParams{
Content: content,
},
At: atParams{
AtMobiles: atMobiles,
IsAtAll: isAtAll,
},
})
}

// SendMarkdown send a markdown type message.
func (r Robot) SendMarkdown(ctx context.Context, title, text string, atMobiles []string, isAtAll bool) error {
return r.send(ctx, &markdownMessage{
Expand Down
11 changes: 11 additions & 0 deletions alert/dingtalk/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ package dingtalk

const (
msgTypeMarkdown = "markdown"
msgTypeText = "text"
)

type textMessage struct {
MsgType string `json:"msgtype"`
Text textParams `json:"text"`
At atParams `json:"at"`
}

type textParams struct {
Content string `json:"content"`
}

type atParams struct {
AtMobiles []string `json:"atMobiles,omitempty"`
IsAtAll bool `json:"isAtAll,omitempty"`
Expand Down
11 changes: 5 additions & 6 deletions alert/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ type PagerDutyConfig struct {

// PagerDutyChannel represents a PagerDuty notification channel.
type PagerDutyChannel struct {
*pagerduty.Client
ID string // the identifier of the channel
Config PagerDutyConfig // the configuration for the PagerDuty channel

tags []string
client *pagerduty.Client
tags []string // the tags used for the PagerDuty channel
}

// NewPagerDutyChannel creates a new PagerDuty channel with the given ID and configuration
func NewPagerDutyChannel(chID string, tags []string, conf PagerDutyConfig) *PagerDutyChannel {
return &PagerDutyChannel{
ID: chID, Config: conf, tags: tags,
client: pagerduty.NewClient(conf.AuthToken),
Client: pagerduty.NewClient(conf.AuthToken),
}
}

Expand All @@ -49,7 +48,7 @@ func (c *PagerDutyChannel) Type() ChannelType {
return ChannelTypePagerDuty
}

// Send sends a notification using the PagerDuty channel
// Send sends notification using the PagerDuty channel.
func (c *PagerDutyChannel) Send(ctx context.Context, note *Notification) error {
var payload *pagerduty.V2Payload
switch note.Content.(type) {
Expand All @@ -73,7 +72,7 @@ func (c *PagerDutyChannel) Send(ctx context.Context, note *Notification) error {
Payload: payload,
}

_, err := c.client.ManageEventWithContext(ctx, event)
_, err := c.ManageEventWithContext(ctx, event)
return err
}

Expand Down
7 changes: 4 additions & 3 deletions alert/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ func (c *SmtpChannel) Send(ctx context.Context, note *Notification) error {
if err != nil {
return errors.WithMessage(err, "failed to format alert msg")
}

// Send the formatted message
return c.send(ctx, msg)
return c.SendProtoMsg(ctx, msg)
}

// send sends a message using the SMTP channel
func (c *SmtpChannel) send(ctx context.Context, msg string) error {
// SendProtoMsg sends protocol message using the SMTP channel
func (c *SmtpChannel) SendProtoMsg(ctx context.Context, msg string) error {
// Dial the SMTP server
client, err := c.dial(ctx)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions alert/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ type TelegramConfig struct {

// TelegramChannel Telegram notification channel
type TelegramChannel struct {
*bot.Bot
Formatter Formatter // message formatter
ID string // channel id
Config TelegramConfig // channel config

bot *bot.Bot
}

func NewTelegramChannel(chID string, fmt Formatter, conf TelegramConfig) (*TelegramChannel, error) {
Expand All @@ -32,7 +31,7 @@ func NewTelegramChannel(chID string, fmt Formatter, conf TelegramConfig) (*Teleg
return nil, err
}

tc := &TelegramChannel{ID: chID, Formatter: fmt, Config: conf, bot: bot}
tc := &TelegramChannel{ID: chID, Formatter: fmt, Config: conf, Bot: bot}
return tc, nil
}

Expand All @@ -50,7 +49,7 @@ func (tc *TelegramChannel) Send(ctx context.Context, note *Notification) error {
return errors.WithMessage(err, "failed to format alert msg")
}

_, err = tc.bot.SendMessage(ctx, &bot.SendMessageParams{
_, err = tc.SendMessage(ctx, &bot.SendMessageParams{
ChatID: tc.Config.ChatId,
Text: msg,
ParseMode: models.ParseModeMarkdown,
Expand Down
4 changes: 3 additions & 1 deletion alert/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/pkg/errors"
)

var ErrInvalidNotification = errors.New("invalid notification")
var (
ErrInvalidNotification = errors.New("invalid notification")
)

func ErrChannelTypeNotSupported(chType string) error {
return errors.Errorf("channel type %s not supported", chType)
Expand Down

0 comments on commit 7446e94

Please sign in to comment.