Slack.NetStandard.RequestHandler 3.1.1

.NET Standard 2.0
Install-Package Slack.NetStandard.RequestHandler -Version 3.1.1
dotnet add package Slack.NetStandard.RequestHandler --version 3.1.1
<PackageReference Include="Slack.NetStandard.RequestHandler" Version="3.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Slack.NetStandard.RequestHandler --version 3.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Slack.NetStandard.RequestHandler, 3.1.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Slack.NetStandard.RequestHandler as a Cake Addin
#addin nuget:?package=Slack.NetStandard.RequestHandler&version=3.1.1

// Install Slack.NetStandard.RequestHandler as a Cake Tool
#tool nuget:?package=Slack.NetStandard.RequestHandler&version=3.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Slack.NET.RequestHandlers

Slack apps can get large and complicated and editing long switch statements can be error prone. This library allows you to isolate functionality into RequestHandlers. A request handler is an isolated piece of logic that you want your Slack app to run based on a particular condition (it's slash command, it's a specific type of event with extra conditions).

So what is a request handler?

From a code point of view it's any class that implements the following interface

public interface ISlackRequestHandler<TResponse>
{
	bool CanHandle(SlackContext information);
	Task<TResponse> Handle(SlackContext information);
}

The way this works is that when brought together in a pipeline and a request is processed, each of the request handlers has its CanHandle method executed in declaration order. The first handler that returns true is selected, and the handler logic in the Handle method is executed to generated the necessary response.

Here's a few examples of a request handler

Slash Command:

public class EchoCommand:SlashCommandHandler<Message>
{
    public WeatherCommand():base("/echo")

    public Task<Message> Handle(SlackContext context){
        var restOfMessage = context.Command.Text;
        return new Message{Text = restOfMessage };
    }
}

EventCallback in a lambda proxy

public class OnAppHomeOpenedEvent:EventCallbackHandler<AppHomeOpened,APIGatewayProxyResponse>
{
    public override Task<APIGatewayProxyResponse> Handle(SlackContext context)
    {
        //Add event to queue here .....
        return new APIGatewayProxyResponse{StatusCode=200};
    }
}

Executing your request handlers

To execute your request handlers you build a SlackPipeline and register each of your RequestHanders. As we've said order here is important - it will allow you to make handlers that deal with subtle differences in functionality and you can register the most specific first (such as a slash command with formatted text and a slash command with no extra info).

var pipeline = new SlackPipeline(
    new[]{
        new SlashCommandWithText(),
        new SlashCommandWithoutText(),
        new IMMessageSentToApp()
    }
)

return await pipeline.Process(slashContext);

Side note - another advantage of having handlers perform logic is that your executing environment doesn't need to know about the logic its executing, functionality can be tweaked and reordered by the order of the handlers without any alterations to the project that handles the actual Slack requests.

Grouping Common Conditions

If you start having a large number of handlers, you can group common expressions to try and skip groups of handlers if you know they'll all return false.

You can do this with a GroupedRequestHandler

var pipeline = new SlackPipeline<object>(
            new GroupedRequestHandler<object>(
                context => context.Command != null,
                new SlashCommandWithText(),
                new SlashCommandWithoutText())
);

Pre-packaged handlers

Although you can create handlers for yourself if you wish, there are several types of handler already available as base classes.

  • SlashCommandHandler - CanHandle looks for a command with a specific name
  • InteractionHandler - looks for a specific type of interaction payload (GlobalShortcut, ViewSubmision etc.) and performs any extra checks
  • EventHandler - looks for a specific type of event from the Events API
  • EventCallbackHandler - handles specific types of event callback (most events are of this type except UrlVerification and AppRateLimited)
  • AlwaysTrueRequestHandler - Good as a final item in the list, a catch all that always returns true to ensure you never have requests fail without some handled response

Creating Modal Stacks

To help manage a stack of modal views, you can create ModalStack handlers within your application. These maintain modal view hierarchies.

Modal Stacks

To create a modal stack you start by creating a class that will handle all your modal views.

This must inherit from ModalStack<T> and helps translate to your end result

public class GatewayResponseStack : ModalStack<APIGatewayProxyResponse>
    {
        public GatewayResponseStack(Modal initialView) : base(initialView)
        {
        }

        public override Task<APIGatewayProxyResponse> ConvertResponseAction(ModalResult result)
        {
            APIGatewayProxyResponse response = result switch
            {
                { Submit: not null } => //Submission result handled here,
                { Update: not null } => //Update result (open, push, update) handled here,
                _ => //Default response is a positive "all clear"
            };

            return Task.FromResult(response);
        }
    }

Modals

Once you have your stack, then you can create a tree of Modal views that represent the stack you're trying to create.

Modal classes handle scenarios such as

  • If a view lower down the tree is recognised then the view is correctly pushed onto the current stack.
  • If an action ID is one of those attached to the current view, the modal will re-generate the view and update itself
  • View submission is sent to the Submit method
  • Updates via block actions are sent to the 'Update' method

You nest your modal sub-classes together to represent up to three levels of view stack Slack allows

public EditItemStack() : base(
            new EditItem( 
                new EditItemAction(
                    new ItemActionResult())))
        {
        }
    }
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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
3.1.1 79 1/22/2022
3.1.0 61 1/22/2022
3.0.0 66 1/16/2022
2.2.0 303 5/30/2021
2.2.0-beta9 182 5/29/2021
2.2.0-beta8 183 5/29/2021
2.2.0-beta7 159 5/29/2021
2.2.0-beta6 120 5/28/2021
2.2.0-beta5 105 5/28/2021
2.2.0-beta4 147 5/25/2021
2.2.0-beta3 108 5/22/2021
2.2.0-beta2 113 5/22/2021
2.2.0-beta10 185 5/29/2021
2.2.0-beta1 111 5/22/2021
2.1.0 335 4/9/2021
2.0.0 162 4/9/2021
1.0.0 264 12/31/2020

Add grouped request handler