SlackNet .NET Library

repository·master·Indexed 42 days ago

https://github.com/soxtoby/slacknet

A comprehensive .NET library for building Slack applications, providing high-level wrappers for the Slack Web API. It supports Socket Mode and HTTP-based communication via integrations for ASP.NET Core and Azure Functions. The library includes NuGet packages such as SlackNet, SlackNet.AspNetCore, and SlackNet.AzureFunctions, and supports various DI containers including Microsoft.Extensions.DependencyInjection, Autofac, and Simple Injector.

Tokens
4.3K
Snippets
12
Records
23
Agent score
54%

What's inside SlackNet

  1. Explore SlackNet integration examples

    master

    The Examples directory provides several project templates demonstrating how to integrate SlackNet with different frameworks and dependency injection (DI) containers. Use these as starting points for your own implementation:

    • SlackNetDemo: The primary example project demonstrating general Slack app capabilities. It also serves as the reference for integrating with Microsoft.Extensions.DependencyInjection.
    • AspNetCoreExample: Demonstrates integrating SlackNet with ASP.NET Core.
    • AzureFunctionsExample: Demonstrates integrating SlackNet with Azure Functions.
    • AutofacExample: Demonstrates integrating SlackNet with the Autofac DI container.
    • SimpleInjectorExample: Demonstrates integrating SlackNet with the Simple Injector DI container.
    • NoContainerExample: Demonstrates how to use SlackNet in environments without a DI container.
  2. Configure and create services using SlackServiceBuilder

    master

    While using a dependency injection container is recommended for most scenarios, SlackNet provides a SlackServiceBuilder for scenarios where you want to manage services manually via a basic service factory.

    Important: All configuration must be performed on the builder before calling the method to create the service. Any configuration attempted after the service has been created may not have any effect.

  3. Use the SlackNet Socket Mode client

    master

    Socket Mode allows your application to receive events from Slack without needing a public URL. To use it, initialize the client using an App-Level OAuth token via SlackServiceBuilder. You must register event handlers during the build process.

        .UseAppLevelToken("<app-level OAuth token required for socket mode>")
        /* Register handlers here */
        .GetSocketModeClient();
    
    await client.Connect();
  4. Configure SlackNet in ASP.NET Core

    master

    To receive Slack requests in an ASP.NET Core application, register SlackNet in your service container and add the middleware to your request pipeline. You must provide both an API token and a signing secret for security.

        .UseApiToken("<your bot or user OAuth token here>")
        .UseSigningSecret("<your signing secret here>"));
    
    var app = builder.Build();
    app.UseSlackNet();
  5. Develop with Socket Mode in ASP.NET Core

    master
    During local development, you can avoid hosting your website publicly by enabling Socket Mode within your ASP.NET Core middleware configuration.
    ```c#napp.UseSlackNet(c => c.UseSocketMode());
  6. Set up SlackNet with ASP.NET Core

    master

    To integrate SlackNet into an ASP.NET Core application, follow these steps to configure your Slack App and local environment:

    1. Create a Slack App: Create an app on the Slack developer website (select "from scratch").
    2. Configure Scopes: Request the following bot token scopes:
      • users:read, channels:read, groups:read, im:read, mpim:read (for user and conversation info).
      • chat:write (for posting messages).
    3. Configure appsettings.json:
      • Set ApiToken to your bot user OAuth token.
      • Set SigningSecret to the signing secret from your app's Basic Information page.
    4. Expose your site: Host your website with a publicly accessible HTTPS address.
    5. Configure Slack Events:
      • Set the Request URL to https://<your site's base URL>/slack/event.
      • Subscribe to the following events to receive messages: message.channels, message.groups, message.im, and message.mpim.
    6. Activate: Add your app to a Slack channel and send "ping" to test the integration.
  7. Install and connect SlackNet.Bot

    master

    To use SlackNet.Bot, add the SlackNet.Bot NuGet package to your project. You connect the bot by instantiating a SlackBot with your bot token and calling Connect().

    Note: SlackNet.Bot uses Slack's Real Time Messaging (RTM) API. For most modern applications, the Socket Mode client is recommended, as Slack has deprecated the RTM API and it requires a classic app with a V1 OAuth flow.

    await bot.Connect();
  8. Set up a Slack app for SlackNet

    master

    To use SlackNet, you must first configure a Slack app via the Slack developer portal. Follow these steps:

    1. Create an App: Create a new app from scratch on the Slack developer website.
    2. Configure Scopes: Request the following bot token scopes:
      • users:read, channels:read, groups:read, im:read, mpim:read (for user and conversation info).
      • chat:write (for posting messages).
      • files:read (for uploading files).
    3. Install & Token Setup: Install the app to your workspace. Copy the Bot User OAuth Token and paste it into your application's appsettings.json under the ApiToken key.
    4. Enable Socket Mode: Enable Socket Mode in your Slack app settings. Generate an App-level token and paste it into appsettings.json under the AppLevelToken key.
    5. Enable App Home: Enable the Home Tab feature in your Slack app settings.
    6. Subscribe to Events: Enable Events and subscribe to these specific bot events:
      • app_home_opened (to handle the app home tab view).
      • message.channels, message.groups, message.im, message.mpim (to receive messages).
    7. Add to Channels: Add your bot to the channels or groups you want it to interact with.
  9. Configure SlackNet with Autofac

    master

    SlackNet services can be integrated with Autofac using an API similar to other SlackNet configuration scenarios.

    Important Registration Rules:

    • All Slack handlers must be registered inside the AddSlackNet method.
    • Handlers are scoped per Slack request.
    • You can override these registrations using standard Autofac API methods after the initial registration.
  10. Install SlackNet NuGet packages

    master

    Depending on your integration requirements, choose one of the following NuGet packages:

    • SlackNet: The core package. Provides a comprehensive Slack API client and a Socket Mode client for receiving events.
    • SlackNet.AspNetCore: Integration for ASP.NET Core applications to receive requests from Slack via HTTP.
    • SlackNet.AzureFunctions: Integration for Azure Functions to receive requests from Slack.

    Note: A SlackNet.Bot package exists for the deprecated RTM API, but using the Socket Mode client is recommended.

  11. Configure SlackNet in Azure Functions

    master

    SlackNet.AzureFunctions requires ASP.NET Core integration for the Azure Functions isolated process.

    1. Copy the SlackEndpoints.cs class (available in the project examples) into your project to provide the functions Slack calls.
    2. Configure the host builder to use SlackNet.
    3. Register SlackNet services with your API token and signing secret.

    Warning: Early responses are not supported in Azure Functions; ensure you complete request handling promptly.

    
    hostBuilder.ConfigureServices(services => services
        .AddSlackNet(c => c
            .UseApiToken("<your bot or user OAuth token here>")
            .UseSigningSecret("<your signing secret here>")));