Skip to content

Commit

Permalink
hotfix(sender/telegram): remove trigger description from message in t…
Browse files Browse the repository at this point in the history
…elegram sender (#1065)
  • Loading branch information
AleksandrMatsko authored Jul 31, 2024
1 parent c15c44d commit 381cab9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
31 changes: 3 additions & 28 deletions senders/telegram/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package telegram
import (
"errors"
"fmt"
"regexp"
"strings"
"time"

"github.com/russross/blackfriday/v2"

"github.com/moira-alert/moira/senders/msgformat"

"github.com/mitchellh/mapstructure"
Expand All @@ -26,14 +23,6 @@ const (
)

var (
// startHeaderRegexp is used for removing start html header tag from description (which is converted from markdown to html).
// Because of not supporting it in telegram.
startHeaderRegexp = regexp.MustCompile("<h[0-9]+>")

// endHeaderRegexp is used for removing end html header tag from description (which is converted from markdown to html).
// Because of not supporting it in telegram.
endHeaderRegexp = regexp.MustCompile("</h[0-9]+>")

codeBlockStart = "<blockquote expandable>"
codeBlockEnd = "</blockquote>"
)
Expand Down Expand Up @@ -94,7 +83,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
cfg.FrontURI,
location,
urlFormatter,
descriptionFormatter,
emptyDescriptionFormatter,
boldFormatter,
eventStringFormatter,
codeBlockStart,
Expand Down Expand Up @@ -150,22 +139,8 @@ func urlFormatter(triggerURI, triggerName string) string {
return fmt.Sprintf("<a href=\"%s\">%s</a>", triggerURI, triggerName)
}

func descriptionFormatter(trigger moira.TriggerData) string {
desc := trigger.Desc
if trigger.Desc != "" {
desc = trigger.Desc
desc += "\n"
}

htmlDescStr := string(blackfriday.Run([]byte(desc)))

// html headers are not supported by telegram html, so make them bold instead.
htmlDescStr = startHeaderRegexp.ReplaceAllString(htmlDescStr, "<b>")
withReplacedHeaders := endHeaderRegexp.ReplaceAllString(htmlDescStr, "</b>")

// html paragraphs are not supported by telegram html, so delete them.
replacer := strings.NewReplacer("<p>", "", "</p>", "")
return replacer.Replace(withReplacedHeaders)
func emptyDescriptionFormatter(trigger moira.TriggerData) string {
return ""
}

func boldFormatter(str string) string {
Expand Down
6 changes: 4 additions & 2 deletions senders/telegram/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var characterLimits = map[messageType]int{
Album: albumCaptionMaxCharacters,
}

const telegramParseMode = telebot.ModeHTML

var unmarshalTypeError *json.UnmarshalTypeError

// Chat is a structure that represents chat metadata required to send message to recipient.
Expand Down Expand Up @@ -212,7 +214,7 @@ func (sender *Sender) talk(chat *Chat, message string, plots [][]byte, messageTy
func (sender *Sender) sendAsMessage(chat *Chat, message string) error {
_, err := sender.bot.Send(chat, message, &telebot.SendOptions{
ThreadID: chat.ThreadID,
ParseMode: telebot.ModeHTML,
ParseMode: telegramParseMode,
DisableWebPagePreview: true,
})
if err != nil {
Expand Down Expand Up @@ -277,7 +279,7 @@ func (sender *Sender) sendAsAlbum(chat *Chat, plots [][]byte, caption string) er

_, err := sender.bot.SendAlbum(chat, album, &telebot.SendOptions{
ThreadID: chat.ThreadID,
ParseMode: telebot.ModeHTML,
ParseMode: telegramParseMode,
DisableWebPagePreview: true,
})
if err != nil {
Expand Down

0 comments on commit 381cab9

Please sign in to comment.