New: Add metadata links to telegram messages

Closes #5342
This commit is contained in:
Ivar Stangeby 2024-03-09 00:16:12 +01:00 committed by Mark McDowall
parent 72db8099e0
commit 6fec6edc3b
4 changed files with 66 additions and 5 deletions

View File

@ -1433,6 +1433,8 @@
"NotificationsTelegramSettingsSendSilentlyHelpText": "Sends the message silently. Users will receive a notification with no sound", "NotificationsTelegramSettingsSendSilentlyHelpText": "Sends the message silently. Users will receive a notification with no sound",
"NotificationsTelegramSettingsTopicId": "Topic ID", "NotificationsTelegramSettingsTopicId": "Topic ID",
"NotificationsTelegramSettingsTopicIdHelpText": "Specify a Topic ID to send notifications to that topic. Leave blank to use the general topic (Supergroups only)", "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", "NotificationsTraktSettingsAccessToken": "Access Token",
"NotificationsTraktSettingsAuthUser": "Auth User", "NotificationsTraktSettingsAuthUser": "Auth User",
"NotificationsTraktSettingsAuthenticateWithTrakt": "Authenticate with Trakt", "NotificationsTraktSettingsAuthenticateWithTrakt": "Authenticate with Trakt",

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Notifications.Telegram namespace NzbDrone.Core.Notifications.Telegram
{ {
@ -47,15 +48,17 @@ namespace NzbDrone.Core.Notifications.Telegram
public override void OnSeriesAdd(SeriesAddMessage message) public override void OnSeriesAdd(SeriesAddMessage message)
{ {
var title = Settings.IncludeAppNameInTitle ? SERIES_ADDED_TITLE_BRANDED : SERIES_ADDED_TITLE; 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) public override void OnSeriesDelete(SeriesDeleteMessage deleteMessage)
{ {
var title = Settings.IncludeAppNameInTitle ? SERIES_DELETED_TITLE_BRANDED : SERIES_DELETED_TITLE; 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) public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
@ -94,5 +97,37 @@ namespace NzbDrone.Core.Notifications.Telegram
return new ValidationResult(failures); 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;
}
} }
} }

View File

@ -35,13 +35,13 @@ namespace NzbDrone.Core.Notifications.Telegram
public void SendNotification(string title, string message, TelegramSettings settings) public void SendNotification(string title, string message, TelegramSettings settings)
{ {
// Format text to add the title before and bold using markdown // Format text to add the title before and bold using markdown
var text = $"<b>{HttpUtility.HtmlEncode(title)}</b>\n{HttpUtility.HtmlEncode(message)}"; var text = $"*{HttpUtility.HtmlEncode(title)}*\n{HttpUtility.HtmlEncode(message)}";
var requestBuilder = new HttpRequestBuilder(URL).Resource("bot{token}/sendmessage").Post(); var requestBuilder = new HttpRequestBuilder(URL).Resource("bot{token}/sendmessage").Post();
var request = requestBuilder.SetSegment("token", settings.BotToken) var request = requestBuilder.SetSegment("token", settings.BotToken)
.AddFormParameter("chat_id", settings.ChatId) .AddFormParameter("chat_id", settings.ChatId)
.AddFormParameter("parse_mode", "HTML") .AddFormParameter("parse_mode", "MarkdownV2")
.AddFormParameter("text", text) .AddFormParameter("text", text)
.AddFormParameter("disable_notification", settings.SendSilently) .AddFormParameter("disable_notification", settings.SendSilently)
.AddFormParameter("message_thread_id", settings.TopicId) .AddFormParameter("message_thread_id", settings.TopicId)

View File

@ -1,7 +1,7 @@
using System;
using FluentValidation; using FluentValidation;
using NzbDrone.Core.Annotations; using NzbDrone.Core.Annotations;
using NzbDrone.Core.Validation; using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Telegram namespace NzbDrone.Core.Notifications.Telegram
{ {
public class TelegramSettingsValidator : AbstractValidator<TelegramSettings> public class TelegramSettingsValidator : AbstractValidator<TelegramSettings>
@ -12,6 +12,13 @@ namespace NzbDrone.Core.Notifications.Telegram
RuleFor(c => c.ChatId).NotEmpty(); RuleFor(c => c.ChatId).NotEmpty();
RuleFor(c => c.TopicId).Must(topicId => !topicId.HasValue || topicId > 1) RuleFor(c => c.TopicId).Must(topicId => !topicId.HasValue || topicId > 1)
.WithMessage("Topic ID must be greater than 1 or empty"); .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")] [FieldDefinition(4, Label = "NotificationsTelegramSettingsIncludeAppName", Type = FieldType.Checkbox, HelpText = "NotificationsTelegramSettingsIncludeAppNameHelpText")]
public bool IncludeAppNameInTitle { get; set; } 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() public override NzbDroneValidationResult Validate()
{ {
return new NzbDroneValidationResult(Validator.Validate(this)); 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,
}
} }