1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed: Slack/Mattermost notifications improvements from Sonarr (#1930)

This commit is contained in:
James White 2017-08-13 18:42:00 +01:00 committed by Leonardo Galli
parent e155585198
commit 6500edbd14
5 changed files with 126 additions and 87 deletions

View File

@ -12,6 +12,9 @@ public class SlackPayload
[JsonProperty("icon_emoji")] [JsonProperty("icon_emoji")]
public string IconEmoji { get; set; } public string IconEmoji { get; set; }
[JsonProperty("icon_url")]
public string IconUrl { get; set; }
public List<Attachment> Attachments { get; set; } public List<Attachment> Attachments { get; set; }
} }
} }

View File

@ -3,6 +3,8 @@
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Notifications.Slack.Payloads; using NzbDrone.Core.Notifications.Slack.Payloads;
using NzbDrone.Core.Rest; using NzbDrone.Core.Rest;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -14,10 +16,12 @@ namespace NzbDrone.Core.Notifications.Slack
{ {
public class Slack : NotificationBase<SlackSettings> public class Slack : NotificationBase<SlackSettings>
{ {
private readonly ISlackProxy _proxy;
private readonly Logger _logger; private readonly Logger _logger;
public Slack(Logger logger) public Slack(ISlackProxy proxy, Logger logger)
{ {
_proxy = proxy;
_logger = logger; _logger = logger;
} }
@ -27,86 +31,68 @@ public Slack(Logger logger)
public override void OnGrab(GrabMessage message) public override void OnGrab(GrabMessage message)
{ {
var payload = new SlackPayload var attachments = new List<Attachment>
{ {
IconEmoji = Settings.Icon, new Attachment
Username = Settings.Username, {
Text = $"Grabbed: {message.Message}", Fallback = message.Message,
Attachments = new List<Attachment> Title = message.Movie.Title,
{ Text = message.Message,
new Attachment Color = "warning"
{ }
Fallback = message.Message, };
Title = message.Movie.Title, var payload = CreatePayload($"Grabbed: {message.Message}", attachments);
Text = message.Message,
Color = "warning"
}
}
};
NotifySlack(payload); _proxy.SendPayload(payload, Settings);
} }
public override void OnDownload(DownloadMessage message) public override void OnDownload(DownloadMessage message)
{ {
var payload = new SlackPayload var attachments = new List<Attachment>
{ {
IconEmoji = Settings.Icon, new Attachment
Username = Settings.Username, {
Text = $"Imported: {message.Message}", Fallback = message.Message,
Attachments = new List<Attachment> Title = message.Movie.Title,
{ Text = message.Message,
new Attachment Color = "good"
{ }
Fallback = message.Message, };
Title = message.Movie.Title, var payload = CreatePayload($"Imported: {message.Message}", attachments);
Text = message.Message,
Color = "good"
}
}
};
NotifySlack(payload); _proxy.SendPayload(payload, Settings);
} }
public override void OnMovieRename(Movie movie) public override void OnMovieRename(Movie movie)
{ {
var payload = new SlackPayload var attachments = new List<Attachment>
{ {
IconEmoji = Settings.Icon, new Attachment
Username = Settings.Username, {
Text = "Renamed", Title = movie.Title,
Attachments = new List<Attachment> }
{ };
new Attachment
{ var payload = CreatePayload("Renamed", attachments);
Title = movie.Title,
}
}
};
NotifySlack(payload); _proxy.SendPayload(payload, Settings);
} }
public override void OnRename(Series series) public override void OnRename(Series series)
{ {
var payload = new SlackPayload var attachments = new List<Attachment>
{ {
IconEmoji = Settings.Icon, new Attachment
Username = Settings.Username, {
Text = "Renamed", Title = series.Title,
Attachments = new List<Attachment> }
{ };
new Attachment
{
Title = series.Title,
}
}
};
NotifySlack(payload); var payload = CreatePayload("Renamed", attachments);
_proxy.SendPayload(payload, Settings);
} }
public override ValidationResult Test() public override ValidationResult Test()
{ {
var failures = new List<ValidationFailure>(); var failures = new List<ValidationFailure>();
@ -121,14 +107,10 @@ public ValidationFailure TestMessage()
try try
{ {
var message = $"Test message from Radarr posted at {DateTime.Now}"; var message = $"Test message from Radarr posted at {DateTime.Now}";
var payload = new SlackPayload
{
IconEmoji = Settings.Icon,
Username = Settings.Username,
Text = message
};
NotifySlack(payload); var payload = CreatePayload(message);
_proxy.SendPayload(payload, Settings);
} }
catch (SlackExeption ex) catch (SlackExeption ex)
@ -139,24 +121,31 @@ public ValidationFailure TestMessage()
return null; return null;
} }
private void NotifySlack(SlackPayload payload) private SlackPayload CreatePayload(string message, List<Attachment> attachments = null)
{ {
try var icon = Settings.Icon;
var payload = new SlackPayload
{ {
var client = RestClientFactory.BuildClient(Settings.WebHookUrl); Username = Settings.Username,
var request = new RestRequest(Method.POST) Text = message,
Attachments = attachments
};
if (icon.IsNotNullOrWhiteSpace())
{
// Set the correct icon based on the value
if (icon.StartsWith(":") && icon.EndsWith(":"))
{ {
RequestFormat = DataFormat.Json, payload.IconEmoji = icon;
JsonSerializer = new JsonNetSerializer() }
}; else
request.AddBody(payload); {
client.ExecuteAndValidate(request); payload.IconUrl = icon;
} }
catch (RestException ex)
{
_logger.Error(ex, "Unable to post payload {0}", payload);
throw new SlackExeption("Unable to post payload", ex);
} }
return payload;
} }
} }
} }

