diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs index 1c87d64..1f2bdeb 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using TelegramBotBase.Base; @@ -123,6 +124,23 @@ namespace DemoBot.ActionManager.Actions public static class GuidAction_Extensions { + public static void AddGuidAction(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 GuidAction(method, setter)); + } + public static void AddGuidAction(this ExternalActionManager manager, string method, Action action) where TForm : FormBase { diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs index fe46773..4e17136 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.Linq.Expressions; using TelegramBotBase.Base; using TelegramBotBase.DependencyInjection; using TelegramBotBase.Form; @@ -101,7 +102,22 @@ namespace DemoBot.ActionManager.Actions public static class StartWithAction_Extensions { + public static void AddStartsWithAction(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 StartWithAction(method, setter)); + } public static void AddStartsWithAction(this ExternalActionManager manager, string value, Action setProperty) where TForm : FormBase { diff --git a/Experiments/ExternalActionManager/DemoBot/Forms/HiddenLetterForm.cs b/Experiments/ExternalActionManager/DemoBot/Forms/HiddenLetterForm.cs new file mode 100644 index 0000000..72b6f33 --- /dev/null +++ b/Experiments/ExternalActionManager/DemoBot/Forms/HiddenLetterForm.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +namespace DemoBot.Forms +{ + public class HiddenLetterForm : AutoCleanForm + { + + public Guid letterId { get; set; } + + public HiddenLetterForm() + { + + 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"); + + await Device.Send($"Welcome to Hidden letter form\n\nYour letter Id is: {letterId}", bf); + + + } + + } +} diff --git a/Experiments/ExternalActionManager/DemoBot/Program.cs b/Experiments/ExternalActionManager/DemoBot/Program.cs index 84e6220..1a3a12d 100644 --- a/Experiments/ExternalActionManager/DemoBot/Program.cs +++ b/Experiments/ExternalActionManager/DemoBot/Program.cs @@ -27,6 +27,11 @@ namespace DemoBot a.value = b; }); + + //Minimal version, using reflection right now + eam.AddStartsWithAction("a_", a => a.value); + + eam.AddStartsWithAction(typeof(HiddenForm), "t_", (a, b) => { var hf = a as HiddenForm; @@ -36,11 +41,17 @@ namespace DemoBot hf.value = b; }); + eam.AddGuidAction("tickets", (a, b) => { a.ticketId = b; }); + + //Minimal version, using reflection right now + eam.AddGuidAction("letters", a => a.letterId); + + eam.AddGuidAction("open", (a, b) => { a.guid = b; @@ -122,14 +133,18 @@ namespace DemoBot String max_value2 = "t_".PadRight(32, '5'); //Starts with + String max_value3 = "a_".PadRight(32, '5'); //Starts with + Guid test_value = Guid.NewGuid(); //Unhandled caller var callback_guid = GuidAction.GetCallback("open", Guid.NewGuid()); //HiddenOpenForm var callback_tickets = GuidAction.GetCallback("tickets", Guid.NewGuid()); //HiddenTicketForm + var callback_letters = GuidAction.GetCallback("letters", Guid.NewGuid()); //HiddenLetterForm - String message = $"Test notification from 'outside'\n\nTest values are:\n\nTest: {max_value}\nTest2: {max_value2}\nTest (Guid): {test_value.ToString()}\nTest (Callback Guid): {callback_guid.Value}\nTickets (Guid): {callback_tickets.Value}\n"; + + String message = $"Test notification from 'outside'\n\nTest values are:\n\nTest: {max_value}\nTest2: {max_value2}\nTest3: {max_value3}\nTest (Guid): {test_value.ToString()}\nTest (Callback Guid): {callback_guid.Value}\nTickets (Guid): {callback_tickets.Value}\nLetters (Guid): {callback_letters.Value}\n"; var tb = new TelegramBotClient(Token); @@ -142,12 +157,16 @@ namespace DemoBot bf.AddButtonRow("Test2", max_value2); + bf.AddButtonRow("Test3", max_value3); + bf.AddButtonRow("Test (Guid)", test_value.ToString()); bf.AddButtonRow("Test (Callback Gui)", callback_guid); bf.AddButtonRow("Tickets", callback_tickets); + bf.AddButtonRow("Letters", callback_letters); + bf.AddButtonRow("Close", "close"); await tb.SendTextMessageAsync(e.DeviceId, message, disableNotification: true, replyMarkup: (InlineKeyboardMarkup)bf);