1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

New: Discord Notifications

Closes #1511
This commit is contained in:
Mark McDowall 2019-03-16 13:56:29 -07:00
parent 7f99ac0efa
commit de0d0a3526
9 changed files with 264 additions and 8 deletions

View File

@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Notifications.Discord.Payloads;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Discord
{
public class Discord : NotificationBase<DiscordSettings>
{
private readonly IDiscordProxy _proxy;
public Discord(IDiscordProxy proxy)
{
_proxy = proxy;
}
public override string Name => "Discord";
public override string Link => "https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks";
public override void OnGrab(GrabMessage message)
{
var embeds = new List<Embed>
{
new Embed
{
Description = message.Message,
Title = message.Series.Title,
Text = message.Message,
Color = (int)DiscordColors.Warning
}
};
var payload = CreatePayload($"Grabbed: {message.Message}", embeds);
_proxy.SendPayload(payload, Settings);
}
public override void OnDownload(DownloadMessage message)
{
var embeds = new List<Embed>
{
new Embed
{
Description = message.Message,
Title = message.Series.Title,
Text = message.Message,
Color = (int)DiscordColors.Success
}
};
var payload = CreatePayload($"Imported: {message.Message}", embeds);
_proxy.SendPayload(payload, Settings);
}
public override void OnRename(Series series)
{
var attachments = new List<Embed>
{
new Embed
{
Title = series.Title,
}
};
var payload = CreatePayload("Renamed", attachments);
_proxy.SendPayload(payload, Settings);
}
public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(TestMessage());
return new ValidationResult(failures);
}
public ValidationFailure TestMessage()
{
try
{
var message = $"Test message from Sonarr posted at {DateTime.Now}";
var payload = CreatePayload(message);
_proxy.SendPayload(payload, Settings);
}
catch (DiscordException ex)
{
return new NzbDroneValidationFailure("Unable to post", ex.Message);
}
return null;
}
private DiscordPayload CreatePayload(string message, List<Embed> embeds = null)
{
var avatar = Settings.Avatar;
var payload = new DiscordPayload
{
Username = Settings.Username,
Content = message,
Embeds = embeds
};
if (avatar.IsNotNullOrWhiteSpace())
{
payload.AvatarUrl = avatar;
}
if (Settings.Username.IsNotNullOrWhiteSpace())
{
payload.Username = Settings.Username;
}
return payload;
}
}
}

View File

@ -0,0 +1,9 @@
namespace NzbDrone.Core.Notifications.Discord
{
public enum DiscordColors
{
Danger = 15749200,
Success = 2605644,
Warning = 16753920
}
}

View File

@ -0,0 +1,16 @@
using System;
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Notifications.Discord
{
class DiscordException : NzbDroneException
{
public DiscordException(string message) : base(message)
{
}
public DiscordException(string message, Exception innerException, params object[] args) : base(message, innerException, args)
{
}
}
}

View File

@ -0,0 +1,46 @@
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Notifications.Discord.Payloads;
using NzbDrone.Core.Rest;
namespace NzbDrone.Core.Notifications.Discord
{
public interface IDiscordProxy
{
void SendPayload(DiscordPayload payload, DiscordSettings settings);
}
public class DiscordProxy : IDiscordProxy
{
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
public DiscordProxy(IHttpClient httpClient, Logger logger)
{
_httpClient = httpClient;
_logger = logger;
}
public void SendPayload(DiscordPayload payload, DiscordSettings settings)
{
try
{
var request = new HttpRequestBuilder(settings.WebHookUrl)
.Accept(HttpAccept.Json)
.Build();
request.Method = HttpMethod.POST;
request.Headers.ContentType = "application/json";
request.SetContent(payload.ToJson());
_httpClient.Execute(request);
}
catch (RestException ex)
{
_logger.Error(ex, "Unable to post payload {0}", payload);
throw new DiscordException("Unable to post payload", ex);
}
}
}
}

View File

@ -0,0 +1,35 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Discord
{
public class DiscordSettingsValidator : AbstractValidator<DiscordSettings>
{
public DiscordSettingsValidator()
{
RuleFor(c => c.WebHookUrl).IsValidUrl();
}
}
public class DiscordSettings : IProviderConfig
{
private static readonly DiscordSettingsValidator Validator = new DiscordSettingsValidator();
[FieldDefinition(0, Label = "Webhook URL", HelpText = "Discord channel webhook url")]
public string WebHookUrl { get; set; }
[FieldDefinition(1, Label = "Username", HelpText = "The username to post as, defaults to Discord webhook default")]
public string Username { get; set; }
[FieldDefinition(2, Label = "Avatar", HelpText = "Change the avatar that is used for messages from this integration", Type = FieldType.Textbox)]
public string Avatar { get; set; }
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Notifications.Discord.Payloads
{
public class DiscordPayload
{
public string Content { get; set; }
public string Username { get; set; }
[JsonProperty("avatar_url")]
public string AvatarUrl { get; set; }
public List<Embed> Embeds { get; set; }
}
}

View File

@ -0,0 +1,10 @@
namespace NzbDrone.Core.Notifications.Discord.Payloads
{
public class Embed
{
public string Description { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public int Color { get; set; }
}
}

View File

@ -1,15 +1,10 @@
using System;
using System.Collections.Generic;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Notifications.Slack.Payloads;
using NzbDrone.Core.Rest;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Validation;
using RestSharp;
namespace NzbDrone.Core.Notifications.Slack
@ -17,12 +12,10 @@ namespace NzbDrone.Core.Notifications.Slack
public class Slack : NotificationBase<SlackSettings>
{
private readonly ISlackProxy _proxy;
private readonly Logger _logger;
public Slack(ISlackProxy proxy, Logger logger)
public Slack(ISlackProxy proxy)
{
_proxy = proxy;
_logger = logger;
}
public override string Name => "Slack";

View File

@ -779,6 +779,13 @@
<Compile Include="Languages\LanguagesBelowCutoff.cs" />
<Compile Include="MediaFiles\EpisodeImport\Aggregation\Aggregators\AggregateLanguage.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\AbsoluteEpisodeNumberSpecification.cs" />
<Compile Include="Notifications\Discord\Discord.cs" />
<Compile Include="Notifications\Discord\DiscordColors.cs" />
<Compile Include="Notifications\Discord\DiscordException.cs" />
<Compile Include="Notifications\Discord\DiscordProxy.cs" />
<Compile Include="Notifications\Discord\DiscordSettings.cs" />
<Compile Include="Notifications\Discord\Payloads\Embed.cs" />
<Compile Include="Notifications\Discord\Payloads\DiscordPayload.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvPinUrlResponse.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvSignInUrlResponse.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvPinResponse.cs" />