diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 01456d5b0..a36ccadec 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -1429,6 +1429,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", + "NotificationsTelegramSettingsMetadataLinks": "Metadata Links", + "NotificationsTelegramSettingsMetadataLinksHelpText": "Add a links to series metadata when sending notifications", "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 3a8513e27..4d23c3ac9 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using FluentValidation.Results; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Tv; namespace NzbDrone.Core.Notifications.Telegram { @@ -19,71 +20,77 @@ namespace NzbDrone.Core.Notifications.Telegram public override void OnGrab(GrabMessage grabMessage) { var title = Settings.IncludeAppNameInTitle ? EPISODE_GRABBED_TITLE_BRANDED : EPISODE_GRABBED_TITLE; + var links = GetLinks(grabMessage.Series); - _proxy.SendNotification(title, grabMessage.Message, Settings); + _proxy.SendNotification(title, grabMessage.Message, links, Settings); } public override void OnDownload(DownloadMessage message) { var title = Settings.IncludeAppNameInTitle ? EPISODE_DOWNLOADED_TITLE_BRANDED : EPISODE_DOWNLOADED_TITLE; + var links = GetLinks(message.Series); - _proxy.SendNotification(title, message.Message, Settings); + _proxy.SendNotification(title, message.Message, links, Settings); } public override void OnImportComplete(ImportCompleteMessage message) { var title = Settings.IncludeAppNameInTitle ? EPISODE_DOWNLOADED_TITLE_BRANDED : EPISODE_DOWNLOADED_TITLE; + var links = GetLinks(message.Series); - _proxy.SendNotification(title, message.Message, Settings); + _proxy.SendNotification(title, message.Message, links, Settings); } public override void OnEpisodeFileDelete(EpisodeDeleteMessage deleteMessage) { var title = Settings.IncludeAppNameInTitle ? EPISODE_DELETED_TITLE_BRANDED : EPISODE_DELETED_TITLE; + var links = GetLinks(deleteMessage.Series); - _proxy.SendNotification(title, deleteMessage.Message, Settings); + _proxy.SendNotification(title, deleteMessage.Message, links, Settings); } public override void OnSeriesAdd(SeriesAddMessage message) { var title = Settings.IncludeAppNameInTitle ? SERIES_ADDED_TITLE_BRANDED : SERIES_ADDED_TITLE; + var links = GetLinks(message.Series); - _proxy.SendNotification(title, message.Message, Settings); + _proxy.SendNotification(title, message.Message, links, Settings); } public override void OnSeriesDelete(SeriesDeleteMessage deleteMessage) { var title = Settings.IncludeAppNameInTitle ? SERIES_DELETED_TITLE_BRANDED : SERIES_DELETED_TITLE; + var links = GetLinks(deleteMessage.Series); - _proxy.SendNotification(title, deleteMessage.Message, Settings); + _proxy.SendNotification(title, deleteMessage.Message, links, Settings); } public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) { var title = Settings.IncludeAppNameInTitle ? HEALTH_ISSUE_TITLE_BRANDED : HEALTH_ISSUE_TITLE; - _proxy.SendNotification(title, healthCheck.Message, Settings); + _proxy.SendNotification(title, healthCheck.Message, null, Settings); } public override void OnHealthRestored(HealthCheck.HealthCheck previousCheck) { var title = Settings.IncludeAppNameInTitle ? HEALTH_RESTORED_TITLE_BRANDED : HEALTH_RESTORED_TITLE; - _proxy.SendNotification(title, $"The following issue is now resolved: {previousCheck.Message}", Settings); + _proxy.SendNotification(title, $"The following issue is now resolved: {previousCheck.Message}", null, Settings); } public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) { var title = Settings.IncludeAppNameInTitle ? APPLICATION_UPDATE_TITLE_BRANDED : APPLICATION_UPDATE_TITLE; - _proxy.SendNotification(title, updateMessage.Message, Settings); + _proxy.SendNotification(title, updateMessage.Message, null, Settings); } public override void OnManualInteractionRequired(ManualInteractionRequiredMessage message) { var title = Settings.IncludeAppNameInTitle ? MANUAL_INTERACTION_REQUIRED_TITLE_BRANDED : MANUAL_INTERACTION_REQUIRED_TITLE; - _proxy.SendNotification(title, message.Message, Settings); + _proxy.SendNotification(title, message.Message, null, Settings); } public override ValidationResult Test() @@ -94,5 +101,37 @@ namespace NzbDrone.Core.Notifications.Telegram return new ValidationResult(failures); } + + private List GetLinks(Series series) + { + var links = new List(); + + foreach (var link in Settings.MetadataLinks) + { + var linkType = (MetadataLinkType)link; + + if (linkType == MetadataLinkType.Imdb && series.ImdbId.IsNotNullOrWhiteSpace()) + { + links.Add(new TelegramLink("IMDb", $"https://www.imdb.com/title/{series.ImdbId}")); + } + + if (linkType == MetadataLinkType.Tvdb && series.TvdbId > 0) + { + links.Add(new TelegramLink("TVDb", $"http://www.thetvdb.com/?tab=series&id={series.TvdbId}")); + } + + if (linkType == MetadataLinkType.Trakt && series.TvdbId > 0) + { + links.Add(new TelegramLink("TVMaze", $"http://trakt.tv/search/tvdb/{series.TvdbId}?id_type=show")); + } + + if (linkType == MetadataLinkType.Tvmaze && series.TvMazeId > 0) + { + links.Add(new TelegramLink("Trakt", $"http://www.tvmaze.com/shows/{series.TvMazeId}/_")); + } + } + + return links; + } } } diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramLink.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramLink.cs new file mode 100644 index 000000000..ac131b483 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramLink.cs @@ -0,0 +1,14 @@ +namespace NzbDrone.Core.Notifications.Telegram +{ + public class TelegramLink + { + public string Label { get; set; } + public string Link { get; set; } + + public TelegramLink(string label, string link) + { + Label = label; + Link = link; + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs index f1cc39f1a..48f70761e 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Text; using System.Web; using FluentValidation.Results; using NLog; @@ -13,7 +14,7 @@ namespace NzbDrone.Core.Notifications.Telegram { public interface ITelegramProxy { - void SendNotification(string title, string message, TelegramSettings settings); + void SendNotification(string title, string message, List links, TelegramSettings settings); ValidationFailure Test(TelegramSettings settings); } @@ -32,10 +33,16 @@ namespace NzbDrone.Core.Notifications.Telegram _logger = logger; } - public void SendNotification(string title, string message, TelegramSettings settings) + public void SendNotification(string title, string message, List links, TelegramSettings settings) { - // Format text to add the title before and bold using markdown - var text = $"{HttpUtility.HtmlEncode(title)}\n{HttpUtility.HtmlEncode(message)}"; + var text = new StringBuilder($"{HttpUtility.HtmlEncode(title)}\n"); + + text.AppendLine(HttpUtility.HtmlEncode(message)); + + foreach (var link in links) + { + text.AppendLine($"{HttpUtility.HtmlEncode(link.Label)}"); + } var requestBuilder = new HttpRequestBuilder(URL).Resource("bot{token}/sendmessage").Post(); @@ -58,7 +65,12 @@ namespace NzbDrone.Core.Notifications.Telegram const string title = "Test Notification"; const string body = "This is a test message from Sonarr"; - SendNotification(settings.IncludeAppNameInTitle ? brandedTitle : title, body, settings); + var links = new List + { + new TelegramLink("Sonarr.tv", "https://sonarr.tv") + }; + + SendNotification(settings.IncludeAppNameInTitle ? brandedTitle : title, body, links, settings); } catch (Exception ex) { diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs index f3e4d2499..6ec3d4d15 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs @@ -1,7 +1,9 @@ +using System; +using System.Collections.Generic; +using System.Linq; using FluentValidation; using NzbDrone.Core.Annotations; using NzbDrone.Core.Validation; - namespace NzbDrone.Core.Notifications.Telegram { public class TelegramSettingsValidator : AbstractValidator @@ -12,6 +14,16 @@ namespace NzbDrone.Core.Notifications.Telegram 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"); + RuleFor(c => c.MetadataLinks).Custom((links, context) => + { + foreach (var link in links) + { + if (!Enum.IsDefined(typeof(MetadataLinkType), link)) + { + context.AddFailure("MetadataLinks", $"MetadataLink is not valid: {link}"); + } + } + }); } } @@ -19,6 +31,11 @@ namespace NzbDrone.Core.Notifications.Telegram { private static readonly TelegramSettingsValidator Validator = new (); + public TelegramSettings() + { + MetadataLinks = Enumerable.Empty(); + } + [FieldDefinition(0, Label = "NotificationsTelegramSettingsBotToken", Privacy = PrivacyLevel.ApiKey, HelpLink = "https://core.telegram.org/bots")] public string BotToken { get; set; } @@ -34,9 +51,27 @@ namespace NzbDrone.Core.Notifications.Telegram [FieldDefinition(4, Label = "NotificationsTelegramSettingsIncludeAppName", Type = FieldType.Checkbox, HelpText = "NotificationsTelegramSettingsIncludeAppNameHelpText")] public bool IncludeAppNameInTitle { get; set; } + [FieldDefinition(5, Label = "NotificationsTelegramSettingsMetadataLinks", Type = FieldType.Select, SelectOptions = typeof(MetadataLinkType), HelpText = "NotificationsTelegramSettingsMetadataLinksHelpText")] + public IEnumerable MetadataLinks { get; set; } + public override NzbDroneValidationResult Validate() { return new NzbDroneValidationResult(Validator.Validate(this)); } } + + public enum MetadataLinkType + { + [FieldOption(Label = "IMDb")] + Imdb, + + [FieldOption(Label = "TVDb")] + Tvdb, + + [FieldOption(Label = "TVMaze")] + Tvmaze, + + [FieldOption(Label = "Trakt")] + Trakt, + } }