diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/EndsWithAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/EndsWithAction.cs new file mode 100644 index 0000000..0a89555 --- /dev/null +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/EndsWithAction.cs @@ -0,0 +1,160 @@ +using System; +using System.Diagnostics; +using System.Linq.Expressions; +using TelegramBotBase.Base; +using TelegramBotBase.DependencyInjection; +using TelegramBotBase.Form; +using TelegramBotBase.Interfaces; +using TelegramBotBase.Sessions; + +namespace DemoBot.ActionManager.Actions +{ + public class EndsWithAction : IExternalAction + { + public Type FormType { get; } + + public string Value { get; set; } + + public Action SetProperty { get; set; } + + String? _lastValue { get; set; } + + + public EndsWithAction(Type formType, string value, Action setProperty) + { + FormType = formType; + Value = value; + SetProperty = setProperty; + } + + public bool DoesFit(string raw_action) + { + if (!raw_action.EndsWith(Value)) + return false; + + _lastValue = raw_action; + + return true; + } + + + public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session) + { + await mr.ConfirmAction(); + + var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase; + + if (_lastValue != null) + { + SetProperty(new_form, _lastValue); + } + + await session.ActiveForm.NavigateTo(new_form); + } + } + + public class EndsWithAction : IExternalAction + where TForm : FormBase + { + public string Value { get; set; } + + public Action SetProperty { get; set; } + + String? _lastValue { get; set; } + + public EndsWithAction(string value, Action setProperty) + { + Value = value; + SetProperty = setProperty; + } + + + public bool DoesFit(string raw_action) + { + if (!raw_action.EndsWith(Value)) + return false; + + _lastValue = raw_action; + + return true; + } + + + public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session) + { + await mr.ConfirmAction(); + + var type = typeof(TForm); + + TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm; + + if (_lastValue != null) + { + SetProperty(new_form, _lastValue); + } + + await session.ActiveForm.NavigateTo(new_form); + } + + + + } + + public static class EndsWithAction_Extensions + { + public static void AddEndsWithAction(this ExternalActionManager manager, string method, Expression> propertySelector) + where TForm : FormBase + { + if (!typeof(FormBase).IsAssignableFrom(typeof(TForm))) + { + throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type"); + } + + var newValue = Expression.Parameter(propertySelector.Body.Type); + + var assign = Expression.Lambda>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); + + var setter = assign.Compile(true); + + manager.Add(new EndsWithAction(method, setter)); + } + public static void AddEndsWithAction(this ExternalActionManager manager, string value, Action setProperty) + where TForm : FormBase + { + if (!typeof(FormBase).IsAssignableFrom(typeof(TForm))) + { + throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type"); + } + + manager.Add(new EndsWithAction(value, setProperty)); + } + + public static void AddEndsWithAction(this ExternalActionManager manager, Type formType, string value, Expression> propertySelector) + { + if (!typeof(FormBase).IsAssignableFrom(formType)) + { + throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); + } + + var newValue = Expression.Parameter(propertySelector.Body.Type); + + var assign = Expression.Lambda>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); + + var setter = assign.Compile(true); + + manager.Add(new EndsWithAction(formType, value, setter)); + } + + public static void AddEndsWithAction(this ExternalActionManager manager, Type formType, string value, Action setProperty) + { + if (!typeof(FormBase).IsAssignableFrom(formType)) + { + throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); + } + + manager.Add(new EndsWithAction(formType, value, setProperty)); + } + + } + +} diff --git a/Experiments/ExternalActionManager/DemoBot/Forms/HiddenForm_EndsWith.cs b/Experiments/ExternalActionManager/DemoBot/Forms/HiddenForm_EndsWith.cs new file mode 100644 index 0000000..95f773a --- /dev/null +++ b/Experiments/ExternalActionManager/DemoBot/Forms/HiddenForm_EndsWith.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.WebSockets; +using System.Text; +using System.Threading.Tasks; +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +namespace DemoBot.Forms +{ + public class HiddenForm_EndsWith : AutoCleanForm + { + public String value { get; set; } + + public HiddenForm_EndsWith() { + + DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm; + DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both; + } + + public override async Task Action(MessageResult message) + { + if (message.RawData != "start") + { + return; + } + + await message.ConfirmAction("Lets go"); + + message.Handled = true; + + var st = new StartForm(); + + await NavigateTo(st); + } + + public override async Task Render(MessageResult message) + { + + var bf = new ButtonForm(); + + bf.AddButtonRow("Goto Start", "start"); + + value = value.Replace("_", "\\_"); + + await Device.Send($"Welcome to Hidden ends with form\n\nThe given value is {value}", bf); + + } + + } +} diff --git a/Experiments/ExternalActionManager/DemoBot/Program.cs b/Experiments/ExternalActionManager/DemoBot/Program.cs index e108f98..472e87a 100644 --- a/Experiments/ExternalActionManager/DemoBot/Program.cs +++ b/Experiments/ExternalActionManager/DemoBot/Program.cs @@ -48,6 +48,12 @@ namespace DemoBot hf.value = b; }); + //Minimal version, using reflection right now + config.AddEndsWithAction("_u", (a, b) => + { + a.value = b; + }); + //Deserialize input and waiting for the method property to has value 'tickets' config.AddGuidAction("tickets", (a, b) => @@ -84,6 +90,10 @@ namespace DemoBot //Waiting for input starting with 't_' (Non generic version) config.AddStartsWithAction(typeof(HiddenForm), "t_", a => ((HiddenForm)a).value); + //Waiting for input ending with 'n_' + config.AddEndsWithAction("_u", a => a.value); + + //Deserialize input and waiting for the method property to has value 'tickets' config.AddGuidAction("tickets", a => a.ticketId); @@ -174,6 +184,8 @@ namespace DemoBot String max_value3 = "a_".PadRight(32, '5'); //Starts with + String max_value4 = "_u".PadLeft(32, '5'); //Ends with + Guid test_value = Guid.NewGuid(); //Unhandled caller var callback_guid = GuidAction.GetCallback("open", Guid.NewGuid()); //HiddenOpenForm @@ -198,6 +210,8 @@ namespace DemoBot bf.AddButtonRow("Test3", max_value3); + bf.AddButtonRow("Test4", max_value4); + bf.AddButtonRow("Test (Guid)", test_value.ToString()); bf.AddButtonRow("Test (Callback Gui)", callback_guid);