-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic system for sending Discord webhook messages about server problems
To be expanded in the future
- Loading branch information
Showing
6 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
SS14.Watchdog/Components/Notifications/NotificationManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace SS14.Watchdog.Components.Notifications; | ||
|
||
/// <summary> | ||
/// Implements external notifications to various services. | ||
/// </summary> | ||
/// <seealso cref="NotificationOptions"/> | ||
public sealed partial class NotificationManager( | ||
IHttpClientFactory http, | ||
ILogger<NotificationManager> logger, | ||
IOptions<NotificationOptions> options) | ||
{ | ||
public const string DiscordHttpClient = "discord_webhook"; | ||
|
||
private readonly HttpClient _httpClient = http.CreateClient(); | ||
|
||
// I can't wait for this interface to keep expanding in the future. | ||
public void SendNotification(string message) | ||
{ | ||
var optionsValue = options.Value; | ||
if (optionsValue.DiscordWebhook == null) | ||
{ | ||
logger.LogTrace("Not sending notification: no Discord webhook URL configured"); | ||
return; | ||
} | ||
|
||
SendWebhook(optionsValue, message); | ||
} | ||
|
||
private async void SendWebhook(NotificationOptions optionsValue, string message) | ||
{ | ||
logger.LogTrace("Sending notification \"{Message}\" to Discord webhook...", message); | ||
|
||
try | ||
{ | ||
var messageObject = new DiscordWebhookExecute | ||
{ | ||
Content = message, | ||
AllowedMentions = DiscordAllowedMentions.None | ||
}; | ||
|
||
using var response = await _httpClient.PostAsJsonAsync( | ||
optionsValue.DiscordWebhook, | ||
messageObject, | ||
DiscordSourceGenerationContext.Default.DiscordWebhookExecute); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
logger.LogTrace("Succeeded sending notification to Discord webhook"); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.LogError(e, "Error while sending Discord webhook!"); | ||
} | ||
} | ||
|
||
private sealed class DiscordWebhookExecute | ||
{ | ||
public string? Content { get; set; } | ||
public DiscordAllowedMentions? AllowedMentions { get; set; } = null; | ||
} | ||
|
||
private sealed class DiscordAllowedMentions | ||
{ | ||
public static readonly DiscordAllowedMentions None = new() { Parse = [] }; | ||
|
||
public string[]? Parse { get; set; } | ||
} | ||
|
||
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.KebabCaseLower)] | ||
[JsonSerializable(typeof(DiscordWebhookExecute))] | ||
private partial class DiscordSourceGenerationContext : JsonSerializerContext; | ||
} |
15 changes: 15 additions & 0 deletions
15
SS14.Watchdog/Components/Notifications/NotificationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace SS14.Watchdog.Components.Notifications; | ||
|
||
/// <summary> | ||
/// Options for notifications the watchdog can send via various channels. | ||
/// </summary> | ||
/// <seealso cref="NotificationManager"/> | ||
public sealed class NotificationOptions | ||
{ | ||
public const string Position = "Notification"; | ||
|
||
/// <summary> | ||
/// A Discord webhook URL to send notifications like server crashes to. | ||
/// </summary> | ||
public string? DiscordWebhook { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters