Skip to content

Commit

Permalink
Merge pull request #8 from bjoerns1983/splittest
Browse files Browse the repository at this point in the history
Split Messages for Telegram after 4096 chars
  • Loading branch information
bjoerns1983 authored Jan 22, 2022
2 parents 8ac6671 + 176074e commit 4c5a7e4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public async Task PostAsync(TestNotification request)

_logger.Debug("Telegram <TEST> to {0} - {1}", options.BotToken, options.ChatID);


var httpRequestOptions = new HttpRequestOptions
{
Url = "https://api.telegram.org/bot" + options.BotToken + "/sendmessage?chat_id=" + options.ChatID + "&text=" + message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
39 changes: 32 additions & 7 deletions Emby.Plugin.TelegramNotification/Notifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,50 @@ public async Task SendNotification(UserNotification request, CancellationToken c
{

var options = GetOptions(request.User);
string message = Uri.EscapeDataString(request.Name);
string message = (request.Name);

if (string.IsNullOrEmpty(request.Description) == false && options.SendDescription == true)
{
message = Uri.EscapeDataString(request.Name + "\n\n" + request.Description);
message = (request.Name + "\n\n" + request.Description);
}

_logger.Debug("TeleGram to Token : {0} - {1} - {2}", options.BotToken, options.ChatID, request.Name);

var _httpRequest = new HttpRequestOptions

if (message.Length > 4096)
{
Url = "https://api.telegram.org/bot" + options.BotToken + "/sendmessage?chat_id=" + options.ChatID + "&text=" + message,
CancellationToken = cancellationToken
};
int chunkSize = 4096;
int messageLenght = message.Length;
for (int i = 0; i < messageLenght; i += chunkSize)
{
if (i + chunkSize > messageLenght) chunkSize = messageLenght - i;
string TelegramMessage = Uri.EscapeDataString(message.Substring(i, chunkSize));
var httpRequestOptions = new HttpRequestOptions
{
Url = "https://api.telegram.org/bot" + options.BotToken + "/sendmessage?chat_id=" + options.ChatID + "&text=" + TelegramMessage,
CancellationToken = CancellationToken.None
};
using (await _httpClient.Post(httpRequestOptions).ConfigureAwait(false))
{

using (await _httpClient.Post(_httpRequest).ConfigureAwait(false))
}
}
}
else
{
string TelegramMessage = Uri.EscapeDataString(message);
var httpRequestOptions = new HttpRequestOptions
{
Url = "https://api.telegram.org/bot" + options.BotToken + "/sendmessage?chat_id=" + options.ChatID + "&text=" + TelegramMessage,
CancellationToken = CancellationToken.None
};

using (await _httpClient.Post(httpRequestOptions).ConfigureAwait(false))
{

}
}

}

private bool IsValid(TeleGramOptions options)
Expand Down

0 comments on commit 4c5a7e4

Please sign in to comment.