mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-02-08 05:30:59 -05:00
* Enable CA2000 in editorconfig * Remove unused CaptureStdOut class * Add missing dispose calls in Sandbox.Tools * Add missing dispose calls in tests * Add missing dispose calls in launchers * Add missing dispose calls in Topten.RichtTextKit * Add missing dispose calls in Engine * Add missing dispose calls in SboxBuild * Add nullchecks to a few dispose calls * Fix more missing disposal calls and leaks * Disable CA2000 for Textures * Fix disposing too early in ImageFileTextureGenerator * Fix disposing codec, ownership is transferred to animation * Add missing using in ByteStream benchmark
83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace Facepunch;
|
|
|
|
/// <summary>
|
|
/// Generic Slack integration utility for sending messages via webhooks
|
|
/// </summary>
|
|
internal static class Slack
|
|
{
|
|
private static readonly HttpClient httpClient = new();
|
|
|
|
/// <summary>
|
|
/// Parameters for sending a Slack message
|
|
/// </summary>
|
|
public record struct MessageParams(
|
|
string Text,
|
|
string Username = null,
|
|
string IconEmoji = null,
|
|
string Channel = null,
|
|
object[] Attachments = null,
|
|
object[] Blocks = null
|
|
);
|
|
|
|
/// <summary>
|
|
/// Send a message to Slack using a webhook URL
|
|
/// </summary>
|
|
/// <param name="webhookUrl">The Slack webhook URL</param>
|
|
/// <param name="parameters">The message parameters</param>
|
|
/// <returns>True if the message was sent successfully</returns>
|
|
public static bool SendMessage( string webhookUrl, MessageParams parameters )
|
|
{
|
|
try
|
|
{
|
|
return SendMessageAsync( webhookUrl, parameters ).GetAwaiter().GetResult();
|
|
}
|
|
catch ( Exception ex )
|
|
{
|
|
Console.WriteLine( $"Error sending Slack message: {ex.Message}" );
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static async Task<bool> SendMessageAsync( string webhookUrl, MessageParams parameters )
|
|
{
|
|
if ( string.IsNullOrEmpty( webhookUrl ) )
|
|
{
|
|
Console.WriteLine( "Slack webhook URL is empty" );
|
|
return false;
|
|
}
|
|
|
|
// Build the payload
|
|
var payload = new
|
|
{
|
|
text = parameters.Text,
|
|
username = parameters.Username,
|
|
icon_emoji = parameters.IconEmoji,
|
|
channel = parameters.Channel,
|
|
attachments = parameters.Attachments,
|
|
blocks = parameters.Blocks
|
|
};
|
|
|
|
var jsonContent = JsonSerializer.Serialize( payload, new JsonSerializerOptions
|
|
{
|
|
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
|
|
} );
|
|
|
|
using var content = new StringContent( jsonContent, Encoding.UTF8, "application/json" );
|
|
|
|
// Send the request
|
|
var response = await httpClient.PostAsync( webhookUrl, content );
|
|
|
|
if ( !response.IsSuccessStatusCode )
|
|
{
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
Console.WriteLine( $"Slack API error: {response.StatusCode} - {responseContent}" );
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|