PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
FIX (notifications): Count the Telegram text limit in UTF-16 units and send plain text, so a failure message with emoji or with < and & in the dump output still reaches the chat
Rostislav Dugin3654eb84d221
diff --git a/backend/internal/features/notifiers/models/telegram/constants.go b/backend/internal/features/notifiers/models/telegram/constants.goindex 2b4d0f02..d2f620b0 100644--- a/backend/internal/features/notifiers/models/telegram/constants.go+++ b/backend/internal/features/notifiers/models/telegram/constants.go@@ -1,3 +1,3 @@package telegram_notifier-const maxTextRunes = 4096+const maxTextUTF16Units = 4096diff --git a/backend/internal/features/notifiers/models/telegram/model.go b/backend/internal/features/notifiers/models/telegram/model.goindex 1a76fae0..ccf54705 100644--- a/backend/internal/features/notifiers/models/telegram/model.go+++ b/backend/internal/features/notifiers/models/telegram/model.go@@ -10,6 +10,7 @@ import ("net/url""strconv""strings"+ "unicode/utf16""github.com/google/uuid"@@ -82,7 +83,6 @@ func (t *TelegramNotifier) Send(data := url.Values{}data.Set("chat_id", t.TargetChatID)data.Set("text", buildAndTruncateText(notification))- data.Set("parse_mode", "HTML")if t.ThreadID != nil && *t.ThreadID != 0 {data.Set("message_thread_id", strconv.FormatInt(*t.ThreadID, 10))@@ -131,9 +131,14 @@ func buildAndTruncateText(notification notifier_models.Notification) string {text = fmt.Sprintf("%s\n\n%s", notification.Heading, notification.Message)}- textRunes := []rune(text)- if len(textRunes) > maxTextRunes {- return string(textRunes[:maxTextRunes])+ // Telegram measures the text limit in UTF-16 code units, so a character outside the+ // Basic Multilingual Plane, such as most emoji, takes two of them.+ textUTF16Units := 0+ for byteOffset, textRune := range text {+ textUTF16Units += utf16.RuneLen(textRune)+ if textUTF16Units > maxTextUTF16Units {+ return text[:byteOffset]+ }}return textdiff --git a/backend/internal/features/notifiers/models/telegram/model_test.go b/backend/internal/features/notifiers/models/telegram/model_test.goindex b2561e55..f3559857 100644--- a/backend/internal/features/notifiers/models/telegram/model_test.go+++ b/backend/internal/features/notifiers/models/telegram/model_test.go@@ -106,7 +106,56 @@ func Test_GetSendMessageURL_WithoutBotAPIURL_UsesOfficialTelegramAPI(t *testing.assert.Equal(t, "https://api.telegram.org/bot123456:AAH-TOKEN/sendMessage", sendMessageURL)}-func Test_Send_WhenTextExceedsTelegramLimit_TruncatesTextByRunes(t *testing.T) {+func Test_Send_WhenTextExceedsTelegramLimit_TruncatesTextByCharacters(t *testing.T) {+ heading := "Backup failed"++ sentForm := sendNotificationAndCaptureForm(t, notifier_models.Notification{+ Type: notifier_models.NotificationTypeBackupFailed,+ Heading: heading,+ Message: strings.Repeat("界", maxTextUTF16Units),+ })++ messagePrefixLength := maxTextUTF16Units - len(heading) - len("\n\n")+ assert.Equal(+ t,+ heading+"\n\n"+strings.Repeat("界", messagePrefixLength),+ sentForm.Get("text"),+ )+}++func Test_Send_WhenTextWithEmojiExceedsTelegramLimit_CountsEmojiAsTwoUnits(t *testing.T) {+ heading := "Backup failed"++ sentForm := sendNotificationAndCaptureForm(t, notifier_models.Notification{+ Type: notifier_models.NotificationTypeBackupFailed,+ Heading: heading,+ Message: strings.Repeat("🔥", maxTextUTF16Units),+ })++ emojiThatFit := (maxTextUTF16Units - len(heading) - len("\n\n")) / 2+ assert.Equal(+ t,+ heading+"\n\n"+strings.Repeat("🔥", emojiThatFit),+ sentForm.Get("text"),+ )+}++func Test_Send_WhenMessageContainsHTMLSpecialCharacters_SendsPlainTextWithoutParseMode(t *testing.T) {+ message := "mysqldump: Got error: 1146: Table 'db.<orders>' doesn't exist & aborted"++ sentForm := sendNotificationAndCaptureForm(t, notifier_models.Notification{+ Type: notifier_models.NotificationTypeBackupFailed,+ Heading: "Backup failed",+ Message: message,+ })++ assert.Equal(t, "Backup failed\n\n"+message, sentForm.Get("text"))+ assert.False(t, sentForm.Has("parse_mode"))+}++func sendNotificationAndCaptureForm(t *testing.T, notification notifier_models.Notification) url.Values {+ t.Helper()+botAPIURL, recorder := notifier_models.StartRecordingServer(t,notifier_models.StubResponse{StatusCode: http.StatusOK, Body: `{"ok":true}`},@@ -116,26 +165,16 @@ func Test_Send_WhenTextExceedsTelegramLimit_TruncatesTextByRunes(t *testing.T) {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),- },+ notification,))-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"),- )+ return sentForm}