-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cs
40 lines (33 loc) · 1012 Bytes
/
Client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
namespace Slack
{
public class Client
{
/// <summary>
/// Holds the HttpClient to Slack
/// </summary>
private static readonly HttpClient HttpClient = new HttpClient
{
BaseAddress = new Uri("https://hooks.slack.com")
};
private readonly Uri _webhookUri;
public Client(IOptions<Options> options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
if (!Uri.TryCreate(options.Value.WebhookUrl, UriKind.Absolute, out Uri webhookUri))
throw new ArgumentException("Please enter a valid Slack webhook url");
_webhookUri = webhookUri;
}
public async Task<HttpResponseMessage> PostAsync(Message message)
{
var content = new StringContent(JsonConvert.SerializeObject(message), Encoding.UTF8, "application/json");
return await HttpClient.PostAsync(_webhookUri.AbsolutePath, content);
}
}
}