Skip to content

Commit

Permalink
Enable setting ThreadId for Telegram notifications (prometheus#3638)
Browse files Browse the repository at this point in the history
* Enable setting ThreadId for telegram notifications

Signed-off-by: Gokhan Sari <gokhan@sari.m

---------
Signed-off-by: gotjosh <josue.abreu@gmail.com>
  • Loading branch information
th0th authored and TheMeier committed May 3, 2024
1 parent 076a626 commit 925a180
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
9 changes: 8 additions & 1 deletion config/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"testing"

"github.com/stretchr/testify/require"

"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -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
`,
},
{
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,9 @@ attributes:
# ID of the chat where to send the messages.
[ chat_id: <int> ]

# Optional ID of the message thread where to send the messages.
[ message_thread_id: <int> ]

# Message template.
[ message: <tmpl_string> default = '{{ template "telegram.default.message" .}}' ]

Expand Down
1 change: 1 addition & 0 deletions notify/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions notify/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down

0 comments on commit 925a180

Please sign in to comment.