Skip to content

Commit

Permalink
Adding telegram support (#2827)
Browse files Browse the repository at this point in the history
* added telegram config in notifiers

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added telegram config to config.go

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added package telegram to notify and initialization in cmd

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added telegram.default.message

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added telegram template to notifiers.go

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added message rendering from template to telegram integration

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* documentation for telegram_configs

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added tests for telegram

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* fixed config_test.go

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added valid parse_mode list to the error log

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* changed supported values for telegram config

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added mock url for telegram tests to pass

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* added api_url check to telegram config

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* changed bot_token type to secret

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* removed extra emptyline

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* convert bot_token to string in telegram client creation

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* updated configuration.md

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* fixed mixed up errors

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>

* check telegram api url without conversion to string

Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
  • Loading branch information
timmilesdw authored Feb 22, 2022
1 parent d4573e2 commit 1138a08
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 9 deletions.
4 changes: 2 additions & 2 deletions asset/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"github.com/prometheus/alertmanager/notify/pushover"
"github.com/prometheus/alertmanager/notify/slack"
"github.com/prometheus/alertmanager/notify/sns"
"github.com/prometheus/alertmanager/notify/telegram"
"github.com/prometheus/alertmanager/notify/victorops"
"github.com/prometheus/alertmanager/notify/webhook"
"github.com/prometheus/alertmanager/notify/wechat"
Expand Down Expand Up @@ -169,6 +170,9 @@ func buildReceiverIntegrations(nc *config.Receiver, tmpl *template.Template, log
for i, c := range nc.SNSConfigs {
add("sns", i, c, func(l log.Logger) (notify.Notifier, error) { return sns.New(c, tmpl, l) })
}
for i, c := range nc.TelegramConfigs {
add("telegram", i, c, func(l log.Logger) (notify.Notifier, error) { return telegram.New(c, tmpl, l) })
}
if errs.Len() > 0 {
return nil, &errs
}
Expand Down
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ func resolveFilepaths(baseDir string, cfg *Config) {
for _, cfg := range receiver.SNSConfigs {
cfg.HTTPConfig.SetDirectory(baseDir)
}
for _, cfg := range receiver.TelegramConfigs {
cfg.HTTPConfig.SetDirectory(baseDir)
}
}
}

Expand Down Expand Up @@ -460,6 +463,16 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
sns.HTTPConfig = c.Global.HTTPConfig
}
}

for _, telegram := range rcv.TelegramConfigs {
if telegram.HTTPConfig == nil {
telegram.HTTPConfig = c.Global.HTTPConfig
}
if telegram.APIUrl == nil {
telegram.APIUrl = c.Global.TelegramAPIUrl
}
}

names[rcv.Name] = struct{}{}
}

Expand Down Expand Up @@ -540,6 +553,7 @@ func DefaultGlobalConfig() GlobalConfig {
OpsGenieAPIURL: mustParseURL("https://api.opsgenie.com/"),
WeChatAPIURL: mustParseURL("https://qyapi.weixin.qq.com/cgi-bin/"),
VictorOpsAPIURL: mustParseURL("https://alert.victorops.com/integrations/generic/20131114/alert/"),
TelegramAPIUrl: mustParseURL("https://api.telegram.org"),
}
}

