This commit is contained in:
Jamie.Rees 2017-08-07 16:02:00 +01:00
commit d5477adc6b
24 changed files with 613 additions and 8 deletions

View file

@ -0,0 +1,10 @@
using System.Threading.Tasks;
using Ombi.Api.Slack.Models;
namespace Ombi.Api.Slack
{
public interface ISlackApi
{
Task<string> PushAsync(string team, string token, string service, SlackNotificationBody message);
}
}

View file

@ -0,0 +1,30 @@
using Newtonsoft.Json;
namespace Ombi.Api.Slack.Models
{
public class SlackNotificationBody
{
[JsonConstructor]
public SlackNotificationBody()
{
username = "Ombi";
}
[JsonIgnore]
private string _username;
public string username
{
get => _username;
set
{
if (!string.IsNullOrEmpty(value))
_username = value;
}
}
public string channel { get; set; }
public string text { get; set; }
public string icon_url { get; set; }
public string icon_emoji { get; set; }
}
}

View file

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Ombi.Api\Ombi.Api.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,42 @@
using System;
using System.Dynamic;
using System.Net.Http;
using System.Threading.Tasks;
using Ombi.Api.Slack.Models;
namespace Ombi.Api.Slack
{
public class SlackApi : ISlackApi
{
public SlackApi(IApi api)
{
Api = api;
}
private IApi Api { get; }
private const string BaseUrl = "https://hooks.slack.com/";
public async Task<string> PushAsync(string team, string token, string service, SlackNotificationBody message)
{
var request = new Request($"/services/{team}/{service}/{token}", BaseUrl, HttpMethod.Post);
dynamic body = new ExpandoObject();
body.channel = message.channel;
body.text = message.text;
body.username = message.username;
if (!string.IsNullOrEmpty(message.icon_url))
{
body.icon_url = message.icon_url;
}
if (!string.IsNullOrEmpty(message.icon_emoji))
{
body.icon_emoji = message.icon_emoji;
}
request.AddJsonBody(body);
request.ApplicationJsonContentType();
return await Api.Request<string>(request);
}
}
}