using System; using System.Text.RegularExpressions; using Telegram.Bot.Types; namespace RSSBot { /// /// RegexHandler for Telegram Bots. /// public class RegexHandler { private readonly Action CallbackFunction; private readonly string Pattern; /// /// Constructor for the RegexHandler. /// /// Regex pattern /// Callback function to call when the update should be processed public RegexHandler(string pattern, Action callback) { Pattern = pattern; CallbackFunction = callback; } /// /// Checks whether the update should be handled by this handler. /// /// Telegram Message object /// true if the update should be handled public bool HandleUpdate(Message message) { return Regex.IsMatch(message.Text, Pattern, RegexOptions.IgnoreCase); } /// /// Calls the assoicated callback function. /// /// Telegram Message object public void ProcessUpdate(Message message) { GroupCollection matches = Regex.Match(message.Text, Pattern, RegexOptions.IgnoreCase ).Groups; CallbackFunction(message, matches); } } }