Skip to content

Commit 71e650f

Browse files
committed
Removed message splitting for large messages (Doesn't work well with HTML formatting), fixes #33.
1 parent e475c01 commit 71e650f

File tree

3 files changed

+12
-33
lines changed

3 files changed

+12
-33
lines changed

Changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Change history
22
--------------
33

4-
* **Version 1.4.3.0 (2024-12-02)** : Removed support for Net6.0, added support for Net9.0, updated NuGet packages.
4+
* **Version 1.4.3.0 (2024-12-02)** : Removed support for Net6.0, added support for Net9.0, updated NuGet packages, removed message splitting for large messages (Doesn't work well with HTML formatting), fixes https://github.com/serilog-contrib/Serilog.Sinks.Telegram.Alternative/issues/33.
55
* **Version 1.4.2.0 (2024-05-16)** : Removed support for Net7.0.
66
* **Version 1.4.1.0 (2024-03-03)**: Updated NuGet packages.
77
* **Version 1.4.0.0 (2023-11-21)**: Updated NuGet packages, removed support for netstandard, added support for Net8.0.

src/Serilog.Sinks.Telegram.Alternative/Serilog.Sinks.Telegram.Alternative.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<RepositoryUrl>https://github.com/serilog-contrib/Serilog.Sinks.Telegram.Alternative</RepositoryUrl>
1717
<PackageIcon>Icon.png</PackageIcon>
1818
<RepositoryType>Github</RepositoryType>
19-
<PackageReleaseNotes>Version 1.4.3.0 (2024-12-02): Removed support for Net6.0, added support for Net9.0, updated NuGet packages.</PackageReleaseNotes>
19+
<PackageReleaseNotes>Version 1.4.3.0 (2024-12-02): Removed support for Net6.0, added support for Net9.0, updated NuGet packages, removed message splitting for large messages (Doesn't work well with HTML formatting), fixes https://github.com/serilog-contrib/Serilog.Sinks.Telegram.Alternative/issues/33.</PackageReleaseNotes>
2020
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2121
<LangVersion>latest</LangVersion>
2222
<Nullable>enable</Nullable>

src/Serilog.Sinks.Telegram.Alternative/Sinks/Telegram/Alternative/TelegramSink.cs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,17 @@ private async Task SendMessage(HttpClient httpClient, string? botApiUrl, string
211211

212212
try
213213
{
214-
foreach (var messageChunk in GetMessageChunks(message))
214+
if (message.Length > MaxMessageLength)
215215
{
216-
var response = await client.PostMessage(messageChunk, chatId, topicId);
216+
this.TryWriteToSelflog($"Message is too long to be sent, it must not exceed {MaxMessageLength} characters.");
217+
return;
218+
}
217219

218-
if (response is not null)
219-
{
220-
this.TryWriteToSelflog($"Message sent to chatId '{chatId}': '{response.StatusCode}'.");
221-
}
220+
var response = await client.PostMessage(message, chatId, topicId);
221+
222+
if (response is not null)
223+
{
224+
this.TryWriteToSelflog($"Message sent to chatId '{chatId}': '{response.StatusCode}'.");
222225
}
223226
}
224227
catch (Exception ex)
@@ -227,31 +230,7 @@ private async Task SendMessage(HttpClient httpClient, string? botApiUrl, string
227230
this.options.FailureCallback?.Invoke(ex);
228231
}
229232
}
230-
231-
/// <summary>
232-
/// Gets the message chunks.
233-
/// </summary>
234-
/// <param name="message">The message.</param>
235-
/// <returns>A <see cref="IEnumerable{T}"/> of <see cref="string"/> containing the chunks.</returns>
236-
private static IEnumerable<string> GetMessageChunks(string message)
237-
{
238-
if (message.Length <= MaxMessageLength)
239-
{
240-
return new[] { message };
241-
}
242-
243-
var chunksCount = (message.Length + MaxMessageLength - 1) / MaxMessageLength;
244-
var result = new string[chunksCount];
245-
246-
for (var i = 0; i < chunksCount; i++)
247-
{
248-
var isLastChunk = chunksCount - i == 1;
249-
var chunkLength = isLastChunk ? message.Length % MaxMessageLength : MaxMessageLength;
250-
result[i] = message.Substring(i * MaxMessageLength, chunkLength);
251-
}
252-
253-
return result;
254-
}
233+
255234

256235
/// <summary>
257236
/// Tries to write to Selflog and throws an exception if a formatting error occurred.

0 commit comments

Comments
 (0)