From b2b5aa1f795632311303256aa5d1271bbe07af1f Mon Sep 17 00:00:00 2001 From: Stevie Robinson Date: Mon, 9 Dec 2024 04:37:51 +0100 Subject: [PATCH] New: Optionally as Instance Name to Telegram notifications (cherry picked from commit 36633b5d08c19158f185c0fa5faabbaec607fcb5) Closes #10757 --- src/NzbDrone.Core/Localization/Core/en.json | 2 ++ .../Notifications/Telegram/Telegram.cs | 28 ++++++++++++++----- .../Notifications/Telegram/TelegramProxy.cs | 9 ++++-- .../Telegram/TelegramSettings.cs | 3 ++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 7b6c81d77..2f646215e 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -1234,6 +1234,8 @@ "NotificationsTelegramSettingsChatIdHelpText": "You must start a conversation with the bot or add it to your group to receive messages", "NotificationsTelegramSettingsIncludeAppName": "Include {appName} in Title", "NotificationsTelegramSettingsIncludeAppNameHelpText": "Optionally prefix message title with {appName} to differentiate notifications from different applications", + "NotificationsTelegramSettingsIncludeInstanceName": "Include Instance Name in Title", + "NotificationsTelegramSettingsIncludeInstanceNameHelpText": "Optionally include Instance name in notification", "NotificationsTelegramSettingsSendSilently": "Send Silently", "NotificationsTelegramSettingsSendSilentlyHelpText": "Sends the message silently. Users will receive a notification with no sound", "NotificationsTelegramSettingsTopicId": "Topic ID", diff --git a/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs b/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs index 95914da87..71608aebf 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs @@ -2,6 +2,7 @@ using System.Linq; using FluentValidation.Results; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Movies; namespace NzbDrone.Core.Notifications.Telegram @@ -9,41 +10,48 @@ namespace NzbDrone.Core.Notifications.Telegram public class Telegram : NotificationBase { private readonly ITelegramProxy _proxy; + private readonly IConfigFileProvider _configFileProvider; - public Telegram(ITelegramProxy proxy) + public Telegram(ITelegramProxy proxy, IConfigFileProvider configFileProvider) { _proxy = proxy; + _configFileProvider = configFileProvider; } public override string Name => "Telegram"; public override string Link => "https://telegram.org/"; + private string InstanceName => _configFileProvider.InstanceName; + public override void OnGrab(GrabMessage grabMessage) { var title = Settings.IncludeAppNameInTitle ? MOVIE_GRABBED_TITLE_BRANDED : MOVIE_GRABBED_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, grabMessage.Message, Settings); } public override void OnDownload(DownloadMessage message) { + string title; if (message.OldMovieFiles.Any()) { - var title = Settings.IncludeAppNameInTitle ? MOVIE_UPGRADED_TITLE_BRANDED : MOVIE_UPGRADED_TITLE; - - _proxy.SendNotification(title, message.Message, Settings); + title = Settings.IncludeAppNameInTitle ? MOVIE_UPGRADED_TITLE_BRANDED : MOVIE_UPGRADED_TITLE; } else { - var title = Settings.IncludeAppNameInTitle ? MOVIE_DOWNLOADED_TITLE_BRANDED : MOVIE_DOWNLOADED_TITLE; - - _proxy.SendNotification(title, message.Message, Settings); + title = Settings.IncludeAppNameInTitle ? MOVIE_DOWNLOADED_TITLE_BRANDED : MOVIE_DOWNLOADED_TITLE; } + + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; + + _proxy.SendNotification(title, message.Message, Settings); } public override void OnMovieAdded(Movie movie) { var title = Settings.IncludeAppNameInTitle ? MOVIE_ADDED_TITLE_BRANDED : MOVIE_ADDED_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, $"{movie.Title} added to library", Settings); } @@ -51,6 +59,7 @@ public override void OnMovieAdded(Movie movie) public override void OnMovieFileDelete(MovieFileDeleteMessage deleteMessage) { var title = Settings.IncludeAppNameInTitle ? MOVIE_FILE_DELETED_TITLE_BRANDED : MOVIE_FILE_DELETED_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, deleteMessage.Message, Settings); } @@ -58,6 +67,7 @@ public override void OnMovieFileDelete(MovieFileDeleteMessage deleteMessage) public override void OnMovieDelete(MovieDeleteMessage deleteMessage) { var title = Settings.IncludeAppNameInTitle ? MOVIE_DELETED_TITLE_BRANDED : MOVIE_DELETED_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, deleteMessage.Message, Settings); } @@ -65,6 +75,7 @@ public override void OnMovieDelete(MovieDeleteMessage deleteMessage) public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) { var title = Settings.IncludeAppNameInTitle ? HEALTH_ISSUE_TITLE_BRANDED : HEALTH_ISSUE_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, healthCheck.Message, Settings); } @@ -72,6 +83,7 @@ public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) public override void OnHealthRestored(HealthCheck.HealthCheck previousCheck) { var title = Settings.IncludeAppNameInTitle ? HEALTH_RESTORED_TITLE_BRANDED : HEALTH_RESTORED_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, $"The following issue is now resolved: {previousCheck.Message}", Settings); } @@ -79,6 +91,7 @@ public override void OnHealthRestored(HealthCheck.HealthCheck previousCheck) public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) { var title = Settings.IncludeAppNameInTitle ? APPLICATION_UPDATE_TITLE_BRANDED : APPLICATION_UPDATE_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, updateMessage.Message, Settings); } @@ -86,6 +99,7 @@ public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) public override void OnManualInteractionRequired(ManualInteractionRequiredMessage message) { var title = Settings.IncludeAppNameInTitle ? MANUAL_INTERACTION_REQUIRED_TITLE_BRANDED : MANUAL_INTERACTION_REQUIRED_TITLE; + title = Settings.IncludeInstanceNameInTitle ? $"{title} - {InstanceName}" : title; _proxy.SendNotification(title, message.Message, Settings); } diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs index 56db18848..2edb9110f 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs @@ -7,6 +7,7 @@ using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Common.Serializer; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Localization; namespace NzbDrone.Core.Notifications.Telegram @@ -21,12 +22,14 @@ public class TelegramProxy : ITelegramProxy { private const string URL = "https://api.telegram.org"; private readonly IHttpClient _httpClient; + private readonly IConfigFileProvider _configFileProvider; private readonly ILocalizationService _localizationService; private readonly Logger _logger; - public TelegramProxy(IHttpClient httpClient, ILocalizationService localizationService, Logger logger) + public TelegramProxy(IHttpClient httpClient, IConfigFileProvider configFileProvider, ILocalizationService localizationService, Logger logger) { _httpClient = httpClient; + _configFileProvider = configFileProvider; _localizationService = localizationService; _logger = logger; } @@ -57,7 +60,9 @@ public ValidationFailure Test(TelegramSettings settings) const string title = "Test Notification"; const string body = "This is a test message from Radarr"; - SendNotification(settings.IncludeAppNameInTitle ? brandedTitle : title, body, settings); + var testMessageTitle = settings.IncludeAppNameInTitle ? brandedTitle : title; + testMessageTitle = settings.IncludeInstanceNameInTitle ? $"{testMessageTitle} - {_configFileProvider.InstanceName}" : testMessageTitle; + SendNotification(testMessageTitle, body, settings); } catch (Exception ex) { diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs index f3e4d2499..270163513 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs @@ -34,6 +34,9 @@ public class TelegramSettings : NotificationSettingsBase [FieldDefinition(4, Label = "NotificationsTelegramSettingsIncludeAppName", Type = FieldType.Checkbox, HelpText = "NotificationsTelegramSettingsIncludeAppNameHelpText")] public bool IncludeAppNameInTitle { get; set; } + [FieldDefinition(5, Label = "NotificationsTelegramSettingsIncludeInstanceName", Type = FieldType.Checkbox, HelpText = "NotificationsTelegramSettingsIncludeInstanceNameHelpText", Advanced = true)] + public bool IncludeInstanceNameInTitle { get; set; } + public override NzbDroneValidationResult Validate() { return new NzbDroneValidationResult(Validator.Validate(this));