BotBuilder.FilterMiddleware 1.0.0

dotnet add package BotBuilder.FilterMiddleware --version 1.0.0
NuGet\Install-Package BotBuilder.FilterMiddleware -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="BotBuilder.FilterMiddleware" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BotBuilder.FilterMiddleware --version 1.0.0
#r "nuget: BotBuilder.FilterMiddleware, 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 BotBuilder.FilterMiddleware as a Cake Addin
#addin nuget:?package=BotBuilder.FilterMiddleware&version=1.0.0

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

botbuilder-filter-cs

A Middleware to filter the context of a conversation based on a given predicate. Works with Bot Builder v4

Based on botbuilder-filter for Node.JS

Background

When we .use middleware, every message we receive gets passed through it. We use middleware to gather things like intent or sentiment, or to log telemetry, or to transform messages (e.g. translation), or to intercept a message. However, we don't always want our middleware to run. For instance, sentiment analysis on text is only useful if that text is fairly long. "No" registers as having incredibly low sentiment, though it doesn't necessarily indicate a disgruntled user. In this case, we would only want to call our sentiment analysis service if our incoming message is over a specific length.

Enter middleware filtering. FilterMiddleware.Filter is just a static function that only runs middleware if a predicate is true.

If we want our bot to pass route messages through a piece of middleware, we .Use use that middleware from the Startup.cs:

services.AddSingleton<BotFrameworkAdapter>(_ =>
  {
      return new BotFrameworkAdapter(Configuration)
          .Use(new TestMiddleware());
  });

If we only want that middleware to run based on custom logic we pass in our Filter function, which takes a predicate and middleware:

services.AddSingleton<BotFrameworkAdapter>(_ =>
  {
      return new BotFrameworkAdapter(Configuration)
          .Use(FilterMiddleware.Filter(lengthPredicate, new TestMiddleware()));
  });

A predicate is just a boolean function. In the above case, our lengthPredicate is checks whether the length of the message in context.request.text is over 5 characters:

Func<IBotContext, bool> lengthPredicate = (context) => 
{
    return (context.Request.Type == "message" && context.Request.AsMessageActivity().Text != null && context.Request.AsMessageActivity().Text.Length < 5);
};

For the sake of demonstration, our TestMiddleware replies to the user if it runs:

  public Task SendActivity(IBotContext context, IList<IActivity> activities, MiddlewareSet.NextDelegate next)
  {
      context.Reply("test");
      return Task.CompletedTask;
  }

So, the sample will only reply from middleware if the incoming message is less than 5 characters long! Of course, you can define any predicate.

Note, we can pass an arbitrary number of middlewares into the filter!

Neither of the above pieces of middleware will run if the length predicate is not met.

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 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 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.0.0 1,156 3/5/2018

First release of package. For more information, visit https://github.com/starlord-daniel/botbuilder-filter-cs