@ -3,6 +3,8 @@ using System.Collections.Generic;
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 ;
}
}
@ -26,65 +30,51 @@ namespace NzbDrone.Core.Notifications.Slack
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 . Series . Title ,
{
Text = message . Message ,
new Attachment
Color = "warning"
{
}
Fallback = message . Message ,
} ;
Title = message . Series . Title ,
var payload = CreatePayload ( $"Grabbed: {message.Message}" , attachments ) ;
Text = message . Message ,
Color = "warning"
_proxy . SendPayload ( payload , Settings ) ;
}
}
} ;
NotifySlack ( payload ) ;
}
}
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 . Series . Title ,
{
Text = message . Message ,
new Attachment
Color = "good"
{
}
Fallback = message . Message ,
} ;
Title = message . Series . Title ,
var payload = CreatePayload ( $"Imported: {message.Message}" , attachments ) ;
Text = message . Message ,
Color = "good"
_proxy . SendPayload ( payload , Settings ) ;
}
}
} ;
NotifySlack ( payload ) ;
}
}
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
{
var payload = CreatePayload ( "Renamed" , attachments ) ;
Title = series . Title ,
}
}
} ;
NotifySlack( payload ) ;
_proxy . SendPayload ( payload , Settings ) ;
}
}
public override ValidationResult Test ( )
public override ValidationResult Test ( )
@ -101,14 +91,9 @@ namespace NzbDrone.Core.Notifications.Slack
try
try
{
{
var message = $"Test message from Sonarr posted at {DateTime.Now}" ;
var message = $"Test message from Sonarr posted at {DateTime.Now}" ;
var payload = new SlackPayload
var payload = CreatePayload ( message ) ;
{
IconEmoji = Settings . Icon ,
Username = Settings . Username ,
Text = message
} ;
NotifySlack( payload ) ;
_proxy . SendPayload ( payload , Settings ) ;
}
}
catch ( SlackExeption ex )
catch ( SlackExeption ex )
@ -119,24 +104,28 @@ namespace NzbDrone.Core.Notifications.Slack
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
RequestFormat = DataFormat . Json ,
} ;
JsonSerializer = new JsonNetSerializer ( )
} ;
// Set the correct icon based on the value
request . AddBody ( payload ) ;
if ( icon . StartsWith ( ":" ) & & icon . EndsWith ( ":" ) )
client . ExecuteAndValidate ( request ) ;
{
payload . IconEmoji = icon ;
}
}
catch ( RestException ex )
else if ( icon . IsNotNullOrWhiteSpace ( ) )
{
{
_logger . Error ( ex , "Unable to post payload {0}" , payload ) ;
payload . IconUrl = icon ;
throw new SlackExeption ( "Unable to post payload" , ex ) ;
}
}
return payload ;
}
}
}
}
}
}