From 925a18001c645dbee0f8f356db0cdef4fdb20fce Mon Sep 17 00:00:00 2001 From: Gokhan Date: Tue, 30 Apr 2024 13:12:58 +0300 Subject: [PATCH] Enable setting ThreadId for Telegram notifications (#3638) * Enable setting ThreadId for telegram notifications Signed-off-by: Gokhan Sari --- config/notifiers.go | 1 + config/notifiers_test.go | 9 ++++++++- docs/configuration.md | 3 +++ notify/telegram/telegram.go | 1 + notify/telegram/telegram_test.go | 2 ++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/config/notifiers.go b/config/notifiers.go index e6d4c9912d..b1f9970cc0 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -778,6 +778,7 @@ type TelegramConfig struct { BotToken Secret `yaml:"bot_token,omitempty" json:"token,omitempty"` BotTokenFile string `yaml:"bot_token_file,omitempty" json:"token_file,omitempty"` ChatID int64 `yaml:"chat_id,omitempty" json:"chat,omitempty"` + MessageThreadID int `yaml:"message_thread_id,omitempty" json:"message_thread_id,omitempty"` Message string `yaml:"message,omitempty" json:"message,omitempty"` DisableNotifications bool `yaml:"disable_notifications,omitempty" json:"disable_notifications,omitempty"` ParseMode string `yaml:"parse_mode,omitempty" json:"parse_mode,omitempty"` diff --git a/config/notifiers_test.go b/config/notifiers_test.go index de1bb5f96b..af348b9ff5 100644 --- a/config/notifiers_test.go +++ b/config/notifiers_test.go @@ -21,7 +21,6 @@ import ( "testing" "github.com/stretchr/testify/require" - "gopkg.in/yaml.v2" ) @@ -1046,6 +1045,14 @@ bot_token_file: '' in: ` bot_token: xyz chat_id: 123 +`, + }, + { + name: "with bot_token, chat_id and message_thread_id set - it succeeds", + in: ` +bot_token: xyz +chat_id: 123 +message_thread_id: 456 `, }, { diff --git a/docs/configuration.md b/docs/configuration.md index 6d62c26098..3c452184d0 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1371,6 +1371,9 @@ attributes: # ID of the chat where to send the messages. [ chat_id: ] +# Optional ID of the message thread where to send the messages. +[ message_thread_id: ] + # Message template. [ message: default = '{{ template "telegram.default.message" .}}' ] diff --git a/notify/telegram/telegram.go b/notify/telegram/telegram.go index 3e60ab7641..fd61714211 100644 --- a/notify/telegram/telegram.go +++ b/notify/telegram/telegram.go @@ -93,6 +93,7 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err message, err := n.client.Send(telebot.ChatID(n.conf.ChatID), messageText, &telebot.SendOptions{ DisableNotification: n.conf.DisableNotifications, DisableWebPagePreview: true, + ThreadID: n.conf.MessageThreadID, }) if err != nil { return true, err diff --git a/notify/telegram/telegram_test.go b/notify/telegram/telegram_test.go index c48034ca70..8be29e38ef 100644 --- a/notify/telegram/telegram_test.go +++ b/notify/telegram/telegram_test.go @@ -46,6 +46,7 @@ receivers: telegram_configs: - chat_id: 1234 bot_token: secret + message_thread_id: 1357 ` var c config.Config err := yaml.Unmarshal([]byte(in), &c) @@ -57,6 +58,7 @@ receivers: require.Equal(t, "https://api.telegram.org", c.Receivers[0].TelegramConfigs[0].APIUrl.String()) require.Equal(t, config.Secret("secret"), c.Receivers[0].TelegramConfigs[0].BotToken) require.Equal(t, int64(1234), c.Receivers[0].TelegramConfigs[0].ChatID) + require.Equal(t, 1357, c.Receivers[0].TelegramConfigs[0].MessageThreadID) require.Equal(t, "HTML", c.Receivers[0].TelegramConfigs[0].ParseMode) }