From 0a0ddf2b02ddf8077dfc8a34d0c6944f7b6ac063 Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 15 Apr 2023 01:47:06 +0200 Subject: [PATCH] New: Option to use Telegram topics for notifications (cherry picked from commit c8933d812490ba375c6f7e07cd4355921dc513ac) --- .../Notifications/Telegram/TelegramService.cs | 12 +++++++++++- .../Notifications/Telegram/TelegramSettings.cs | 9 +++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramService.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramService.cs index cc09b80ce..9bd2fd7d7 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramService.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramService.cs @@ -39,6 +39,7 @@ namespace NzbDrone.Core.Notifications.Telegram .AddFormParameter("parse_mode", "HTML") .AddFormParameter("text", text) .AddFormParameter("disable_notification", settings.SendSilently) + .AddFormParameter("message_thread_id", settings.TopicId) .Build(); _httpClient.Post(request); @@ -64,7 +65,16 @@ namespace NzbDrone.Core.Notifications.Telegram else if (ex is Common.Http.HttpException restException && restException.Response.StatusCode == HttpStatusCode.BadRequest) { var error = Json.Deserialize(restException.Response.Content); - var property = error.Description.ContainsIgnoreCase("chat not found") ? "ChatId" : "BotToken"; + var property = "BotToken"; + + if (error.Description.ContainsIgnoreCase("chat not found") || error.Description.ContainsIgnoreCase("group chat was upgraded to a supergroup chat")) + { + property = "ChatId"; + } + else if (error.Description.ContainsIgnoreCase("message thread not found")) + { + property = "TopicId"; + } return new ValidationFailure(property, error.Description); } diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs index 824821ea4..774fd4ca0 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs @@ -1,4 +1,4 @@ -using FluentValidation; +using FluentValidation; using NzbDrone.Core.Annotations; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; @@ -11,6 +11,8 @@ namespace NzbDrone.Core.Notifications.Telegram { RuleFor(c => c.BotToken).NotEmpty(); RuleFor(c => c.ChatId).NotEmpty(); + RuleFor(c => c.TopicId).Must(topicId => !topicId.HasValue || topicId > 1) + .WithMessage("Topic ID must be greater than 1 or empty"); } } @@ -24,7 +26,10 @@ namespace NzbDrone.Core.Notifications.Telegram [FieldDefinition(1, Label = "Chat ID", HelpLink = "http://stackoverflow.com/a/37396871/882971", HelpText = "You must start a conversation with the bot or add it to your group to receive messages")] public string ChatId { get; set; } - [FieldDefinition(2, Label = "Send Silently", Type = FieldType.Checkbox, HelpText = "Sends the message silently. Users will receive a notification with no sound")] + [FieldDefinition(2, Label = "Topic ID", HelpLink = "https://stackoverflow.com/a/75178418", HelpText = "Specify a Topic ID to send notifications to that topic. Leave blank to use the general topic (Supergroups only)")] + public int? TopicId { get; set; } + + [FieldDefinition(3, Label = "Send Silently", Type = FieldType.Checkbox, HelpText = "Sends the message silently. Users will receive a notification with no sound")] public bool SendSilently { get; set; } public NzbDroneValidationResult Validate()