View File

@ -0,0 +1,46 @@
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Notifications.Slack.Payloads;
using NzbDrone.Core.Rest;
namespace NzbDrone.Core.Notifications.Slack
{
public interface ISlackProxy
{
void SendPayload(SlackPayload payload, SlackSettings settings);
}
public class SlackProxy : ISlackProxy
{
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
public SlackProxy(IHttpClient httpClient, Logger logger)
{
_httpClient = httpClient;
_logger = logger;
}
public void SendPayload(SlackPayload payload, SlackSettings 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 SlackExeption("Unable to post payload", ex);
}
}
}
}

View File

@ -24,7 +24,7 @@ public class SlackSettings : IProviderConfig
[FieldDefinition(1, Label = "Username", HelpText = "Choose the username that this integration will post as", Type = FieldType.Textbox)] [FieldDefinition(1, Label = "Username", HelpText = "Choose the username that this integration will post as", Type = FieldType.Textbox)]
public string Username { get; set; } public string Username { get; set; }
[FieldDefinition(2, Label = "Icon", HelpText = "Change the icon that is used for messages from this integration", Type = FieldType.Textbox, HelpLink = "http://www.emoji-cheat-sheet.com/")] [FieldDefinition(2, Label = "Icon", HelpText = "Change the icon that is used for messages from this integration (Emoji or URL)", Type = FieldType.Textbox, HelpLink = "http://www.emoji-cheat-sheet.com/")]
public string Icon { get; set; } public string Icon { get; set; }
public NzbDroneValidationResult Validate() public NzbDroneValidationResult Validate()

View File

@ -970,6 +970,7 @@
<Compile Include="Notifications\Slack\Payloads\SlackPayload.cs" /> <Compile Include="Notifications\Slack\Payloads\SlackPayload.cs" />
<Compile Include="Notifications\Slack\Slack.cs" /> <Compile Include="Notifications\Slack\Slack.cs" />
<Compile Include="Notifications\Slack\SlackExeption.cs" /> <Compile Include="Notifications\Slack\SlackExeption.cs" />
<Compile Include="Notifications\Slack\SlackProxy.cs" />
<Compile Include="Notifications\Slack\SlackSettings.cs" /> <Compile Include="Notifications\Slack\SlackSettings.cs" />
<Compile Include="Notifications\Synology\SynologyException.cs" /> <Compile Include="Notifications\Synology\SynologyException.cs" />
<Compile Include="Notifications\Synology\SynologyIndexer.cs" /> <Compile Include="Notifications\Synology\SynologyIndexer.cs" />