https://github.com/alertulabs/hooki
Hooki: A .NET library for creating strongly-typed webhook payloads. Simplify Discord, Slack, and Microsoft Teams integrations with type-safe POCOs. Build, serialize, and send webhooks effortlessly.
https://github.com/alertulabs/hooki
c-sharp discord dotnet json library microsoftteams nuget-package payload sdk slack webhook
Last synced: about 1 year ago
JSON representation
Hooki: A .NET library for creating strongly-typed webhook payloads. Simplify Discord, Slack, and Microsoft Teams integrations with type-safe POCOs. Build, serialize, and send webhooks effortlessly.
- Host: GitHub
- URL: https://github.com/alertulabs/hooki
- Owner: AlertuLabs
- License: mit
- Created: 2024-09-30T01:20:12.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-05T00:02:04.000Z (over 1 year ago)
- Last Synced: 2025-03-30T08:25:19.142Z (about 1 year ago)
- Topics: c-sharp, discord, dotnet, json, library, microsoftteams, nuget-package, payload, sdk, slack, webhook
- Language: C#
- Homepage:
- Size: 577 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Hooki
An awesome library created by Alertu to help with implementing incoming webhooks for various applications!
View Examples
ยท
Documentation
ยท
Report Bug
ยท
Request Feature
# ๐ Table of Contents
- [About Hooki](#-about-hooki)
* [Features](#-features)
* [Why use Hooki?](#-why-use-hooki)
- [Trusted By](#-trusted-by)
- [Getting Started](#-getting-started)
* [Prerequisites](#-prerequisites)
- [Usage](#-usage)
- [Roadmap](#-roadmap)
- [Contributing](#-contributing)
* [Code of Conduct](#-code-of-conduct)
- [License](#-license)
- [Contact](#-contact)
- [Acknowledgements](#-acknowledgements)
## ๐ About Hooki
### ๐ฏ Features
- Strongly typed POCOs for the following platforms:
- Discord Webhook API
- Slack Block Kit SDK
- Microsoft Teams Message Card
- Compile-time checks for missing properties
- Type safety ensuring correct payload structure
- Leveraging existing platform SDKs and standards
### ๐ช Why use Hooki?
Hooki is a powerful .NET library designed to simplify the creation of webhook payloads for popular platforms like Discord, Slack, and Microsoft Teams. It provides a set of strongly-typed C# POCO classes that serve as building blocks, allowing developers to easily construct and serialize webhook payloads into JSON format.
**Main Benefits:**
- **Simplified Development:** Pre-built POCOs for common webhook JSON payloads across various platforms.
- **Type Safety:** Strongly-typed classes ensure compile-time checks and prevent runtime errors.
- **Clean Code:** Eliminates the need for anonymous objects and inline JSON strings.
- **Focus on Content:** Concentrate on your payload's data and style rather than low-level JSON structure.
- **Flexibility:** Easily extensible for custom webhook requirements while maintaining type safety.
## ๐ข Trusted By
## ๐งฐ Getting Started
### โผ๏ธ Prerequisites
The only requirement is compatibility with .net 8.0.x or .net 9.0.x
## ๐ Usage
```csharp
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 Hooki.Discord.Enums;
using Hooki.Discord.Models.BuildingBlocks;
using Hooki.Discord.Models;
public class DiscordWebhookService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger _logger;
public DiscordWebhookService(IHttpClientFactory httpClientFactory, ILogger logger)
{
_httpClientFactory = httpClientFactory;
_logger = logger;
}
private DiscordWebhookPayload CreateDiscordPayload()
{
return new DiscordWebhookPayload
{
Username = "Alertu Webhook",
AvatarUrl = "https://example-url/image.png",
Embeds = new List
{
new DiscordEmbed
{
Author = new DiscordEmbedAuthor
{
Name = "Alertu",
Url = "https://alertu.io",
IconUrl = "https://example-url/image.png"
},
Title = $"Azure Metric Alert triggered",
Description = $"[**View in Alertu**](https://alertu.io) | [**View in Azure**](https://portal.azure.com)",
Color = 959721,
Fields = new List
{
new DiscordEmbedField { Name = "Summary", Value = "This is a test summary", Inline = false },
new DiscordEmbedField { Name = "Organization Name", Value = "Test Organization", Inline = true },
new DiscordEmbedField { Name = "Project Name", Value = "Test Project", Inline = true },
new DiscordEmbedField { Name = "Cloud Provider", Value = "Azure", Inline = true },
new DiscordEmbedField { Name = "Resources", Value = "test-redis, test-postgreSQL", Inline = true },
new DiscordEmbedField { Name = "Severity", Value = "Critical", Inline = true },
new DiscordEmbedField { Name = "Status", Value = "Open", Inline = true },
new DiscordEmbedField { Name = "Triggered At", Value = DateTimeOffset.UtcNow.ToString("f"), Inline = true },
new DiscordEmbedField { Name = "Resolved At", Value = DateTimeOffset.UtcNow.ToString("f"), Inline = true }
}
}
}
};
}
public async Task SendWebhookAsync(string webhookUrl, CancellationToken cancellationToken)
{
try
{
var discordPayload = CreateDiscordPayload();
var jsonString = discordPayload.Serialize();
using var client = _httpClientFactory.CreateClient();
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var response = await client.PostAsync(webhookUrl, content, cancellationToken);
response.EnsureSuccessStatusCode();
_logger.LogInformation($"Successfully posted a Discord message to the webhook URL: {webhookUrl}");
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, $"Failed to post Discord message to webhook URL: {webhookUrl}");
throw;
}
}
}
// Example usage
public class ExampleController
{
private readonly DiscordWebhookService _discordWebhookService;
public ExampleController(DiscordWebhookService discordWebhookService)
{
_discordWebhookService = discordWebhookService;
}
public async Task SendDiscordNotification()
{
string webhookUrl = "https://discord.com/api/webhooks/your-webhook-url-here";
await _discordWebhookService.SendWebhookAsync(webhookUrl, CancellationToken.None);
}
}
```
## ๐งญ Roadmap
* [x] POCOs
* [x] Implement Unit Tests
* [x] Implement Integration Tests
* [ ] Builders are implemented but the Slack builder needs work to be more fluent
* [ ] Implement POCOs for Discord message components
* [ ] Introduce Validation
* [ ] Support other languages?
## ๐ Contributing
Contributions are always welcome!
Please read [Contributing](https://github.com/AlertuLabs/Hooki/blob/main/.github/CONTRIBUTING.md) for ways to get started.
### ๐ Code of Conduct
Please read the [Code of Conduct](https://github.com/AlertuLabs/Hooki/blob/main/.github/CODE_OF_CONDUCT.md)
## โ ๏ธ License
Distributed under MIT License. See LICENSE.txt for more information.
## ๐ค Contact
[@adamthewilliam](https://twitter.com/adamthewilliam)
## ๐ Acknowledgements
- [Readme Template](https://github.com/Louis3797/awesome-readme-template)