Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

choose message template format HTML/MarkDown #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/alertmanager-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -65,6 +66,7 @@ func main() {
telegramAdmins []int
telegramToken string
templatesPaths []string
parseMode string
}{}

a := kingpin.New("alertmanager-bot", "Bot for Prometheus' Alertmanager")
Expand Down Expand Up @@ -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)).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than introducing a direct dependency on telebot in the main.go, could we create both ParseHTML and ParseMarkdown in pkg/telegram.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used telebot.ParseMode because telebot.SendMessage() depends on it, we can do what you said above, but still, I need to cast the param in telebot.SendMessage() to telebot.ParseMode to be able to compile cf:

pkg/telegram/bot.go:320:85: cannot use b.parseMode (type ParseMode) as type telebot.ParseMode in field value
make: *** [build] Error 2

What do you think? casting casting still holds the dependancy anyway.

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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions pkg/telegram/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Bot struct {
logger log.Logger
revision string
startTime time.Time
parseMode telebot.ParseMode
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set this still to telebot.ModeHTML as this is the current default.


telegram *telebot.Bot

Expand Down Expand Up @@ -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?
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down