Slack.NetStandard.RequestHandler 3.1.1

dotnet add package Slack.NetStandard.RequestHandler --version 3.1.1                
NuGet\Install-Package Slack.NetStandard.RequestHandler -Version 3.1.1                
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="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                
#r "nuget: Slack.NetStandard.RequestHandler, 3.1.1"                
#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 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                

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Slack.NetStandard.RequestHandler:

Package Downloads
Slack.NetStandard.Annotations

Library that uses method attributes to generate a Slack app

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.1 3,287 1/22/2022
3.1.0 448 1/22/2022
3.0.0 439 1/16/2022
2.2.0 521 5/30/2021
2.2.0-beta9 303 5/29/2021
2.2.0-beta8 272 5/29/2021
2.2.0-beta7 270 5/29/2021
2.2.0-beta6 238 5/28/2021
2.2.0-beta5 231 5/28/2021
2.2.0-beta4 272 5/25/2021
2.2.0-beta3 225 5/22/2021
2.2.0-beta2 205 5/22/2021
2.2.0-beta10 294 5/29/2021
2.2.0-beta1 203 5/22/2021
2.1.0 530 4/9/2021
2.0.0 360 4/9/2021
1.0.0 482 12/31/2020

Add grouped request handler