Expand Down Expand Up @@ -660,6 +674,7 @@ type GlobalConfig struct {
WeChatAPICorpID string `yaml:"wechat_api_corp_id,omitempty" json:"wechat_api_corp_id,omitempty"`
VictorOpsAPIURL *URL `yaml:"victorops_api_url,omitempty" json:"victorops_api_url,omitempty"`
VictorOpsAPIKey Secret `yaml:"victorops_api_key,omitempty" json:"victorops_api_key,omitempty"`
TelegramAPIUrl *URL `yaml:"telegram_api_url,omitempty" json:"telegram_api_url,omitempty"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface for GlobalConfig.
Expand Down Expand Up @@ -799,6 +814,7 @@ type Receiver struct {
PushoverConfigs []*PushoverConfig `yaml:"pushover_configs,omitempty" json:"pushover_configs,omitempty"`
VictorOpsConfigs []*VictorOpsConfig `yaml:"victorops_configs,omitempty" json:"victorops_configs,omitempty"`
SNSConfigs []*SNSConfig `yaml:"sns_configs,omitempty" json:"sns_configs,omitempty"`
TelegramConfigs []*TelegramConfig `yaml:"telegram_configs,omitempty" json:"telegram_configs,omitempty"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface for Receiver.
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ func TestEmptyFieldsAndRegex(t *testing.T) {
OpsGenieAPIURL: mustParseURL("https://api.opsgenie.com/"),
WeChatAPIURL: mustParseURL("https://qyapi.weixin.qq.com/cgi-bin/"),
VictorOpsAPIURL: mustParseURL("https://alert.victorops.com/integrations/generic/20131114/alert/"),
TelegramAPIUrl: mustParseURL("https://api.telegram.org"),
},

Templates: []string{
Expand Down
48 changes: 48 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ var (
Subject: `{{ template "sns.default.subject" . }}`,
Message: `{{ template "sns.default.message" . }}`,
}

DefaultTelegramConfig = TelegramConfig{
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
DisableNotifications: false,
Message: `{{ template "telegram.default.message" . }}`,
ParseMode: "MarkdownV2",
}
)

// NotifierConfig contains base options common across all notifier configurations.
Expand Down Expand Up @@ -625,3 +634,42 @@ func (c *SNSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
return nil
}

// TelegramConfig configures notifications via Telegram.
type TelegramConfig struct {
NotifierConfig `yaml:",inline" json:",inline"`

HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`

APIUrl *URL `yaml:"api_url" json:"api_url,omitempty"`
BotToken Secret `yaml:"bot_token,omitempty" json:"token,omitempty"`
ChatID int64 `yaml:"chat_id,omitempty" json:"chat,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"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *TelegramConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultTelegramConfig
type plain TelegramConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if c.BotToken == "" {
return fmt.Errorf("missing bot_token on telegram_config")
}
if c.ChatID == 0 {
return fmt.Errorf("missing chat_id on telegram_config")
}
if c.APIUrl == nil {
return fmt.Errorf("missing api_url on telegram_config")
}
if c.ParseMode != "" &&
c.ParseMode != "Markdown" &&
c.ParseMode != "MarkdownV2" &&
c.ParseMode != "HTML" {
return fmt.Errorf("unknown parse_mode on telegram_config, must be Markdown, MarkdownV2, HTML or empty string")
}
return nil
}
32 changes: 31 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ global:
[ wechat_api_url: <string> | default = "https://qyapi.weixin.qq.com/cgi-bin/" ]
[ wechat_api_secret: <secret> ]
[ wechat_api_corp_id: <string> ]

[ telegram_api_url: <string> | default = "https://api.telegram.org" ]
# The default HTTP client configuration
[ http_config: <http_config> ]

Expand Down Expand Up @@ -452,6 +452,8 @@ webhook_configs:
[ - <webhook_config>, ... ]
wechat_configs:
[ - <wechat_config>, ... ]
telegram_configs:
[ - <telegram_config>, ... ]
```

## `<email_config>`
Expand Down Expand Up @@ -1002,3 +1004,31 @@ API](http://admin.wechat.com/wiki/index.php?title=Customer_Service_Messages).
[ to_party: <string> | default = '{{ template "wechat.default.to_party" . }}' ]
[ to_tag: <string> | default = '{{ template "wechat.default.to_tag" . }}' ]
```
## `<telegram_config>`
```yaml
# Whether to notify about resolved alerts.
[ send_resolved: <boolean> | default = true ]
# The Telegram API URL i.e. https://api.telegram.org.
# If not specified, default API URL will be used.
[ api_url: <string> | default = global.telegram_api_url ]
# Telegram bot token
[ bot_token: <string> ]
# ID of the chat where to send the messages.
[ chat_id: <int> ]
# Message template
[ message: <tmpl_string> default = '{{ template "telegram.default.message" .}}' ]
# Disable telegram notifications
[ disable_notifications: <boolean> | default = false ]
# Parse mode for telegram message, supported values are MarkdownV2, Markdown, HTML and empty string for plain text.
[ parse_mode: <string> | default = "MarkdownV2" ]
# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]
```
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/oklog/run v1.1.0
github.com/oklog/ulid v1.3.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/common v0.32.1
github.com/prometheus/common/sigv4 v0.1.0
github.com/prometheus/exporter-toolkit v0.7.1
Expand All @@ -38,6 +38,7 @@ require (
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f
golang.org/x/tools v0.1.8
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/telebot.v3 v3.0.0-20220130115853-f0291132d3c3
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
Loading

0 comments on commit 1138a08

Please sign in to comment.