{"id":17982661,"url":"https://github.com/starlord-daniel/botbuilder-filter-cs","last_synced_at":"2025-04-04T01:49:31.318Z","repository":{"id":111777454,"uuid":"123569310","full_name":"starlord-daniel/botbuilder-filter-cs","owner":"starlord-daniel","description":"A Middleware to filter the context of a conversation based on a given predicate. Works with [Bot Builder v4](https://github.com/Microsoft/botbuilder-dotnet)","archived":false,"fork":false,"pushed_at":"2018-03-02T11:45:21.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-11T19:36:24.196Z","etag":null,"topics":["bot","bot-framework","microsoft-bot-framework"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starlord-daniel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-02T11:04:47.000Z","updated_at":"2020-05-23T15:25:53.000Z","dependencies_parsed_at":"2023-06-09T00:45:24.139Z","dependency_job_id":null,"html_url":"https://github.com/starlord-daniel/botbuilder-filter-cs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starlord-daniel%2Fbotbuilder-filter-cs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starlord-daniel%2Fbotbuilder-filter-cs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starlord-daniel%2Fbotbuilder-filter-cs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starlord-daniel%2Fbotbuilder-filter-cs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starlord-daniel","download_url":"https://codeload.github.com/starlord-daniel/botbuilder-filter-cs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247107833,"owners_count":20884797,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","bot-framework","microsoft-bot-framework"],"created_at":"2024-10-29T18:14:47.553Z","updated_at":"2025-04-04T01:49:31.299Z","avatar_url":"https://github.com/starlord-daniel.png","language":"C#","readme":"# botbuilder-filter-cs\nA Middleware to filter the context of a conversation based on a given predicate. Works with [Bot Builder v4](https://github.com/Microsoft/botbuilder-dotnet)\n\nBased on [botbuilder-filter for Node.JS](https://github.com/ryanvolum/botbuilder-filter)\n\n## Background\n\nWhen 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. \n\nEnter middleware filtering. FilterMiddleware.Filter is just a static function that only runs middleware if a predicate is true.\n\nIf we want our bot to pass route messages through a piece of middleware, we .Use use that middleware from the Startup.cs:\n\n```csharp\nservices.AddSingleton\u003cBotFrameworkAdapter\u003e(_ =\u003e\n  {\n      return new BotFrameworkAdapter(Configuration)\n          .Use(new TestMiddleware());\n  });\n```\n\nIf we only want that middleware to run based on custom logic we pass in our Filter function, which takes a predicate and middleware:\n\n```csharp\nservices.AddSingleton\u003cBotFrameworkAdapter\u003e(_ =\u003e\n  {\n      return new BotFrameworkAdapter(Configuration)\n          .Use(FilterMiddleware.Filter(lengthPredicate, new TestMiddleware()));\n  });\n```\n\nA 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:\n\n```csharp\nFunc\u003cIBotContext, bool\u003e lengthPredicate = (context) =\u003e \n{\n    return (context.Request.Type == \"message\" \u0026\u0026 context.Request.AsMessageActivity().Text != null \u0026\u0026 context.Request.AsMessageActivity().Text.Length \u003c 5);\n};\n```\n\nFor the sake of demonstration, our TestMiddleware replies to the user if it runs:\n\n```csharp\n  public Task SendActivity(IBotContext context, IList\u003cIActivity\u003e activities, MiddlewareSet.NextDelegate next)\n  {\n      context.Reply(\"test\");\n      return Task.CompletedTask;\n  }\n```\n\nSo, the sample will only reply from middleware if the incoming message is less than 5 characters long! Of course, you can define any predicate. \n\nNote, we can pass an arbitrary number of middlewares into the filter!\n\nNeither of the above pieces of middleware will run if the length predicate is not met.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarlord-daniel%2Fbotbuilder-filter-cs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarlord-daniel%2Fbotbuilder-filter-cs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarlord-daniel%2Fbotbuilder-filter-cs/lists"}