Skip to content

Commit

Permalink
Add MD support for slack and telegram senders (moira-alert#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
aswinmprabhu committed May 15, 2019
1 parent 0e3d04c commit b50ea7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
11 changes: 9 additions & 2 deletions senders/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,22 @@ func (sender *Sender) buildMessage(events moira.NotificationEvents, trigger moir
message.WriteString(trigger.Name)
}

messageCharsCount := len([]rune(message.String()))

if trigger.Desc != "" {
message.WriteString("\n")
message.WriteString(trigger.Desc)
charsAvailableForDesc := messageMaxCharacters - messageCharsCount - 9000 - 1
if charsAvailableForDesc < len(trigger.Desc) {
message.WriteString(trigger.Desc[0 : charsAvailableForDesc-1])
} else {
message.WriteString(trigger.Desc)
}
messageCharsCount = len([]rune(message.String()))
}

message.WriteString("\n```")

var printEventsCount int
messageCharsCount := len([]rune(message.String()))
messageLimitReached := false

for _, event := range events {
Expand Down
18 changes: 15 additions & 3 deletions senders/telegram/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@ func (sender *Sender) buildMessage(events moira.NotificationEvents, trigger moir
buffer.WriteString(title)

var messageCharsCount, printEventsCount int
messageCharsCount += len([]rune(title))
messageCharsCount = len([]rune(buffer.String()))

if trigger.Desc != "" {
charsAvailableForDesc := messageMaxCharacters - messageCharsCount - 900
if charsAvailableForDesc < len(trigger.Desc) {
buffer.WriteString(trigger.Desc[0 : charsAvailableForDesc-1])
} else {
buffer.WriteString(trigger.Desc)
}
buffer.WriteString("\n")
messageCharsCount = len([]rune(buffer.String()))
}

messageLimitReached := false

for _, event := range events {
Expand Down Expand Up @@ -107,7 +119,7 @@ func (sender *Sender) talk(chat *telebot.Chat, message string, plot []byte, mess
}

func (sender *Sender) sendAsMessage(chat *telebot.Chat, message string) error {
_, err := sender.bot.Send(chat, message)
_, err := sender.bot.Send(chat, message, []interface{}{telebot.ModeMarkdown})
if err != nil {
return fmt.Errorf("can't send event message [%s] to %v: %s", message, chat.ID, err.Error())
}
Expand All @@ -116,7 +128,7 @@ func (sender *Sender) sendAsMessage(chat *telebot.Chat, message string) error {

func (sender *Sender) sendAsPhoto(chat *telebot.Chat, plot []byte, caption string) error {
photo := telebot.Photo{File: telebot.FromReader(bytes.NewReader(plot)), Caption: caption}
_, err := photo.Send(sender.bot, chat, &telebot.SendOptions{})
_, err := photo.Send(sender.bot, chat, &telebot.SendOptions{ParseMode: telebot.ModeMarkdown})
if err != nil {
return fmt.Errorf("can't send event plot to %v: %s", chat.ID, err.Error())
}
Expand Down

0 comments on commit b50ea7e

Please sign in to comment.