2017-09-23 01:59:24 +00:00
|
|
|
using System;
|
2016-03-22 18:37:06 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using FluentValidation.Results;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Extensions;
|
2017-10-29 02:35:50 +00:00
|
|
|
using NzbDrone.Common.Http;
|
|
|
|
using NzbDrone.Common.Serializer;
|
2016-03-22 18:37:06 +00:00
|
|
|
using NzbDrone.Core.Notifications.Slack.Payloads;
|
|
|
|
using NzbDrone.Core.Rest;
|
2017-09-23 01:59:24 +00:00
|
|
|
using NzbDrone.Core.Music;
|
2016-03-22 18:37:06 +00:00
|
|
|
using NzbDrone.Core.Validation;
|
|
|
|
using RestSharp;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications.Slack
|
|
|
|
{
|
|
|
|
public class Slack : NotificationBase<SlackSettings>
|
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
private readonly ISlackProxy _proxy;
|
2016-03-22 18:37:06 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2017-10-29 02:35:50 +00:00
|
|
|
public Slack(ISlackProxy proxy, Logger logger)
|
2016-03-22 18:37:06 +00:00
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
_proxy = proxy;
|
2016-03-22 18:37:06 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public override string Name => "Slack";
|
|
|
|
public override string Link => "https://my.slack.com/services/new/incoming-webhook/";
|
2016-03-22 18:37:06 +00:00
|
|
|
|
|
|
|
public override void OnGrab(GrabMessage message)
|
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
var attachments = new List<Attachment>
|
|
|
|
{
|
|
|
|
new Attachment
|
|
|
|
{
|
|
|
|
Fallback = message.Message,
|
|
|
|
Title = message.Artist.Name,
|
|
|
|
Text = message.Message,
|
|
|
|
Color = "warning"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var payload = CreatePayload($"Grabbed: {message.Message}", attachments);
|
|
|
|
|
|
|
|
_proxy.SendPayload(payload, Settings);
|
2016-03-22 18:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnDownload(DownloadMessage message)
|
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
var attachments = new List<Attachment>
|
|
|
|
{
|
|
|
|
new Attachment
|
|
|
|
{
|
|
|
|
Fallback = message.Message,
|
|
|
|
Title = message.Artist.Name,
|
|
|
|
Text = message.Message,
|
|
|
|
Color = "good"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var payload = CreatePayload($"Imported: {message.Message}", attachments);
|
|
|
|
|
|
|
|
_proxy.SendPayload(payload, Settings);
|
2016-03-22 18:37:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 01:59:24 +00:00
|
|
|
public override void OnRename(Artist artist)
|
2016-03-22 18:37:06 +00:00
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
var attachments = new List<Attachment>
|
|
|
|
{
|
|
|
|
new Attachment
|
|
|
|
{
|
|
|
|
Title = artist.Name,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var payload = CreatePayload("Renamed", attachments);
|
2016-03-22 18:37:06 +00:00
|
|
|
|
2017-10-29 02:35:50 +00:00
|
|
|
_proxy.SendPayload(payload, Settings);
|
2016-03-22 18:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override ValidationResult Test()
|
|
|
|
{
|
|
|
|
var failures = new List<ValidationFailure>();
|
|
|
|
|
|
|
|
failures.AddIfNotNull(TestMessage());
|
|
|
|
|
|
|
|
return new ValidationResult(failures);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ValidationFailure TestMessage()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-03-30 03:49:38 +00:00
|
|
|
var message = $"Test message from Lidarr posted at {DateTime.Now}";
|
2017-10-29 02:35:50 +00:00
|
|
|
var payload = CreatePayload(message);
|
2016-03-22 18:37:06 +00:00
|
|
|
|
2017-10-29 02:35:50 +00:00
|
|
|
_proxy.SendPayload(payload, Settings);
|
2016-03-22 18:37:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
catch (SlackExeption ex)
|
|
|
|
{
|
|
|
|
return new NzbDroneValidationFailure("Unable to post", ex.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-29 02:35:50 +00:00
|
|
|
private SlackPayload CreatePayload(string message, List<Attachment> attachments = null)
|
2016-03-22 18:37:06 +00:00
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
var icon = Settings.Icon;
|
2017-12-03 20:58:29 +00:00
|
|
|
var channel = Settings.Channel;
|
2017-10-29 02:35:50 +00:00
|
|
|
|
|
|
|
var payload = new SlackPayload
|
2016-03-22 18:37:06 +00:00
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
Username = Settings.Username,
|
|
|
|
Text = message,
|
|
|
|
Attachments = attachments
|
|
|
|
};
|
|
|
|
|
|
|
|
if (icon.IsNotNullOrWhiteSpace())
|
2016-03-22 18:37:06 +00:00
|
|
|
{
|
2017-10-29 02:35:50 +00:00
|
|
|
// Set the correct icon based on the value
|
|
|
|
if (icon.StartsWith(":") && icon.EndsWith(":"))
|
|
|
|
{
|
|
|
|
payload.IconEmoji = icon;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
payload.IconUrl = icon;
|
|
|
|
}
|
2016-03-22 18:37:06 +00:00
|
|
|
}
|
2017-10-29 02:35:50 +00:00
|
|
|
|
2017-12-03 20:58:29 +00:00
|
|
|
if (channel.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
payload.Channel = channel;
|
|
|
|
}
|
|
|
|
|
2017-10-29 02:35:50 +00:00
|
|
|
return payload;
|
2016-03-22 18:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|