diff --git a/pkg/channels/telegram/telegram.go b/pkg/channels/telegram/telegram.go index bfed0d2a4..6bc774e9c 100644 --- a/pkg/channels/telegram/telegram.go +++ b/pkg/channels/telegram/telegram.go @@ -237,14 +237,10 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err return nil } - // Split the raw markdown before converting to HTML so that - // SplitMessage's code-fence-aware logic works correctly and - // we never break HTML tags/entities by splitting converted output. - mdChunks := channels.SplitMessage(msg.Content, 4000) - - // Use a queue so that chunks whose HTML expansion still exceeds - // Telegram's 4096-char limit can be re-split until every chunk fits. - queue := append([]string{}, mdChunks...) + // The Manager already splits messages to ≤4000 chars (WithMaxMessageLength), + // so msg.Content is guaranteed to be within that limit. We still need to + // check if HTML expansion pushes it beyond Telegram's 4096-char API limit. + queue := []string{msg.Content} for len(queue) > 0 { chunk := queue[0] queue = queue[1:] diff --git a/pkg/channels/telegram/telegram_test.go b/pkg/channels/telegram/telegram_test.go index 71ad71636..3a2f1aa66 100644 --- a/pkg/channels/telegram/telegram_test.go +++ b/pkg/channels/telegram/telegram_test.go @@ -115,7 +115,11 @@ func TestSend_ShortMessage_SingleCall(t *testing.T) { assert.Len(t, caller.calls, 1, "short message should result in exactly one SendMessage call") } -func TestSend_LongMessage_MultipleCalls(t *testing.T) { +func TestSend_LongMessage_SingleCall(t *testing.T) { + // With WithMaxMessageLength(4000), the Manager pre-splits messages before + // they reach Send(). A message at exactly 4000 chars should go through + // as a single SendMessage call (no re-split needed since HTML expansion + // won't exceed 4096 for plain text). caller := &stubCaller{ callFn: func(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) { return successResponse(t), nil @@ -123,8 +127,7 @@ func TestSend_LongMessage_MultipleCalls(t *testing.T) { } ch := newTestChannel(t, caller) - // Create a message over 4000 chars so it gets split into multiple chunks. - longContent := strings.Repeat("a", 4001) + longContent := strings.Repeat("a", 4000) err := ch.Send(context.Background(), bus.OutboundMessage{ ChatID: "12345", @@ -132,7 +135,7 @@ func TestSend_LongMessage_MultipleCalls(t *testing.T) { }) assert.NoError(t, err) - assert.Greater(t, len(caller.calls), 1, "long message should be split into multiple SendMessage calls") + assert.Len(t, caller.calls, 1, "pre-split message within limit should result in one SendMessage call") } func TestSend_HTMLFallback_PerChunk(t *testing.T) {