-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlackService.cs
63 lines (55 loc) · 3.05 KB
/
SlackService.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Threading;
using System.Threading.Tasks;
using WebhookSenderHub.Slack;
namespace WebhookSenderHub
{
/// <summary>
/// A webhook service implementation for sending messages to Slack.
/// </summary>
public sealed class SlackService : WebhookService<MessageCard>, IMessagingService<MessageCard>
{
private readonly string _channel;
/// <summary>
/// Initializes a new instance of the <see cref="SlackService"/> class using a URL and channel.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="url"/> is null or whitespace.</exception>
public SlackService(string url, string channel = null) : base(url)
{
_channel = channel;
}
/// <summary>
/// Initializes a new instance of the <see cref="SlackService"/> class using a URI and channel.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="uri"/> is null or whitespace.</exception>
public SlackService(Uri uri, string channel = null) : base(uri)
{
_channel = channel;
}
/// <inheritdoc/>
public async Task<bool> SendAsync(string text, string title, string summary, string themeColor, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard { Channel = _channel, Attachments = new System.Collections.Generic.List<Attachment> { new Attachment { Color = themeColor, Title = title, Text = text, Fallback = summary } } }, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<bool> SendAsync(string text, string title, string summary, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard { Channel = _channel, Attachments = new System.Collections.Generic.List<Attachment> { new Attachment { Title = title, Text = text, Fallback = summary } } }, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<bool> SendAsync(string text, string title, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard { Channel = _channel, Attachments = new System.Collections.Generic.List<Attachment> { new Attachment { Title = title, Text = text } } }, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public async override Task<bool> SendAsync(string message, CancellationToken cancellationToken = default)
{
return await SendAsync(new MessageCard { Channel = _channel, Attachments = new System.Collections.Generic.List<Attachment> { new Attachment { Text = message } } }, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task<bool> SendAsync(MessageCard message, CancellationToken cancellationToken = default)
{
return await base.SendAsync(message, cancellationToken).ConfigureAwait(false);
}
}
}