diff --git a/http/swagger.yml b/http/swagger.yml index 168c889f41..cea3ab340e 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -11616,9 +11616,6 @@ components: messageTemplate: description: The message template as a flux interpolated string. type: string - channel: - description: Id of the telegram channel. - type: string parseMode: description: Parse mode of the message text per https://core.telegram.org/bots/api#formatting-options . Defaults to "MarkdownV2" . type: string @@ -11778,10 +11775,14 @@ components: allOf: - $ref: "#/components/schemas/NotificationEndpointBase" - type: object + required: [token, channel] properties: token: description: Specifies the Telegram bot token. See https://core.telegram.org/bots#creating-a-new-bot . type: string + channel: + description: ID of the telegram channel, a chat_id in https://core.telegram.org/bots/api#sendmessage . + type: string NotificationEndpointType: type: string enum: ["slack", "pagerduty", "http", "telegram"] diff --git a/notification/endpoint/endpoint_test.go b/notification/endpoint/endpoint_test.go index a1bf94e17d..a2a6932071 100644 --- a/notification/endpoint/endpoint_test.go +++ b/notification/endpoint/endpoint_test.go @@ -62,7 +62,8 @@ func TestValidEndpoint(t *testing.T) { OrgID: influxTesting.MustIDBase16Ptr(id3), Status: influxdb.Active, }, - Token: influxdb.SecretField{Key: id1 + "-token"}, + Token: influxdb.SecretField{Key: id1 + "-token"}, + Channel: "-1001406363649", }, err: &influxdb.Error{ Code: influxdb.EInvalid, @@ -146,11 +147,23 @@ func TestValidEndpoint(t *testing.T) { }, }, { - name: "valid telegram token", + name: "empty telegram channel", src: &endpoint.Telegram{ Base: goodBase, Token: influxdb.SecretField{Key: id1 + "-token"}, }, + err: &influxdb.Error{ + Code: influxdb.EInvalid, + Msg: "empty telegram channel", + }, + }, + { + name: "valid telegram token", + src: &endpoint.Telegram{ + Base: goodBase, + Token: influxdb.SecretField{Key: id1 + "-token"}, + Channel: "-1001406363649", + }, err: nil, }, } diff --git a/notification/endpoint/telegram.go b/notification/endpoint/telegram.go index 9852338fc6..00498ead39 100644 --- a/notification/endpoint/telegram.go +++ b/notification/endpoint/telegram.go @@ -15,6 +15,8 @@ type Telegram struct { Base // Token is the telegram bot token, see https://core.telegram.org/bots#creating-a-new-bot Token influxdb.SecretField `json:"token"` + // Channel is an ID of the telegram channel, see https://core.telegram.org/bots/api#sendmessage + Channel string `json:"channel"` } // BackfillSecretKeys fill back the secret field key during the unmarshalling @@ -45,6 +47,12 @@ func (s Telegram) Valid() error { Msg: "empty telegram bot token", } } + if s.Channel == "" { + return &influxdb.Error{ + Code: influxdb.EInvalid, + Msg: "empty telegram channel", + } + } return nil } diff --git a/notification/rule/rule_test.go b/notification/rule/rule_test.go index f7950d49f9..91475cd950 100644 --- a/notification/rule/rule_test.go +++ b/notification/rule/rule_test.go @@ -353,7 +353,6 @@ func TestJSON(t *testing.T) { UpdatedAt: timeGen2.Now(), }, }, - Channel: "channel1", MessageTemplate: "blah", }, }, diff --git a/notification/rule/telegram.go b/notification/rule/telegram.go index 73dc2dc061..fdfb5cc670 100644 --- a/notification/rule/telegram.go +++ b/notification/rule/telegram.go @@ -14,7 +14,6 @@ import ( type Telegram struct { Base MessageTemplate string `json:"messageTemplate"` - Channel string `json:"channel"` ParseMode string `json:"parseMode"` DisableWebPagePreview bool `json:"disableWebPagePreview"` } @@ -52,7 +51,7 @@ func (s *Telegram) generateFluxASTBody(e *endpoint.Telegram) []ast.Statement { statements = append(statements, s.generateFluxASTNotificationDefinition(e)) statements = append(statements, s.generateFluxASTStatuses()) statements = append(statements, s.generateLevelChecks()...) - statements = append(statements, s.generateFluxASTNotifyPipe()) + statements = append(statements, s.generateFluxASTNotifyPipe(e)) return statements } @@ -77,9 +76,9 @@ func (s *Telegram) generateFluxASTEndpoint(e *endpoint.Telegram) ast.Statement { return flux.DefineVariable("telegram_endpoint", call) } -func (s *Telegram) generateFluxASTNotifyPipe() ast.Statement { +func (s *Telegram) generateFluxASTNotifyPipe(e *endpoint.Telegram) ast.Statement { endpointProps := []*ast.Property{} - endpointProps = append(endpointProps, flux.Property("channel", flux.String(s.Channel))) + endpointProps = append(endpointProps, flux.Property("channel", flux.String(e.Channel))) endpointProps = append(endpointProps, flux.Property("text", flux.String(s.MessageTemplate))) endpointProps = append(endpointProps, flux.Property("silent", s.generateSilent())) endpointFn := flux.Function(flux.FunctionParams("r"), flux.Object(endpointProps...)) @@ -132,12 +131,6 @@ func (s Telegram) Valid() error { Msg: "Telegram MessageTemplate is invalid", } } - if s.Channel == "" { - return &influxdb.Error{ - Code: influxdb.EInvalid, - Msg: "Telegram Channel is invalid", - } - } return nil } diff --git a/notification/rule/telegram_test.go b/notification/rule/telegram_test.go index fa52d680eb..8dae3b0fa9 100644 --- a/notification/rule/telegram_test.go +++ b/notification/rule/telegram_test.go @@ -31,7 +31,6 @@ func TestTelegram_GenerateFlux(t *testing.T) { }, rule: &rule.Telegram{ MessageTemplate: "blah", - Channel: "-12345", Base: rule.Base{ ID: 1, EndpointID: 3, @@ -69,11 +68,11 @@ func TestTelegram_GenerateFlux(t *testing.T) { ID: idPtr(3), Name: "foo", }, - Token: influxdb.SecretField{Key: "3-key"}, + Token: influxdb.SecretField{Key: "3-key"}, + Channel: "-12345", }, rule: &rule.Telegram{ MessageTemplate: "blah", - Channel: "-12345", Base: rule.Base{ ID: 1, EndpointID: 3, @@ -139,11 +138,11 @@ all_statuses ID: idPtr(3), Name: "foo", }, - Token: influxdb.SecretField{Key: "3-key"}, + Token: influxdb.SecretField{Key: "3-key"}, + Channel: "-12345", }, rule: &rule.Telegram{ MessageTemplate: "blah", - Channel: "-12345", DisableWebPagePreview: true, ParseMode: "HTML", Base: rule.Base{ @@ -233,7 +232,6 @@ func TestTelegram_Valid(t *testing.T) { name: "valid template", rule: &rule.Telegram{ MessageTemplate: "blah", - Channel: "-12345", Base: rule.Base{ ID: 1, EndpointID: 3, @@ -255,7 +253,6 @@ func TestTelegram_Valid(t *testing.T) { name: "missing MessageTemplate", rule: &rule.Telegram{ MessageTemplate: "", - Channel: "-12345", Base: rule.Base{ ID: 1, EndpointID: 3, @@ -280,7 +277,6 @@ func TestTelegram_Valid(t *testing.T) { name: "missing EndpointID", rule: &rule.Telegram{ MessageTemplate: "", - Channel: "-12345", Base: rule.Base{ ID: 1, // EndpointID: 3, @@ -301,30 +297,6 @@ func TestTelegram_Valid(t *testing.T) { Msg: "Notification Rule EndpointID is invalid", }, }, - { - name: "missing Channel", - rule: &rule.Telegram{ - MessageTemplate: "blah", - Base: rule.Base{ - ID: 1, - EndpointID: 3, - OwnerID: 4, - OrgID: 5, - Name: "foo", - Every: mustDuration("1h"), - StatusRules: []notification.StatusRule{ - { - CurrentLevel: notification.Critical, - }, - }, - TagRules: []notification.TagRule{}, - }, - }, - err: &influxdb.Error{ - Code: influxdb.EInvalid, - Msg: "Telegram Channel is invalid", - }, - }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { diff --git a/ui/src/notifications/endpoints/components/EndpointOptions.tsx b/ui/src/notifications/endpoints/components/EndpointOptions.tsx index 6bf2ca6860..542a906e77 100644 --- a/ui/src/notifications/endpoints/components/EndpointOptions.tsx +++ b/ui/src/notifications/endpoints/components/EndpointOptions.tsx @@ -43,8 +43,14 @@ const EndpointOptions: FC = ({ ) } case 'telegram': { - const {token} = endpoint as TelegramNotificationEndpoint - return + const {token, channel} = endpoint as TelegramNotificationEndpoint + return ( + + ) } case 'http': { const { diff --git a/ui/src/notifications/endpoints/components/EndpointOptionsTelegram.tsx b/ui/src/notifications/endpoints/components/EndpointOptionsTelegram.tsx index 91a11ac662..4fa8d324f6 100644 --- a/ui/src/notifications/endpoints/components/EndpointOptionsTelegram.tsx +++ b/ui/src/notifications/endpoints/components/EndpointOptionsTelegram.tsx @@ -13,10 +13,11 @@ import { interface Props { token: string + channel: string onChange: (e: ChangeEvent) => void } -const EndpointOptionsTelegram: FC = ({token, onChange}) => { +const EndpointOptionsTelegram: FC = ({token, channel, onChange}) => { return ( @@ -35,6 +36,15 @@ const EndpointOptionsTelegram: FC = ({token, onChange}) => { type={InputType.Password} /> + + + diff --git a/ui/src/notifications/endpoints/components/EndpointOverlay.reducer.ts b/ui/src/notifications/endpoints/components/EndpointOverlay.reducer.ts index 7762295cee..c8532a5cae 100644 --- a/ui/src/notifications/endpoints/components/EndpointOverlay.reducer.ts +++ b/ui/src/notifications/endpoints/components/EndpointOverlay.reducer.ts @@ -64,6 +64,7 @@ export const reducer = ( ...baseProps, type: 'telegram', token: '', + channel: '', } } } diff --git a/ui/src/notifications/rules/components/RuleMessageContents.tsx b/ui/src/notifications/rules/components/RuleMessageContents.tsx index e0410e8193..1106263cc4 100644 --- a/ui/src/notifications/rules/components/RuleMessageContents.tsx +++ b/ui/src/notifications/rules/components/RuleMessageContents.tsx @@ -63,11 +63,10 @@ const RuleMessageContents: FC = ({rule}) => { ) } case 'telegram': { - const {messageTemplate, channel} = rule + const {messageTemplate} = rule return ( ) diff --git a/ui/src/notifications/rules/components/TelegramMessage.tsx b/ui/src/notifications/rules/components/TelegramMessage.tsx index 2b23ebfbe5..712d44bc0f 100644 --- a/ui/src/notifications/rules/components/TelegramMessage.tsx +++ b/ui/src/notifications/rules/components/TelegramMessage.tsx @@ -2,7 +2,7 @@ import React, {FC, ChangeEvent} from 'react' // Components -import {Form, TextArea, Input} from '@influxdata/clockface' +import {Form, TextArea} from '@influxdata/clockface' import {TelegramNotificationRuleBase} from 'src/types/alerting' interface EventHandlers { @@ -10,23 +10,19 @@ interface EventHandlers { } type Props = Omit & EventHandlers -const TelegramMessage: FC = ({messageTemplate, channel, onChange}) => { +const TelegramMessage: FC = ({messageTemplate, onChange}) => { return ( - <> - - - - -