From 7446e9424c0286ccc9bba62de265306e9ffa6629 Mon Sep 17 00:00:00 2001 From: Liqun Date: Tue, 4 Jun 2024 11:11:04 +0800 Subject: [PATCH] Exposing more sending methods of alert channel (#47) --- alert/dingtalk.go | 9 ++++----- alert/dingtalk/bot.go | 14 ++++++++++++++ alert/dingtalk/message.go | 11 +++++++++++ alert/pagerduty.go | 11 +++++------ alert/smtp.go | 7 ++++--- alert/telegram.go | 7 +++---- alert/util.go | 4 +++- 7 files changed, 44 insertions(+), 19 deletions(-) diff --git a/alert/dingtalk.go b/alert/dingtalk.go index e60570a..21af959 100644 --- a/alert/dingtalk.go +++ b/alert/dingtalk.go @@ -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), } } @@ -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) } diff --git a/alert/dingtalk/bot.go b/alert/dingtalk/bot.go index 022b576..ae0adf9 100644 --- a/alert/dingtalk/bot.go +++ b/alert/dingtalk/bot.go @@ -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{ diff --git a/alert/dingtalk/message.go b/alert/dingtalk/message.go index ae063ed..622160c 100644 --- a/alert/dingtalk/message.go +++ b/alert/dingtalk/message.go @@ -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"` diff --git a/alert/pagerduty.go b/alert/pagerduty.go index 953678a..0ad0a9d 100644 --- a/alert/pagerduty.go +++ b/alert/pagerduty.go @@ -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), } } @@ -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) { @@ -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 } diff --git a/alert/smtp.go b/alert/smtp.go index d7f0726..9f5c513 100644 --- a/alert/smtp.go +++ b/alert/smtp.go @@ -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 { diff --git a/alert/telegram.go b/alert/telegram.go index 4eb0532..3da2892 100644 --- a/alert/telegram.go +++ b/alert/telegram.go @@ -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) { @@ -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 } @@ -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, diff --git a/alert/util.go b/alert/util.go index 5ed0ce1..2d983bc 100644 --- a/alert/util.go +++ b/alert/util.go @@ -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)