Hooki 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Hooki --version 1.0.0                
NuGet\Install-Package Hooki -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hooki" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hooki --version 1.0.0                
#r "nuget: Hooki, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hooki as a Cake Addin
#addin nuget:?package=Hooki&version=1.0.0

// Install Hooki as a Cake Tool
#tool nuget:?package=Hooki&version=1.0.0                

<div align="center">

<img src="./assets/hooki-icon.png" alt="logo" width="100" height="auto" /> <h1>Hooki</h1>

<p> An awesome library created by <a href="https://alertu.io">Alertu</a> to help with implementing incoming webhooks for various applications! </p>

<p> <a href="https://www.nuget.org/packages/Hooki/"> <img src="https://img.shields.io/nuget/dt/Hooki.svg" alt="nuget downloads" /> </a> <a href="https://www.nuget.org/packages/Hooki/"> <img src="https://img.shields.io/nuget/v/Hooki.svg" alt="latest version" /> </a> <a href="https://github.com/AlertuLabs/Hooki/graphs/contributors"> <img src="https://img.shields.io/github/contributors/AlertuLabs/Hooki" alt="contributors" /> </a> <a href=""> <img src="https://img.shields.io/github/last-commit/AlertuLabs/Hooki" alt="last update" /> </a> <a href="https://github.com/AlertuLabs/Hooki/network/members"> <img src="https://img.shields.io/github/forks/AlertuLabs/Hooki" alt="forks" /> </a> <a href="https://github.com/AlertuLabs/Hooki/stargazers"> <img src="https://img.shields.io/github/stars/AlertuLabs/Hooki" alt="stars" /> </a> <a href="https://github.com/AlertuLabs/Hooki/issues/"> <img src="https://img.shields.io/github/issues/AlertuLabs/Hooki" alt="open issues" /> </a> <a href="https://github.com/AlertuLabs/Hooki/blob/master/LICENSE"> <img src="https://img.shields.io/github/license/AlertuLabs/Hooki.svg" alt="license" /> </a> </p>

<h4> <a href="https://github.com/AlertuLabs/Hooki/tree/main/docs/examples">View Examples</a> <span> ยท </span> <a href="https://github.com/AlertuLabs/Hooki/tree/main/docs">Documentation</a> <span> ยท </span> <a href="https://github.com/AlertuLabs/Hooki/issues/">Report Bug</a> <span> ยท </span> <a href="https://github.com/AlertuLabs/Hooki/issues/">Request Feature</a> </h4> </div>

<br />

๐Ÿ“” Table of Contents

๐ŸŒŸ About the Project

๐ŸŽฏ 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

<div align="center"> <table> <tr> <td align="center"> <a href="https://cloudcat.dev"> <img src="https://cloudcat.dev/img/logo.png" width="100px;" alt="Cloudcat Logo"/> </a> </td> </tr> <tr> <td align="center">Cloudcat.dev</td> </tr> </table> </div>

๐Ÿงฐ Getting Started

โ€ผ๏ธ Prerequisites

The only requirement is compatibility with .net 8.0.x

๐Ÿ‘€ Usage

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<DiscordWebhookService> _logger;

    public DiscordWebhookService(IHttpClientFactory httpClientFactory, ILogger<DiscordWebhookService> logger)
    {
        _httpClientFactory = httpClientFactory;
        _logger = logger;
    }

    private DiscordWebhookPayload CreateDiscordPayload()
    {
        return new DiscordWebhookPayload
        {
            Username = "Alertu Webhook",
            AvatarUrl = "https://example-url/image.png",
            Embeds = new List<Embed>
            {
                new Embed
                {
                    Author = new EmbedAuthor
                    {
                        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<EmbedField>
                    {
                        new EmbedField { Name = "Summary", Value = "This is a test summary", Inline = false },
                        new EmbedField { Name = "Organization Name", Value = "Test Organization", Inline = true },
                        new EmbedField { Name = "Project Name", Value = "Test Project", Inline = true },
                        new EmbedField { Name = "Cloud Provider", Value = "Azure", Inline = true },
                        new EmbedField { Name = "Resources", Value = "test-redis, test-postgreSQL", Inline = true },
                        new EmbedField { Name = "Severity", Value = "Critical", Inline = true },
                        new EmbedField { Name = "Status", Value = "Open", Inline = true },
                        new EmbedField { Name = "Triggered At", Value = DateTimeOffset.UtcNow.ToString("f"), Inline = true },
                        new EmbedField { Name = "Resolved At", Value = DateTimeOffset.UtcNow.ToString("f"), Inline = true }
                    }
                }
            }
        };
    }

    public async Task SendWebhookAsync(string webhookUrl, CancellationToken cancellationToken)
    {
        try
        {
            var discordPayload = CreateDiscordPayload();
            var jsonString = JsonSerializer.Serialize(discordPayload);

            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

  • POCOs
  • Implement Unit Tests
  • Provide builders utilising fluent api to reduce boilerplate code when creating webhook payloads
  • Support Files and Polls in Discord Webhook
  • Implement type safety POCOs for Discord message components
  • Introduce Validation to provide a better developer experience (Apps are not returning error details for 400s)
  • Remove the use of objects in numerous places and replace with a clean union type solution for type safety and readability
  • Support other languages?

๐Ÿ‘‹ Contributing

<a href="https://github.com/AlertuLabs/Hooki/graphs/contributors"> <img src="https://contrib.rocks/image?repo=AlertuLabs/Hooki" /> </a>

Contributions are always welcome!

Please read Contributing for ways to get started.

๐Ÿ“œ Code of Conduct

Please read the Code of Conduct

โš ๏ธ License

Distributed under MIT License. See LICENSE.txt for more information.

๐Ÿค Contact

Adam Ferguson - @adamthewilliam

๐Ÿ’Ž Acknowledgements

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 223 10/8/2024
1.0.0 91 10/7/2024