From 3c93daed35e169a989e1beb64ef5ab185120db0f Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 24 Jan 2024 22:05:34 +0100 Subject: [PATCH] Refactoring of Actions - renaming existing Actions to "Navigation" and adding simple Actions for custom coding - fitting namespaces to purpose - cleanup of older test code --- .../ActionManager/Actions/EndsWithAction.cs | 147 ++------------- .../ActionManager/Actions/GuidAction.cs | 129 +++----------- .../ActionManager/Actions/StartWithAction.cs | 149 ++-------------- .../Navigation/EndsWithNavigation.cs | 135 ++++++++++++++ .../Navigation/GuidNavigation.cs | 168 ++++++++++++++++++ .../Navigation/StartWithNavigation.cs | 132 ++++++++++++++ .../ExternalActionManager/DemoBot/Program.cs | 100 ++++++++--- 7 files changed, 555 insertions(+), 405 deletions(-) create mode 100644 Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/EndsWithNavigation.cs create mode 100644 Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/GuidNavigation.cs create mode 100644 Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/StartWithNavigation.cs diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/EndsWithAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/EndsWithAction.cs index 0a89555..ffd5f6c 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/EndsWithAction.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/EndsWithAction.cs @@ -1,160 +1,37 @@ -using System; -using System.Diagnostics; -using System.Linq.Expressions; -using TelegramBotBase.Base; -using TelegramBotBase.DependencyInjection; +using TelegramBotBase.Base; using TelegramBotBase.Form; -using TelegramBotBase.Interfaces; -using TelegramBotBase.Sessions; namespace DemoBot.ActionManager.Actions { + public class EndsWithAction : IExternalAction { - public Type FormType { get; } + public string SearchForString { get; set; } - public string Value { get; set; } + public Action SetProperty { get; set; } - public Action SetProperty { get; set; } + Func Action; - String? _lastValue { get; set; } - - public EndsWithAction(Type formType, string value, Action setProperty) + public EndsWithAction(string searchFor, Func action) { - FormType = formType; - Value = value; - SetProperty = setProperty; + SearchForString = searchFor; + Action = action; } - 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 bool DoesFit(string raw_data) => raw_data.EndsWith(SearchForString); + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(raw_data, ur, mr); } public static class EndsWithAction_Extensions { - public static void AddEndsWithAction(this ExternalActionManager manager, string method, Expression> propertySelector) - where TForm : FormBase + public static void AddEndsWithAction(this ExternalActionManager manager, string value, Func action) { - 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)); + manager.Add(new EndsWithAction(value, action)); } - 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/ActionManager/Actions/GuidAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs index fc593b9..54c7a7f 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs @@ -1,35 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using System.Threading.Tasks; -using TelegramBotBase.Base; +using TelegramBotBase.Base; using TelegramBotBase.Form; -using TelegramBotBase.Sessions; namespace DemoBot.ActionManager.Actions { public class GuidAction : IExternalAction { - public Type FormType { get; } - - public String Method { get; set; } - - public Action SetProperty { get; set; } + public string Method { get; set; } Guid? _lastValue { get; set; } - public GuidAction(Type formType, string method, Action setProperty) + Func Action; + + public GuidAction(string method, Func action) { - FormType = formType; Method = method; - SetProperty = setProperty; + Action = action; } - public bool DoesFit(string raw_action) + public bool DoesFit(string raw_data) { - var cd = CallbackData.Deserialize(raw_action); + var cd = CallbackData.Deserialize(raw_data); if (cd == null) return false; @@ -46,43 +36,30 @@ namespace DemoBot.ActionManager.Actions } - public async Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session) - { - await mr.ConfirmAction(); + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, ur, mr); - var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase; + public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString()); - if (_lastValue != null) - SetProperty(new_form, _lastValue.Value); - - await session.ActiveForm.NavigateTo(new_form); - } - - - public static CallbackData GetCallback(String method, Guid guid) - { - return new CallbackData(method, guid.ToString()); - } } public class GuidAction : IExternalAction where TForm : FormBase { - public String Method { get; set; } - - public Action SetProperty { get; set; } + public string Method { get; set; } Guid? _lastValue { get; set; } - public GuidAction(string method, Action setProperty) + Func Action; + + public GuidAction(string method, Func action) { Method = method; - SetProperty = setProperty; + Action = action; } - public bool DoesFit(string raw_action) + public bool DoesFit(string raw_data) { - var cd = CallbackData.Deserialize(raw_action); + var cd = CallbackData.Deserialize(raw_data); if (cd == null) return false; @@ -99,83 +76,19 @@ namespace DemoBot.ActionManager.Actions } - 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.Value); - - await session.ActiveForm.NavigateTo(new_form); - } + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, ur, mr); - public static CallbackData GetCallback(String method, Guid guid) - { - return new CallbackData(method, guid.ToString()); - } + public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString()); } public static class GuidAction_Extensions { - public static void AddGuidAction(this ExternalActionManager manager, string method, Expression> propertySelector) - where TForm : FormBase + public static void AddGuidAction(this ExternalActionManager manager, string method, Func action) { - 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 - { - if (!typeof(FormBase).IsAssignableFrom(typeof(TForm))) - { - throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type"); - } - - manager.Add(new GuidAction(method, action)); - } - - public static void AddGuidAction(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 GuidAction(formType, value, setter)); - } - - public static void AddGuidAction(this ExternalActionManager manager, Type formType, string method, Action action) - { - if (!typeof(FormBase).IsAssignableFrom(formType)) - { - throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); - } - - manager.Add(new GuidAction(formType, method, action)); + manager.Add(new GuidAction(method, action)); } } } diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs index 2c46f63..1af6e44 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs @@ -1,160 +1,37 @@ -using System; -using System.Diagnostics; -using System.Linq.Expressions; -using TelegramBotBase.Base; -using TelegramBotBase.DependencyInjection; +using TelegramBotBase.Base; using TelegramBotBase.Form; -using TelegramBotBase.Interfaces; -using TelegramBotBase.Sessions; namespace DemoBot.ActionManager.Actions { + public class StartWithAction : IExternalAction { - public Type FormType { get; } + public string SearchForString { get; set; } - public string Value { get; set; } + public Action SetProperty { get; set; } - public Action SetProperty { get; set; } + Func Action; - String? _lastValue { get; set; } - - public StartWithAction(Type formType, string value, Action setProperty) + public StartWithAction(string searchFor, Func action) { - FormType = formType; - Value = value; - SetProperty = setProperty; - } - - public bool DoesFit(string raw_action) - { - if (!raw_action.StartsWith(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 StartWithAction : IExternalAction - where TForm : FormBase - { - public string Value { get; set; } - - public Action SetProperty { get; set; } - - String? _lastValue { get; set; } - - public StartWithAction(string value, Action setProperty) - { - Value = value; - SetProperty = setProperty; - } - - - public bool DoesFit(string raw_action) - { - if (!raw_action.StartsWith(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); + SearchForString = searchFor; + Action = action; } + public bool DoesFit(string raw_data) => raw_data.StartsWith(SearchForString); + + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) => await Action(raw_data, ur, mr); } public static class StartWithAction_Extensions { - public static void AddStartsWithAction(this ExternalActionManager manager, string method, Expression> propertySelector) - where TForm : FormBase + public static void AddStartsWithAction(this ExternalActionManager manager, string value, Func action) { - 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)); + manager.Add(new StartWithAction(value, action)); } - public static void AddStartsWithAction(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 StartWithAction(value, setProperty)); - } - - public static void AddStartsWithAction(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 StartWithAction(formType, value, setter)); - } - - public static void AddStartsWithAction(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 StartWithAction(formType, value, setProperty)); - } - } } diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/EndsWithNavigation.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/EndsWithNavigation.cs new file mode 100644 index 0000000..a68aa2b --- /dev/null +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/EndsWithNavigation.cs @@ -0,0 +1,135 @@ +using System.Linq.Expressions; +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +namespace DemoBot.ActionManager.Navigation +{ + public class EndsWithNavigation : IExternalAction + { + public Type FormType { get; } + + public string Value { get; set; } + + public Action SetProperty { get; set; } + + public EndsWithNavigation(Type formType, string value, Action setProperty) + { + FormType = formType; + Value = value; + SetProperty = setProperty; + } + + + public bool DoesFit(string raw_data) => raw_data.EndsWith(Value); + + + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) + { + await mr.ConfirmAction(); + + var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase; + + if (raw_data != null) + { + SetProperty(new_form, raw_data); + } + + await ur.Device.ActiveForm.NavigateTo(new_form); + } + } + + public class EndsWithNavigation : IExternalAction + where TForm : FormBase + { + public string Value { get; set; } + + public Action SetProperty { get; set; } + + public EndsWithNavigation(string value, Action setProperty) + { + Value = value; + SetProperty = setProperty; + } + + + public bool DoesFit(string raw_data) => raw_data.EndsWith(Value); + + + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) + { + await mr.ConfirmAction(); + + var type = typeof(TForm); + + TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm; + + if (raw_data != null) + { + SetProperty(new_form, raw_data); + } + + await ur.Device.ActiveForm.NavigateTo(new_form); + } + + + + } + + public static class EndsWithNavigation_Extensions + { + public static void AddEndsWithNavigation(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 EndsWithNavigation(method, setter)); + } + public static void AddEndsWithNavigation(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 EndsWithNavigation(value, setProperty)); + } + + public static void AddEndsWithNavigation(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 EndsWithNavigation(formType, value, setter)); + } + + public static void AddEndsWithNavigation(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 EndsWithNavigation(formType, value, setProperty)); + } + + } + +} diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/GuidNavigation.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/GuidNavigation.cs new file mode 100644 index 0000000..26e47c9 --- /dev/null +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/GuidNavigation.cs @@ -0,0 +1,168 @@ +using System.Linq.Expressions; +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +namespace DemoBot.ActionManager.Navigation +{ + public class GuidNavigation : IExternalAction + { + public Type FormType { get; } + + public string Method { get; set; } + + public Action SetProperty { get; set; } + + Guid? _lastValue { get; set; } + + public GuidNavigation(Type formType, string method, Action setProperty) + { + FormType = formType; + Method = method; + SetProperty = setProperty; + } + + public bool DoesFit(string raw_action) + { + var cd = CallbackData.Deserialize(raw_action); + + if (cd == null) + return false; + + if (cd.Method != Method) + return false; + + Guid g; + + if (Guid.TryParse(cd.Value, out g)) + _lastValue = g; + + return true; + } + + + public async Task DoAction(String data, UpdateResult ur, MessageResult mr) + { + await mr.ConfirmAction(); + + var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase; + + if (_lastValue != null) + SetProperty(new_form, _lastValue.Value); + + await ur.Device.ActiveForm.NavigateTo(new_form); + } + + + public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString()); + } + + public class GuidNavigation : IExternalAction + where TForm : FormBase + { + public string Method { get; set; } + + public Action SetProperty { get; set; } + + Guid? _lastValue { get; set; } + + public GuidNavigation(string method, Action setProperty) + { + Method = method; + SetProperty = setProperty; + } + + public bool DoesFit(string raw_data) + { + var cd = CallbackData.Deserialize(raw_data); + + if (cd == null) + return false; + + if (cd.Method != Method) + return false; + + Guid g; + + if (Guid.TryParse(cd.Value, out g)) + _lastValue = g; + + return true; + } + + + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) + { + 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.Value); + + await ur.Device.ActiveForm.NavigateTo(new_form); + } + + + public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString()); + } + + public static class GuidNavigation_Extensions + { + + public static void AddGuidNavigation(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 GuidNavigation(method, setter)); + } + + public static void AddGuidNavigation(this ExternalActionManager manager, string method, Action action) + where TForm : FormBase + { + if (!typeof(FormBase).IsAssignableFrom(typeof(TForm))) + { + throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type"); + } + + manager.Add(new GuidNavigation(method, action)); + } + + public static void AddGuidNavigation(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 GuidNavigation(formType, value, setter)); + } + + public static void AddGuidNavigation(this ExternalActionManager manager, Type formType, string method, Action action) + { + if (!typeof(FormBase).IsAssignableFrom(formType)) + { + throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); + } + + manager.Add(new GuidNavigation(formType, method, action)); + } + } +} diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/StartWithNavigation.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/StartWithNavigation.cs new file mode 100644 index 0000000..7f45597 --- /dev/null +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Navigation/StartWithNavigation.cs @@ -0,0 +1,132 @@ +using System.Linq.Expressions; +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +namespace DemoBot.ActionManager.Navigation +{ + public class StartWithNavigation : IExternalAction + { + public Type FormType { get; } + + public string Value { get; set; } + + public Action SetProperty { get; set; } + + + public StartWithNavigation(Type formType, string value, Action setProperty) + { + FormType = formType; + Value = value; + SetProperty = setProperty; + } + + public bool DoesFit(string raw_data) => raw_data.StartsWith(Value); + + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) + { + await mr.ConfirmAction(); + + var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase; + + if (raw_data != null) + { + SetProperty(new_form, raw_data); + } + + await ur.Device.ActiveForm.NavigateTo(new_form); + } + } + + public class StartWithNavigation : IExternalAction + where TForm : FormBase + { + public string Value { get; set; } + + public Action SetProperty { get; set; } + + public StartWithNavigation(string value, Action setProperty) + { + Value = value; + SetProperty = setProperty; + } + + + public bool DoesFit(string raw_data) => raw_data.StartsWith(Value); + + + public async Task DoAction(String raw_data, UpdateResult ur, MessageResult mr) + { + await mr.ConfirmAction(); + + var type = typeof(TForm); + + TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm; + + if (raw_data != null) + { + SetProperty(new_form, raw_data); + } + + await ur.Device.ActiveForm.NavigateTo(new_form); + } + + } + + public static class StartWithNavigation_Extensions + { + public static void AddStartsWithNavigation(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 StartWithNavigation(method, setter)); + } + public static void AddStartsWithNavigation(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 StartWithNavigation(value, setProperty)); + } + + public static void AddStartsWithNavigation(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 StartWithNavigation(formType, value, setter)); + } + + public static void AddStartsWithNavigation(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 StartWithNavigation(formType, value, setProperty)); + } + + } + +} diff --git a/Experiments/ExternalActionManager/DemoBot/Program.cs b/Experiments/ExternalActionManager/DemoBot/Program.cs index 472e87a..f37ff6a 100644 --- a/Experiments/ExternalActionManager/DemoBot/Program.cs +++ b/Experiments/ExternalActionManager/DemoBot/Program.cs @@ -1,5 +1,4 @@ using DemoBot.ActionManager; -using DemoBot.ActionManager.Actions; using DemoBot.Extensions; using DemoBot.Forms; using Telegram.Bot; @@ -7,6 +6,9 @@ using Telegram.Bot.Types.ReplyMarkups; using TelegramBotBase.Builder; using TelegramBotBase.Commands; using TelegramBotBase.Form; +using DemoBot.ActionManager.Navigation; +using DemoBot.ActionManager.Actions; +using TelegramBotBase.Base; namespace DemoBot { @@ -25,21 +27,21 @@ namespace DemoBot var eam = ExternalActionManager.Configure(config => { //Waiting for input starting with 'n_' - config.AddStartsWithAction("n_", (a, b) => + config.AddStartsWithNavigation("n_", (a, b) => { a.value = b; }); //Minimal version, using reflection right now - config.AddStartsWithAction("a_", (a, b) => + config.AddStartsWithNavigation("a_", (a, b) => { a.value = b; }); //Waiting for input starting with 't_' - config.AddStartsWithAction(typeof(HiddenForm), "t_", (a, b) => + config.AddStartsWithNavigation(typeof(HiddenForm), "t_", (a, b) => { var hf = a as HiddenForm; if (hf == null) @@ -48,62 +50,92 @@ namespace DemoBot hf.value = b; }); + //Minimal version, using reflection right now - config.AddEndsWithAction("_u", (a, b) => + config.AddEndsWithNavigation("_u", (a, b) => { a.value = b; }); //Deserialize input and waiting for the method property to has value 'tickets' - config.AddGuidAction("tickets", (a, b) => + config.AddGuidNavigation("tickets", (a, b) => { a.ticketId = b; }); //Minimal version, using reflection right now - config.AddGuidAction("letters", (a, b) => + config.AddGuidNavigation("letters", (a, b) => { a.letterId = b; }); //Deserialize input and waiting for the method property to has value 'open' - config.AddGuidAction("open", (a, b) => + config.AddGuidNavigation("open", (a, b) => { a.guid = b; }); + + //Do custom action + config.AddStartsWithAction("p_", StartsWithActionCallback_Demo); + + //Do custom action + config.AddStartsWithAction("p2_", async (value, update, message) => + { + if (message.UpdateData.CallbackQuery == null) + return; + + await update.Device.ConfirmAction(message.UpdateData.CallbackQuery.Id, "Confirmed!"); + }); + }); //Example 2: Short version var eam2 = ExternalActionManager.Configure(config => { //Waiting for input starting with 'n_' - config.AddStartsWithAction("n_", a => a.value); + config.AddStartsWithNavigation("n_", a => a.value); //Waiting for input starting with 'a_' - config.AddStartsWithAction("a_", a => a.value); + config.AddStartsWithNavigation("a_", a => a.value); + //Waiting for input starting with 't_' (Non generic version) - config.AddStartsWithAction(typeof(HiddenForm), "t_", a => ((HiddenForm)a).value); + config.AddStartsWithNavigation(typeof(HiddenForm), "t_", a => ((HiddenForm)a).value); + //Waiting for input ending with 'n_' - config.AddEndsWithAction("_u", a => a.value); + config.AddEndsWithNavigation("_u", a => a.value); //Deserialize input and waiting for the method property to has value 'tickets' - config.AddGuidAction("tickets", a => a.ticketId); + config.AddGuidNavigation("tickets", a => a.ticketId); //Minimal version, using reflection right now - config.AddGuidAction("letters", a => a.letterId); + config.AddGuidNavigation("letters", a => a.letterId); //Deserialize input and waiting for the method property to has value 'open' - config.AddGuidAction("open", a => a.guid); + config.AddGuidNavigation("open", a => a.guid); + + + //Do custom action + config.AddStartsWithAction("p_", StartsWithActionCallback_Demo); + + + //Do custom action + config.AddStartsWithAction("p2_", async (value, update, message) => + { + if (message.UpdateData.CallbackQuery == null) + return; + + await update.Device.ConfirmAction(message.UpdateData.CallbackQuery.Id, "Confirmed!"); + }); }); @@ -144,6 +176,14 @@ namespace DemoBot } + private static async Task StartsWithActionCallback_Demo(String value, UpdateResult ur, MessageResult mr) + { + if (mr.UpdateData.CallbackQuery == null) + return; + + await ur.Device.ConfirmAction(mr.UpdateData.CallbackQuery.Id, "Confirmed!"); + } + private static void Bb_UnhandledCall(object? sender, TelegramBotBase.Args.UnhandledCallEventArgs e) { @@ -178,21 +218,25 @@ namespace DemoBot //Test values - String max_value = "n_".PadRight(32, '5'); //Starts with + String max_value = "n_".PadRight(32, '1'); //Starts with - String max_value2 = "t_".PadRight(32, '5'); //Starts with + String max_value2 = "t_".PadRight(32, '2'); //Starts with - String max_value3 = "a_".PadRight(32, '5'); //Starts with + String max_value3 = "a_".PadRight(32, '3'); //Starts with - String max_value4 = "_u".PadLeft(32, '5'); //Ends with + String max_value4 = "_u".PadLeft(32, '4'); //Ends with + + String max_value5 = "p_".PadRight(32, '5'); //Ends with + + String max_value6 = "p2_".PadRight(32, '6'); //Ends with Guid test_value = Guid.NewGuid(); //Unhandled caller - var callback_guid = GuidAction.GetCallback("open", Guid.NewGuid()); //HiddenOpenForm + var callback_guid = GuidNavigation.GetCallback("open", Guid.NewGuid()); //HiddenOpenForm - var callback_tickets = GuidAction.GetCallback("tickets", Guid.NewGuid()); //HiddenTicketForm + var callback_tickets = GuidNavigation.GetCallback("tickets", Guid.NewGuid()); //HiddenTicketForm - var callback_letters = GuidAction.GetCallback("letters", Guid.NewGuid()); //HiddenLetterForm + var callback_letters = GuidNavigation.GetCallback("letters", Guid.NewGuid()); //HiddenLetterForm 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"; @@ -204,13 +248,17 @@ namespace DemoBot bf.AddButtonRow(new ButtonBase("Ok", "n_ok"), new ButtonBase("Later", "n_later")); - bf.AddButtonRow("Test", max_value); + bf.AddButtonRow("Test (n_)", max_value); - bf.AddButtonRow("Test2", max_value2); + bf.AddButtonRow("Test2 (t_)", max_value2); - bf.AddButtonRow("Test3", max_value3); + bf.AddButtonRow("Test3 (a_)", max_value3); - bf.AddButtonRow("Test4", max_value4); + bf.AddButtonRow("Test4 (_u)", max_value4); + + bf.AddButtonRow("Test5 (p_)", max_value5); + + bf.AddButtonRow("Test6 (p2_)", max_value6); bf.AddButtonRow("Test (Guid)", test_value.ToString());