https://github.com/banane9/compiledfilters
Write reusable filter expressions that get compiled at runtime!
https://github.com/banane9/compiledfilters
Last synced: 5 months ago
JSON representation
Write reusable filter expressions that get compiled at runtime!
- Host: GitHub
- URL: https://github.com/banane9/compiledfilters
- Owner: Banane9
- License: mit
- Created: 2018-04-22T22:03:33.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-26T16:07:15.000Z (about 7 years ago)
- Last Synced: 2024-11-14T23:29:39.870Z (7 months ago)
- Language: C#
- Size: 29.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
CompiledFilters
===============[](https://ci.appveyor.com/project/Banane9/compiledfilters)
Write reusable filter expressions that get compiled at runtime!
## How to Use
[Example for Telegram.Bot](https://github.com/TelegramBotExtensions/Telegram.Bot.Extensions.Filters/tree/develop/src/Telegram.Bot.Extensions.Filters)
``` CSharp
///
/// A that checks if a came from a bot.
///
public static readonly Filter FromBot = UserFilters.IsBot.Using((Message msg) => msg.From);///
/// A that checks if a was forward from a Channel.
///
public static readonly Filter ForwardedFromChannel = Filter.With(msg => msg.ForwardFromChat != null);///
/// A that checks if a was forwarded at all.
///
public static readonly Filter IsForwarded = ForwardedFromUser | ForwardedFromChannel;internal sealed class MessageRegexFilter : CustomFilter
{
private readonly Regex regex;
private readonly bool text;
private readonly bool caption;private MessageRegexFilter(bool checkText, bool checkCaption)
{
text = checkText;
caption = checkCaption;
}public MessageRegexFilter(Regex regex, bool checkText, bool checkCaption)
: this(checkText, checkCaption)
{
this.regex = regex ?? throw new ArgumentNullException(nameof(regex), "Regex can't be null!");
}public MessageRegexFilter(string text, bool checkText, bool checkCaption)
:this(checkText, checkCaption)
{
regex = new Regex(Regex.Escape(text ?? throw new ArgumentNullException(nameof(text), "Pattern can't be null!")));
}protected override bool Matches(Message message)
{
return (text && message.Text != null && regex.IsMatch(message.Text))
|| (caption && message.Caption != null && regex.IsMatch(message.Caption));
}
}
```