diff --git a/cmd/alertmanager-bot/main.go b/cmd/alertmanager-bot/main.go index c0110de..48c2d2b 100644 --- a/cmd/alertmanager-bot/main.go +++ b/cmd/alertmanager-bot/main.go @@ -25,6 +25,7 @@ import ( "github.com/prometheus/alertmanager/template" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/tucnak/telebot" "gopkg.in/alecthomas/kingpin.v2" ) @@ -65,6 +66,7 @@ func main() { telegramAdmins []int telegramToken string templatesPaths []string + parseMode string }{} a := kingpin.New("alertmanager-bot", "Bot for Prometheus' Alertmanager") @@ -117,6 +119,11 @@ func main() { Default("/templates/default.tmpl"). ExistingFilesVar(&config.templatesPaths) + a.Flag("parse.mode", "The template formatting mode HTML/Markdown"). + Envar("TEMPLATE_FORMAT"). + Default(string(telebot.ModeHTML)). + EnumVar(&config.parseMode, string(telebot.ModeHTML), string(telebot.ModeMarkdown)) + _, err := a.Parse(os.Args[1:]) if err != nil { fmt.Printf("error parsing commandline arguments: %v\n", err) @@ -208,6 +215,7 @@ func main() { telegram.WithRevision(Revision), telegram.WithStartTime(StartTime), telegram.WithExtraAdmins(config.telegramAdmins[1:]...), + telegram.WithParseMode(config.parseMode), ) if err != nil { level.Error(tlogger).Log("msg", "failed to create bot", "err", err) diff --git a/pkg/telegram/bot.go b/pkg/telegram/bot.go index 215c58f..b1ace94 100644 --- a/pkg/telegram/bot.go +++ b/pkg/telegram/bot.go @@ -66,6 +66,7 @@ type Bot struct { logger log.Logger revision string startTime time.Time + parseMode telebot.ParseMode telegram *telebot.Bot @@ -100,6 +101,7 @@ func NewBot(chats BotChatStore, token string, admin int, opts ...BotOption) (*Bo admins: []int{admin}, alertmanager: &url.URL{Host: "localhost:9093"}, commandsCounter: commandsCounter, + parseMode: telebot.ModeDefault, // TODO: initialize templates with default? } @@ -161,6 +163,13 @@ func WithExtraAdmins(ids ...int) BotOption { } } +// WithParseMode sets the alerts parsing mode (HTML/MarkDown) +func WithParseMode(pm string) BotOption { + return func(b *Bot) { + b.parseMode = telebot.ParseMode(pm) + } +} + // SendAdminMessage to the admin's ID with a message func (b *Bot) SendAdminMessage(adminID int, message string) { b.telegram.SendMessage(telebot.User{ID: adminID}, message, nil) @@ -288,14 +297,19 @@ func (b *Bot) sendWebhook(ctx context.Context, webhooks <-chan notify.WebhookMes ExternalURL: w.ExternalURL, } - out, err := b.templates.ExecuteHTMLString(`{{ template "telegram.default" . }}`, data) + out := "" + if b.parseMode == telebot.ModeMarkdown { + out, err = b.templates.ExecuteTextString(`{{ template "telegram.default" . }}`, data) + } else { + out, err = b.templates.ExecuteHTMLString(`{{ template "telegram.default" . }}`, data) + } if err != nil { level.Warn(b.logger).Log("msg", "failed to template alerts", "err", err) continue } for _, chat := range chats { - err = b.telegram.SendMessage(chat, b.truncateMessage(out), &telebot.SendOptions{ParseMode: telebot.ModeHTML}) + err = b.telegram.SendMessage(chat, b.truncateMessage(out), &telebot.SendOptions{ParseMode: b.parseMode}) if err != nil { level.Warn(b.logger).Log("msg", "failed to send message to subscribed chat", "err", err) }