From 6fec6edc3b323d37cee4236af5696af8da2c23ba Mon Sep 17 00:00:00 2001 From: Ivar Stangeby Date: Sat, 9 Mar 2024 00:16:12 +0100 Subject: [PATCH] New: Add metadata links to telegram messages Closes #5342 --- src/NzbDrone.Core/Localization/Core/en.json | 2 + .../Notifications/Telegram/Telegram.cs | 39 ++++++++++++++++++- .../Notifications/Telegram/TelegramProxy.cs | 4 +- .../Telegram/TelegramSettings.cs | 26 ++++++++++++- 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 01456d5b0..01fb12483 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -1433,6 +1433,8 @@ "NotificationsTelegramSettingsSendSilentlyHelpText": "Sends the message silently. Users will receive a notification with no sound", "NotificationsTelegramSettingsTopicId": "Topic ID", "NotificationsTelegramSettingsTopicIdHelpText": "Specify a Topic ID to send notifications to that topic. Leave blank to use the general topic (Supergroups only)", + "NotificationsTelegramSettingsSendMetadataLink": "Add a link to the series metadata", + "NotificationsTelegramSettingsSendMetadataLinkHelpText": "Add a link to the series metadata when sending the notifications", "NotificationsTraktSettingsAccessToken": "Access Token", "NotificationsTraktSettingsAuthUser": "Auth User", "NotificationsTraktSettingsAuthenticateWithTrakt": "Authenticate with Trakt", diff --git a/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs b/src/NzbDrone.Core/Notifications/Telegram/Telegram.cs index 3a8513e27..a4b9ffd6c 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 { @@ -47,15 +48,17 @@ namespace NzbDrone.Core.Notifications.Telegram public override void OnSeriesAdd(SeriesAddMessage message) { var title = Settings.IncludeAppNameInTitle ? SERIES_ADDED_TITLE_BRANDED : SERIES_ADDED_TITLE; + var text = FormatMessageWithLink(message.Message, message.Series); - _proxy.SendNotification(title, message.Message, Settings); + _proxy.SendNotification(title, text, Settings); } public override void OnSeriesDelete(SeriesDeleteMessage deleteMessage) { var title = Settings.IncludeAppNameInTitle ? SERIES_DELETED_TITLE_BRANDED : SERIES_DELETED_TITLE; + var text = FormatMessageWithLink(deleteMessage.Message, deleteMessage.Series); - _proxy.SendNotification(title, deleteMessage.Message, Settings); + _proxy.SendNotification(title, text, Settings); } public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) @@ -94,5 +97,37 @@ namespace NzbDrone.Core.Notifications.Telegram return new ValidationResult(failures); } + + private string FormatMessageWithLink(string message, Series series) + { + var linkType = Settings.MetadataLinkType; + + if (linkType == MetadataLinkType.None) + { + return message; + } + + if (linkType == MetadataLinkType.Imdb && series.ImdbId.IsNotNullOrWhiteSpace()) + { + return $"[{message}](https://www.imdb.com/title/{series.ImdbId})"; + } + + if (linkType == MetadataLinkType.Tvdb && series.TvdbId > 0) + { + return $"[{message}](http://www.thetvdb.com/?tab=series&id={series.TvdbId})"; + } + + if (linkType == MetadataLinkType.Trakt && series.TvdbId > 0) + { + return $"[{message}](http://trakt.tv/search/tvdb/{series.TvdbId}?id_type=show)"; + } + + if (linkType == MetadataLinkType.Tvmaze && series.TvMazeId > 0) + { + return $"[{message}](http://www.tvmaze.com/shows/{series.TvMazeId}/_)"; + } + + return message; + } } } diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs index f1cc39f1a..465aff9ed 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramProxy.cs @@ -35,13 +35,13 @@ namespace NzbDrone.Core.Notifications.Telegram public void SendNotification(string title, string message, TelegramSettings settings) { // Format text to add the title before and bold using markdown - var text = $"{HttpUtility.HtmlEncode(title)}\n{HttpUtility.HtmlEncode(message)}"; + var text = $"*{HttpUtility.HtmlEncode(title)}*\n{HttpUtility.HtmlEncode(message)}"; var requestBuilder = new HttpRequestBuilder(URL).Resource("bot{token}/sendmessage").Post(); var request = requestBuilder.SetSegment("token", settings.BotToken) .AddFormParameter("chat_id", settings.ChatId) - .AddFormParameter("parse_mode", "HTML") + .AddFormParameter("parse_mode", "MarkdownV2") .AddFormParameter("text", text) .AddFormParameter("disable_notification", settings.SendSilently) .AddFormParameter("message_thread_id", settings.TopicId) diff --git a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs index f3e4d2499..4ff9d9573 100644 --- a/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs +++ b/src/NzbDrone.Core/Notifications/Telegram/TelegramSettings.cs @@ -1,7 +1,7 @@ +using System; using FluentValidation; using NzbDrone.Core.Annotations; using NzbDrone.Core.Validation; - namespace NzbDrone.Core.Notifications.Telegram { public class TelegramSettingsValidator : AbstractValidator @@ -12,6 +12,13 @@ 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.MetadataLinkType).Custom((metadataLinkType, context) => + { + if (!Enum.IsDefined(typeof(MetadataLinkType), metadataLinkType)) + { + context.AddFailure($"MetadataLinkType is not valid: {0}"); + } + }); } } @@ -34,9 +41,26 @@ namespace NzbDrone.Core.Notifications.Telegram [FieldDefinition(4, Label = "NotificationsTelegramSettingsIncludeAppName", Type = FieldType.Checkbox, HelpText = "NotificationsTelegramSettingsIncludeAppNameHelpText")] public bool IncludeAppNameInTitle { get; set; } + [FieldDefinition(5, Label = "NotificationsTelegramSettingsMetadataLinkType", Type = FieldType.Select, SelectOptions = typeof(MetadataLinkType), HelpText = "NotificationsTelegramSettingsMetadataLinkType")] + public MetadataLinkType MetadataLinkType { get; set; } + public override NzbDroneValidationResult Validate() { return new NzbDroneValidationResult(Validator.Validate(this)); } } + + public enum MetadataLinkType + { + [FieldOption(Label = "None")] + None = 0, + [FieldOption(Label = "IMDb")] + Imdb, + [FieldOption(Label = "TVDb")] + Tvdb, + [FieldOption(Label = "TVMaze")] + Tvmaze, + [FieldOption(Label = "Trakt")] + Trakt, + } }