From 569ed83d7dd1cef39ec83930210b227cc519261e Mon Sep 17 00:00:00 2001 From: sergeyWh1te Date: Tue, 24 Sep 2024 16:37:01 +0300 Subject: [PATCH] feat: Fix sending network alerts though telegram Signed-off-by: sergeyWh1te --- Changelog.md | 3 +++ internal/app/worker/worker.go | 23 ++++++++++++++++++++--- internal/pkg/notifiler/telegram.go | 20 ++++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Changelog.md b/Changelog.md index 829406b..2c550d3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,6 @@ +## 24.09.2024 +1. Fix sending network alerts though telegram + ## 23.09.2024 1. Add lru for quorum 2. Tun docker-compose-file diff --git a/internal/app/worker/worker.go b/internal/app/worker/worker.go index b704220..5deca5a 100644 --- a/internal/app/worker/worker.go +++ b/internal/app/worker/worker.go @@ -228,6 +228,26 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error { err error ) + // TODO add finding.blockNumber + w.log.Info(fmt.Sprintf("Consumer: %s AlertId %s read %d times", consumer.Name, finding.AlertId, count), + slog.Attr{ + Key: `desc`, + Value: slog.StringValue(finding.Description), + }, + slog.Attr{ + Key: `name`, + Value: slog.StringValue(finding.Name), + }, + slog.Attr{ + Key: `alertId`, + Value: slog.StringValue(finding.AlertId), + }, + slog.Attr{ + Key: `severity`, + Value: slog.StringValue(finding.Severity.String()), + }, + ) + if !w.cache.Contains(key) { count, err = w.redisClient.Incr(ctx, countKey).Uint64() if err != nil { @@ -263,9 +283,6 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error { } } - // TODO add finding.blockNumber - w.log.InfoContext(ctx, fmt.Sprintf("Consumer: %s AlertId %s read %d times", consumer.Name, finding.AlertId, count)) - if count >= w.quorum { status, err := w.GetStatus(ctx, statusKey) if err != nil { diff --git a/internal/pkg/notifiler/telegram.go b/internal/pkg/notifiler/telegram.go index 93ef073..c6d86c7 100644 --- a/internal/pkg/notifiler/telegram.go +++ b/internal/pkg/notifiler/telegram.go @@ -35,18 +35,30 @@ func NewTelegram(botToken, chatID string, httpClient *http.Client, metricsStore func (u *telegram) SendFinding(ctx context.Context, alert *proto.Finding) error { message := fmt.Sprintf("%s\n\n%s\n\nAlertId: %s\nSource: %s", alert.Name, alert.Description, alert.GetAlertId(), u.source) - return u.send(ctx, message) + if alert.Severity != proto.Finding_UNKNOWN { + return u.send(ctx, message, true) + } + + return u.send(ctx, message, false) } func (u *telegram) SendAlert(ctx context.Context, alert *models.Alert) error { message := fmt.Sprintf("%s\n\n%s\n\nAlertId: %s\nSource: %s", alert.Name, alert.Description, alert.AlertID, u.source) - return u.send(ctx, message) + if alert.Severity != models.AlertSeverityUNKNOWN { + return u.send(ctx, message, true) + } + + return u.send(ctx, message, false) } -func (u *telegram) send(ctx context.Context, message string) error { +func (u *telegram) send(ctx context.Context, message string, useMarkdown bool) error { //nolint - requestURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=-%s&text=%s&parse_mode=markdown", u.botToken, u.chatID, url.QueryEscape(message)) + requestURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=-%s&text=%s", u.botToken, u.chatID, url.QueryEscape(message)) + if useMarkdown { + requestURL += `&parse_mode=markdown` + } + req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, http.NoBody) if err != nil { return fmt.Errorf("could not create telegram request: %w", err)