Skip to content

Commit b9bf900

Browse files
committed
feat(alerting): Added the ability to send messages to Telegram topics in groups.
1 parent 7780195 commit b9bf900

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

README.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -1322,24 +1322,27 @@ Here's an example of what the notifications look like:
13221322

13231323

13241324
#### Configuring Telegram alerts
1325-
| Parameter | Description | Default |
1326-
|:--------------------------------------|:-------------------------------------------------------------------------------------------|:---------------------------|
1327-
| `alerting.telegram` | Configuration for alerts of type `telegram` | `{}` |
1328-
| `alerting.telegram.token` | Telegram Bot Token | Required `""` |
1329-
| `alerting.telegram.id` | Telegram User ID | Required `""` |
1330-
| `alerting.telegram.api-url` | Telegram API URL | `https://api.telegram.org` |
1331-
| `alerting.telegram.client` | Client configuration. <br />See [Client configuration](#client-configuration). | `{}` |
1332-
| `alerting.telegram.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A |
1333-
| `alerting.telegram.overrides` | List of overrides that may be prioritized over the default configuration | `[]` |
1334-
| `alerting.telegram.overrides[].group` | Endpoint group for which the configuration will be overridden by this configuration | `""` |
1335-
| `alerting.telegram.overrides[].token` | Telegram Bot Token for override default value | `""` |
1336-
| `alerting.telegram.overrides[].id` | Telegram User ID for override default value | `""` |
1325+
| Parameter | Description | Default |
1326+
|:-----------------------------------------|:-------------------------------------------------------------------------------------------|:---------------------------|
1327+
| `alerting.telegram` | Configuration for alerts of type `telegram` | `{}` |
1328+
| `alerting.telegram.token` | Telegram Bot Token | Required `""` |
1329+
| `alerting.telegram.id` | Telegram User ID or Group ID | Required `""` |
1330+
| `alerting.telegram.topic-id` | Telegram Topic ID in Group | `""` |
1331+
| `alerting.telegram.api-url` | Telegram API URL | `https://api.telegram.org` |
1332+
| `alerting.telegram.client` | Client configuration. <br />See [Client configuration](#client-configuration). | `{}` |
1333+
| `alerting.telegram.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A |
1334+
| `alerting.telegram.overrides` | List of overrides that may be prioritized over the default configuration | `[]` |
1335+
| `alerting.telegram.overrides[].group` | Endpoint group for which the configuration will be overridden by this configuration | `""` |
1336+
| `alerting.telegram.overrides[].token` | Telegram Bot Token for override default value | `""` |
1337+
| `alerting.telegram.overrides[].id` | Telegram User ID for override default value | `""` |
1338+
| `alerting.telegram.overrides[].topic-id` | Telegram Topic ID in Group for override default value | `""` |
13371339

13381340
```yaml
13391341
alerting:
13401342
telegram:
13411343
token: "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
13421344
id: "0123456789"
1345+
topic-id: "7"
13431346
13441347
endpoints:
13451348
- name: website

alerting/provider/telegram/telegram.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const defaultAPIURL = "https://api.telegram.org"
1616

1717
// AlertProvider is the configuration necessary for sending an alert using Telegram
1818
type AlertProvider struct {
19-
Token string `yaml:"token"`
20-
ID string `yaml:"id"`
21-
APIURL string `yaml:"api-url"`
19+
Token string `yaml:"token"`
20+
ID string `yaml:"id"`
21+
TopicId string `yaml:"topic-id,omitempty"`
22+
APIURL string `yaml:"api-url"`
2223

2324
// ClientConfig is the configuration of the client used to communicate with the provider's target
2425
ClientConfig *client.Config `yaml:"client,omitempty"`
@@ -32,9 +33,10 @@ type AlertProvider struct {
3233

3334
// Override is a configuration that may be prioritized over the default configuration
3435
type Override struct {
35-
group string `yaml:"group"`
36-
token string `yaml:"token"`
37-
id string `yaml:"id"`
36+
group string `yaml:"group"`
37+
token string `yaml:"token"`
38+
id string `yaml:"id"`
39+
topicId string `yaml:"topic-id,omitempty"`
3840
}
3941

4042
// IsValid returns whether the provider's configuration is valid
@@ -94,6 +96,7 @@ type Body struct {
9496
ChatID string `json:"chat_id"`
9597
Text string `json:"text"`
9698
ParseMode string `json:"parse_mode"`
99+
TopicId string `json:"message_thread_id,omitempty"`
97100
}
98101

99102
// buildRequestBody builds the request body for the provider
@@ -127,6 +130,7 @@ func (provider *AlertProvider) buildRequestBody(ep *endpoint.Endpoint, alert *al
127130
ChatID: provider.getIDForGroup(ep.Group),
128131
Text: text,
129132
ParseMode: "MARKDOWN",
133+
TopicId: provider.getTopicIdForGroup(ep.Group),
130134
})
131135
return bodyAsJSON
132136
}
@@ -140,6 +144,15 @@ func (provider *AlertProvider) getIDForGroup(group string) string {
140144
return provider.ID
141145
}
142146

147+
func (provider *AlertProvider) getTopicIdForGroup(group string) string {
148+
for _, override := range provider.Overrides {
149+
if override.group == group && len(override.topicId) > 0 {
150+
return override.topicId
151+
}
152+
}
153+
return provider.TopicId
154+
}
155+
143156
// GetDefaultAlert returns the provider's default alert configuration
144157
func (provider *AlertProvider) GetDefaultAlert() *alert.Alert {
145158
return provider.DefaultAlert

alerting/provider/telegram/telegram_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ func TestAlertProvider_buildRequestBody(t *testing.T) {
205205
Resolved: true,
206206
ExpectedBody: "{\"chat_id\":\"123\",\"text\":\"⛑ *Gatus* \\nAn alert for *endpoint-name* has been resolved:\\n—\\n _healthcheck passing successfully 5 time(s) in a row_\\n— \\n*Description* \\n_description-2_ \\n\",\"parse_mode\":\"MARKDOWN\"}",
207207
},
208+
{
209+
Name: "send to topic",
210+
Provider: AlertProvider{ID: "123", TopicId: "7"},
211+
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
212+
Resolved: false,
213+
ExpectedBody: "{\"chat_id\":\"123\",\"text\":\"⛑ *Gatus* \\nAn alert for *endpoint-name* has been triggered:\\n—\\n _healthcheck failed 3 time(s) in a row_\\n— \\n*Description* \\n_description-1_ \\n\\n*Condition results*\\n❌ - `[CONNECTED] == true`\\n❌ - `[STATUS] == 200`\\n\",\"parse_mode\":\"MARKDOWN\",\"message_thread_id\":\"7\"}",
214+
},
208215
}
209216
for _, scenario := range scenarios {
210217
t.Run(scenario.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)