Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding telegram support #2827

Merged
merged 20 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
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.

5 changes: 5 additions & 0 deletions cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ 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"

timmilesdw marked this conversation as resolved.
Show resolved Hide resolved
"github.com/prometheus/alertmanager/provider/mem"
"github.com/prometheus/alertmanager/silence"
"github.com/prometheus/alertmanager/template"
Expand Down Expand Up @@ -169,6 +171,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
45 changes: 45 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,39 @@ 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 string `yaml:"bot_token,omitempty" json:"token,omitempty"`
timmilesdw marked this conversation as resolved.
Show resolved Hide resolved
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 {
timmilesdw marked this conversation as resolved.
Show resolved Hide resolved
*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.ParseMode != "" &&
c.ParseMode != "Markdown" &&
c.ParseMode != "MarkdownV2" &&
c.ParseMode != "HTML" {
return fmt.Errorf("unknown parse_mode on telegram_config")
timmilesdw marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
31 changes: 30 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,30 @@ 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> ]

[ chat_id: <int> ]
timmilesdw marked this conversation as resolved.
Show resolved Hide resolved

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

# Disable telegram notifications
[ disable_notifications: <boolean> | default = false ]

# Parse mode for telegram messages, can be MarkdownV2 | Markdown | HTML
[ parse_mode: <string> | default = "MarkdownV2" ]
timmilesdw marked this conversation as resolved.
Show resolved Hide resolved

# 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