Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FIX (notifications): Truncate Telegram messages to 4096 characters

leocaa3a140724222
diff --git a/backend/internal/features/notifiers/models/telegram/constants.go b/backend/internal/features/notifiers/models/telegram/constants.go
new file mode 100644
index 00000000..2b4d0f02
--- /dev/null
+++ b/backend/internal/features/notifiers/models/telegram/constants.go
@@ -0,0 +1,3 @@
+package telegram_notifier
+
+const maxTextRunes = 4096
diff --git a/backend/internal/features/notifiers/models/telegram/model.go b/backend/internal/features/notifiers/models/telegram/model.go
index 3fa61164..1a76fae0 100644
--- a/backend/internal/features/notifiers/models/telegram/model.go
+++ b/backend/internal/features/notifiers/models/telegram/model.go
@@ -74,11 +74,6 @@ func (t *TelegramNotifier) Send(
return fmt.Errorf("failed to decrypt bot token: %w", err)
}
- fullMessage := notification.Heading
- if notification.Message != "" {
- fullMessage = fmt.Sprintf("%s\n\n%s", notification.Heading, notification.Message)
- }
-
sendMessageURL, err := t.getSendMessageURL(botToken)
if err != nil {
return err
@@ -86,7 +81,7 @@ func (t *TelegramNotifier) Send(
data := url.Values{}
data.Set("chat_id", t.TargetChatID)
- data.Set("text", fullMessage)
+ data.Set("text", buildAndTruncateText(notification))
data.Set("parse_mode", "HTML")
if t.ThreadID != nil && *t.ThreadID != 0 {
@@ -130,6 +125,20 @@ func (t *TelegramNotifier) Send(
return nil
}
+func buildAndTruncateText(notification notifier_models.Notification) string {
+ text := notification.Heading
+ if notification.Message != "" {
+ text = fmt.Sprintf("%s\n\n%s", notification.Heading, notification.Message)
+ }
+
+ textRunes := []rune(text)
+ if len(textRunes) > maxTextRunes {
+ return string(textRunes[:maxTextRunes])
+ }
+
+ return text
+}
+
func (t *TelegramNotifier) HideSensitiveData() {
t.BotToken = ""
t.ProxyURL = ""
diff --git a/backend/internal/features/notifiers/models/telegram/model_test.go b/backend/internal/features/notifiers/models/telegram/model_test.go
index 98f7c537..b2561e55 100644
--- a/backend/internal/features/notifiers/models/telegram/model_test.go
+++ b/backend/internal/features/notifiers/models/telegram/model_test.go
@@ -1,6 +1,11 @@
package telegram_notifier
import (
+ "io"
+ "log/slog"
+ "net/http"
+ "net/url"
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -100,3 +105,37 @@ func Test_GetSendMessageURL_WithoutBotAPIURL_UsesOfficialTelegramAPI(t *testing.
require.NoError(t, err)
assert.Equal(t, "https://api.telegram.org/bot123456:AAH-TOKEN/sendMessage", sendMessageURL)
}
+
+func Test_Send_WhenTextExceedsTelegramLimit_TruncatesTextByRunes(t *testing.T) {
+ botAPIURL, recorder := notifier_models.StartRecordingServer(
+ t,
+ notifier_models.StubResponse{StatusCode: http.StatusOK, Body: `{"ok":true}`},
+ )
+ notifier := &TelegramNotifier{
+ BotToken: "123456:AAH-TOKEN",
+ TargetChatID: "123456",
+ BotAPIURL: botAPIURL,
+ }
+ heading := "Backup failed"
+
+ require.NoError(t, notifier.Send(
+ notifier_models.PassthroughEncryptor{},
+ slog.New(slog.NewTextHandler(io.Discard, nil)),
+ notifier_models.Notification{
+ Type: notifier_models.NotificationTypeBackupFailed,
+ Heading: heading,
+ Message: strings.Repeat("界", maxTextRunes),
+ },
+ ))
+
+ require.Equal(t, 1, recorder.GetRequestCount())
+ sentForm, err := url.ParseQuery(recorder.GetLastRequest().Body)
+ require.NoError(t, err)
+
+ messagePrefixLength := maxTextRunes - len([]rune(heading)) - len([]rune("\n\n"))
+ assert.Equal(
+ t,
+ heading+"\n\n"+strings.Repeat("界", messagePrefixLength),
+ sentForm.Get("text"),
+ )
+}