From aee75624246da2cb4a574bd617b8c151b358f1f3 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Mon, 22 Jan 2024 03:23:45 +0100 Subject: [PATCH] Adding Configure method and simplify usability --- .../ActionManager/ExternalActionManager.cs | 14 +++ .../ExternalActionManager/DemoBot/Program.cs | 114 +++++++++++++----- .../DemoBot/StaticExtensions.cs | 27 +++++ 3 files changed, 122 insertions(+), 33 deletions(-) create mode 100644 Experiments/ExternalActionManager/DemoBot/StaticExtensions.cs diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/ExternalActionManager.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/ExternalActionManager.cs index bc09fee..4f3f614 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/ExternalActionManager.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/ExternalActionManager.cs @@ -38,5 +38,19 @@ namespace DemoBot.ActionManager return false; } + + /// + /// Creates an instance of the ExternalActionManager for configuration. + /// + /// + /// + public static ExternalActionManager Configure(Action action) + { + var eam = new ExternalActionManager(); + + action(eam); + + return eam; + } } } diff --git a/Experiments/ExternalActionManager/DemoBot/Program.cs b/Experiments/ExternalActionManager/DemoBot/Program.cs index 1a3a12d..e739f49 100644 --- a/Experiments/ExternalActionManager/DemoBot/Program.cs +++ b/Experiments/ExternalActionManager/DemoBot/Program.cs @@ -1,5 +1,6 @@ using DemoBot.ActionManager; using DemoBot.ActionManager.Actions; +using DemoBot.Extensions; using DemoBot.Forms; using Telegram.Bot; using Telegram.Bot.Types.ReplyMarkups; @@ -18,50 +19,97 @@ namespace DemoBot //Using a custom FormBase message loop which is based on the original FormBaseMessageLoop within the framework. //Would integrate this later into the BotBaseBuilder -> MessageLoop step. - var cfb = new CustomFormBaseMessageLoop(); + //var cfb = new CustomFormBaseMessageLoop(); - var eam = new ExternalActionManager(); - - eam.AddStartsWithAction("n_", (a, b) => + //Example 1: Long version + var eam = ExternalActionManager.Configure(config => { - a.value = b; + //Waiting for input starting with 'n_' + config.AddStartsWithAction("n_", (a, b) => + { + a.value = b; + }); + + + //Minimal version, using reflection right now + config.AddStartsWithAction("a_", (a, b) => + { + a.value = b; + }); + + + //Waiting for input starting with 't_' + config.AddStartsWithAction(typeof(HiddenForm), "t_", (a, b) => + { + var hf = a as HiddenForm; + if (hf == null) + return; + + hf.value = b; + }); + + + //Deserialize input and waiting for the method property to has value 'tickets' + config.AddGuidAction("tickets", (a, b) => + { + a.ticketId = b; + }); + + + //Minimal version, using reflection right now + config.AddGuidAction("letters", (a, b) => + { + a.letterId = b; + }); + + + //Deserialize input and waiting for the method property to has value 'open' + config.AddGuidAction("open", (a, b) => + { + a.guid = b; + }); + }); - - //Minimal version, using reflection right now - eam.AddStartsWithAction("a_", a => a.value); - - - eam.AddStartsWithAction(typeof(HiddenForm), "t_", (a, b) => + //Example 2: Short version + var eam2 = ExternalActionManager.Configure(config => { - var hf = a as HiddenForm; - if (hf == null) - return; + //Waiting for input starting with 'n_' + config.AddStartsWithAction("n_", a => a.value); + + + //Waiting for input starting with 'a_' + config.AddStartsWithAction("a_", a => a.value); + + + //Waiting for input starting with 't_' + config.AddStartsWithAction(typeof(HiddenForm), "t_", (a, b) => + { + var hf = a as HiddenForm; + if (hf == null) + return; + + hf.value = b; + }); + + + //Deserialize input and waiting for the method property to has value 'tickets' + config.AddGuidAction("tickets", a => a.ticketId); + + + //Minimal version, using reflection right now + config.AddGuidAction("letters", a => a.letterId); + + + //Deserialize input and waiting for the method property to has value 'open' + config.AddGuidAction("open", a => a.guid); - 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; - }); - - cfb.ExternalActionManager = eam; - var bb = BotBaseBuilder.Create() .WithAPIKey(Token) - .CustomMessageLoop(cfb) + .ActionMessageLoop(eam2) //.CustomMessageLoop(cfb) .WithStartForm() .NoProxy() .CustomCommands(a => diff --git a/Experiments/ExternalActionManager/DemoBot/StaticExtensions.cs b/Experiments/ExternalActionManager/DemoBot/StaticExtensions.cs new file mode 100644 index 0000000..08d041c --- /dev/null +++ b/Experiments/ExternalActionManager/DemoBot/StaticExtensions.cs @@ -0,0 +1,27 @@ +using DemoBot.ActionManager; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TelegramBotBase.Builder.Interfaces; +using TelegramBotBase.Interfaces; + +namespace DemoBot.Extensions +{ + public static class StaticExtensions + { + public static IStartFormSelectionStage ActionMessageLoop(this IMessageLoopSelectionStage loopSelection, ExternalActionManager managerInstance) + { + var cfb = new CustomFormBaseMessageLoop(); + + cfb.ExternalActionManager = managerInstance; + + return loopSelection.CustomMessageLoop(cfb); + } + + + + + } +}