BotBuilder.FilterMiddleware 1.0.0

.NET Core 2.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


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


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)
      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 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 net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
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
1.0.0 1,003 3/5/2018

First release of package. For more information, visit