-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebhookNotifier.cs
More file actions
59 lines (53 loc) · 2.09 KB
/
WebhookNotifier.cs
File metadata and controls
59 lines (53 loc) · 2.09 KB
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
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace RunCommandsService
{
public class WebhookOptions
{
public bool Enabled { get; set; } = false;
public string Url { get; set; } = null;
public string AuthorizationHeader { get; set; } = null; // e.g., "Bearer xyz"
}
/// <summary>
/// Minimal webhook notifier that POSTs subject+message as JSON to a configured URL.
/// Implements IAlertNotifier so you can plug it into your alert pipeline if desired.
/// </summary>
public class WebhookNotifier : IAlertNotifier
{
private readonly WebhookOptions _opt;
private readonly ILogger<WebhookNotifier> _logger;
private static readonly HttpClient _http = new HttpClient();
public WebhookNotifier(IOptions<WebhookOptions> options, ILogger<WebhookNotifier> logger)
{
_opt = options?.Value ?? new WebhookOptions();
_logger = logger;
}
public async Task NotifyAsync(string subject, string message, CancellationToken ct = default)
{
if (!_opt.Enabled || string.IsNullOrWhiteSpace(_opt.Url)) return;
try
{
var payload = new { subject, message, ts = DateTime.UtcNow.ToString("o") };
var json = JsonSerializer.Serialize(payload);
var req = new HttpRequestMessage(HttpMethod.Post, _opt.Url)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
if (!string.IsNullOrWhiteSpace(_opt.AuthorizationHeader))
req.Headers.TryAddWithoutValidation("Authorization", _opt.AuthorizationHeader);
var res = await _http.SendAsync(req, ct);
res.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
_logger.LogError(ex, "Webhook notify failed");
}
}
}
}