This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
choose message template format HTML/MarkDown #80
Open
kaminek
wants to merge
1
commit into
metalmatze:master
Choose a base branch
from
kaminek:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ type Bot struct { | |
logger log.Logger | ||
revision string | ||
startTime time.Time | ||
parseMode telebot.ParseMode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we set this still to |
||
|
||
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) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 bothParseHTML
andParseMarkdown
inpkg/telegram
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used
telebot.ParseMode
becausetelebot.SendMessage()
depends on it, we can do what you said above, but still, I need to cast the param intelebot.SendMessage()
totelebot.ParseMode
to be able to compile cf:What do you think? casting casting still holds the dependancy anyway.