Moving experiment files to external class library for use in production systems
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||
{
|
||||
|
||||
public class EndsWithAction : IExternalAction
|
||||
{
|
||||
public string SearchForString { get; set; }
|
||||
|
||||
public Action<FormBase, string> SetProperty { get; set; }
|
||||
|
||||
Func<String, UpdateResult, MessageResult, Task> Action;
|
||||
|
||||
|
||||
public EndsWithAction(string searchFor, Func<String, UpdateResult, MessageResult, Task> action)
|
||||
{
|
||||
SearchForString = searchFor;
|
||||
Action = action;
|
||||
}
|
||||
|
||||
public bool DoesFit(string raw_data) => raw_data.EndsWith(SearchForString);
|
||||
|
||||
|
||||
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(mr.RawData, ur, mr);
|
||||
|
||||
}
|
||||
|
||||
public static class EndsWithAction_Extensions
|
||||
{
|
||||
public static void AddEndsWithAction(this ExternalActionManager manager, string value, Func<String, UpdateResult, MessageResult, Task> action)
|
||||
{
|
||||
manager.Add(new EndsWithAction(value, action));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||
{
|
||||
public class GuidAction : IExternalAction
|
||||
{
|
||||
public string Method { get; set; }
|
||||
|
||||
Guid? _lastValue { get; set; }
|
||||
|
||||
Func<Guid, UpdateResult, MessageResult, Task> Action;
|
||||
|
||||
public GuidAction(string method, Func<Guid, UpdateResult, MessageResult, Task> action)
|
||||
{
|
||||
Method = method;
|
||||
Action = action;
|
||||
}
|
||||
|
||||
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(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, ur, mr);
|
||||
|
||||
public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString());
|
||||
|
||||
}
|
||||
|
||||
public class GuidAction<TForm> : IExternalAction
|
||||
where TForm : FormBase
|
||||
{
|
||||
public string Method { get; set; }
|
||||
|
||||
Guid? _lastValue { get; set; }
|
||||
|
||||
Func<Guid, UpdateResult, MessageResult, Task> Action;
|
||||
|
||||
public GuidAction(string method, Func<Guid, UpdateResult, MessageResult, Task> action)
|
||||
{
|
||||
Method = method;
|
||||
Action = action;
|
||||
}
|
||||
|
||||
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(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, ur, mr);
|
||||
|
||||
|
||||
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, Func<Guid, UpdateResult, MessageResult, Task> action)
|
||||
{
|
||||
manager.Add(new GuidAction(method, action));
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||
{
|
||||
|
||||
public class StartWithAction : IExternalAction
|
||||
{
|
||||
public string SearchForString { get; set; }
|
||||
|
||||
public Action<FormBase, string> SetProperty { get; set; }
|
||||
|
||||
Func<String, UpdateResult, MessageResult, Task> Action;
|
||||
|
||||
|
||||
public StartWithAction(string searchFor, Func<String, UpdateResult, MessageResult, Task> action)
|
||||
{
|
||||
SearchForString = searchFor;
|
||||
Action = action;
|
||||
}
|
||||
|
||||
public bool DoesFit(string raw_data) => raw_data.StartsWith(SearchForString);
|
||||
|
||||
|
||||
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(mr.RawData, ur, mr);
|
||||
|
||||
}
|
||||
|
||||
public static class StartWithAction_Extensions
|
||||
{
|
||||
public static void AddStartsWithAction(this ExternalActionManager manager, string value, Func<String, UpdateResult, MessageResult, Task> action)
|
||||
{
|
||||
manager.Add(new StartWithAction(value, action));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
using TelegramBotBase.Base;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager
|
||||
{
|
||||
public partial class ExternalActionManager
|
||||
{
|
||||
|
||||
List<IExternalAction> actions = new List<IExternalAction>();
|
||||
|
||||
public void Add(IExternalAction action)
|
||||
{
|
||||
actions.Add(action);
|
||||
}
|
||||
|
||||
public async Task<bool> ManageCall(UpdateResult ur, MessageResult mr)
|
||||
{
|
||||
|
||||
foreach (var action in actions)
|
||||
{
|
||||
if (!action.DoesFit(mr.RawData))
|
||||
continue;
|
||||
|
||||
await action.DoAction(ur, mr);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the ExternalActionManager for configuration.
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static ExternalActionManager Configure(Action<ExternalActionManager> action)
|
||||
{
|
||||
var eam = new ExternalActionManager();
|
||||
|
||||
action(eam);
|
||||
|
||||
return eam;
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using TelegramBotBase.Base;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager
|
||||
{
|
||||
|
||||
public interface IExternalAction
|
||||
{
|
||||
bool DoesFit(string raw_data);
|
||||
|
||||
Task DoAction(UpdateResult ur, MessageResult mr);
|
||||
}
|
||||
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
using System.Linq.Expressions;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||
{
|
||||
public class EndsWithNavigation : IExternalAction
|
||||
{
|
||||
public Type FormType { get; }
|
||||
|
||||
public string Value { get; set; }
|
||||
|
||||
public Action<FormBase, string> SetProperty { get; set; }
|
||||
|
||||
public EndsWithNavigation(Type formType, string value, Action<FormBase, string> setProperty)
|
||||
{
|
||||
FormType = formType;
|
||||
Value = value;
|
||||
SetProperty = setProperty;
|
||||
}
|
||||
|
||||
|
||||
public bool DoesFit(string raw_data) => raw_data.EndsWith(Value);
|
||||
|
||||
|
||||
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||
{
|
||||
await mr.ConfirmAction();
|
||||
|
||||
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||
|
||||
if (mr.RawData != null)
|
||||
{
|
||||
SetProperty(new_form, mr.RawData);
|
||||
}
|
||||
|
||||
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||
}
|
||||
}
|
||||
|
||||
public class EndsWithNavigation<TForm> : IExternalAction
|
||||
where TForm : FormBase
|
||||
{
|
||||
public string Value { get; set; }
|
||||
|
||||
public Action<TForm, string> SetProperty { get; set; }
|
||||
|
||||
public EndsWithNavigation(string value, Action<TForm, string> setProperty)
|
||||
{
|
||||
Value = value;
|
||||
SetProperty = setProperty;
|
||||
}
|
||||
|
||||
|
||||
public bool DoesFit(string raw_data) => raw_data.EndsWith(Value);
|
||||
|
||||
|
||||
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||
{
|
||||
await mr.ConfirmAction();
|
||||
|
||||
var type = typeof(TForm);
|
||||
|
||||
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||
|
||||
if (mr.RawData != null)
|
||||
{
|
||||
SetProperty(new_form, mr.RawData);
|
||||
}
|
||||
|
||||
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static class EndsWithNavigation_Extensions
|
||||
{
|
||||
public static void AddEndsWithNavigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, string>> 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<Action<TForm, string>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||
|
||||
var setter = assign.Compile(true);
|
||||
|
||||
manager.Add(new EndsWithNavigation<TForm>(method, setter));
|
||||
}
|
||||
public static void AddEndsWithNavigation<TForm>(this ExternalActionManager manager, string value, Action<TForm, string> 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<TForm>(value, setProperty));
|
||||
}
|
||||
|
||||
public static void AddEndsWithNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, string>> 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<Action<FormBase, string>>(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<FormBase, string> 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
using System.Linq.Expressions;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||
{
|
||||
public class GuidNavigation : IExternalAction
|
||||
{
|
||||
public Type FormType { get; }
|
||||
|
||||
public string Method { get; set; }
|
||||
|
||||
public Action<FormBase, Guid> SetProperty { get; set; }
|
||||
|
||||
Guid? _lastValue { get; set; }
|
||||
|
||||
public GuidNavigation(Type formType, string method, Action<FormBase, Guid> 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(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<TForm> : IExternalAction
|
||||
where TForm : FormBase
|
||||
{
|
||||
public string Method { get; set; }
|
||||
|
||||
public Action<TForm, Guid> SetProperty { get; set; }
|
||||
|
||||
Guid? _lastValue { get; set; }
|
||||
|
||||
public GuidNavigation(string method, Action<TForm, Guid> 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(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<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, Guid>> 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<Action<TForm, Guid>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||
|
||||
var setter = assign.Compile(true);
|
||||
|
||||
manager.Add(new GuidNavigation<TForm>(method, setter));
|
||||
}
|
||||
|
||||
public static void AddGuidNavigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, Guid> 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<TForm>(method, action));
|
||||
}
|
||||
|
||||
public static void AddGuidNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, Guid>> 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<Action<FormBase, Guid>>(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<FormBase, Guid> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
using System.Linq.Expressions;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||
{
|
||||
public class StartWithNavigation : IExternalAction
|
||||
{
|
||||
public Type FormType { get; }
|
||||
|
||||
public string Value { get; set; }
|
||||
|
||||
public Action<FormBase, string> SetProperty { get; set; }
|
||||
|
||||
|
||||
public StartWithNavigation(Type formType, string value, Action<FormBase, string> setProperty)
|
||||
{
|
||||
FormType = formType;
|
||||
Value = value;
|
||||
SetProperty = setProperty;
|
||||
}
|
||||
|
||||
public bool DoesFit(string raw_data) => raw_data.StartsWith(Value);
|
||||
|
||||
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||
{
|
||||
await mr.ConfirmAction();
|
||||
|
||||
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||
|
||||
if (mr.RawData != null)
|
||||
{
|
||||
SetProperty(new_form, mr.RawData);
|
||||
}
|
||||
|
||||
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||
}
|
||||
}
|
||||
|
||||
public class StartWithNavigation<TForm> : IExternalAction
|
||||
where TForm : FormBase
|
||||
{
|
||||
public string Value { get; set; }
|
||||
|
||||
public Action<TForm, string> SetProperty { get; set; }
|
||||
|
||||
public StartWithNavigation(string value, Action<TForm, string> setProperty)
|
||||
{
|
||||
Value = value;
|
||||
SetProperty = setProperty;
|
||||
}
|
||||
|
||||
|
||||
public bool DoesFit(string raw_data) => raw_data.StartsWith(Value);
|
||||
|
||||
|
||||
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||
{
|
||||
await mr.ConfirmAction();
|
||||
|
||||
var type = typeof(TForm);
|
||||
|
||||
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||
|
||||
if (mr.RawData != null)
|
||||
{
|
||||
SetProperty(new_form, mr.RawData);
|
||||
}
|
||||
|
||||
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class StartWithNavigation_Extensions
|
||||
{
|
||||
public static void AddStartsWithNavigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, string>> 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<Action<TForm, string>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||
|
||||
var setter = assign.Compile(true);
|
||||
|
||||
manager.Add(new StartWithNavigation<TForm>(method, setter));
|
||||
}
|
||||
public static void AddStartsWithNavigation<TForm>(this ExternalActionManager manager, string value, Action<TForm, string> 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<TForm>(value, setProperty));
|
||||
}
|
||||
|
||||
public static void AddStartsWithNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, string>> 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<Action<FormBase, string>>(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<FormBase, string> 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
using System.ComponentModel;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using TelegramBotBase;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Interfaces;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager
|
||||
{
|
||||
public class CustomFormBaseMessageLoop : IMessageLoopFactory
|
||||
{
|
||||
private static readonly object EvUnhandledCall = new();
|
||||
|
||||
private readonly EventHandlerList _events = new();
|
||||
|
||||
public ExternalActionManager ExternalActionManager { get; set; }
|
||||
|
||||
public async Task MessageLoop(BotBase bot, DeviceSession s, UpdateResult ur, MessageResult mr)
|
||||
{
|
||||
var update = ur.RawData;
|
||||
|
||||
if (update.Type != UpdateType.Message
|
||||
&& update.Type != UpdateType.EditedMessage
|
||||
&& update.Type != UpdateType.CallbackQuery)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//Remove unecessary parameter from method call in the future
|
||||
var session = ur.Device;
|
||||
|
||||
//Is this a bot command ?
|
||||
if (mr.IsFirstHandler && mr.IsBotCommand && bot.IsKnownBotCommand(mr.BotCommand))
|
||||
{
|
||||
var sce = new BotCommandEventArgs(mr.BotCommand, mr.BotCommandParameters, mr.Message, session.DeviceId,
|
||||
session);
|
||||
await bot.OnBotCommand(sce);
|
||||
|
||||
if (sce.Handled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mr.Device = session;
|
||||
ur.Device = session;
|
||||
|
||||
var activeForm = session.ActiveForm;
|
||||
|
||||
//Pre Loading Event
|
||||
await activeForm.PreLoad(mr);
|
||||
|
||||
//Send Load event to controls
|
||||
await activeForm.LoadControls(mr);
|
||||
|
||||
//Loading Event
|
||||
await activeForm.Load(mr);
|
||||
|
||||
|
||||
//Is Attachment ? (Photo, Audio, Video, Contact, Location, Document) (Ignore Callback Queries)
|
||||
if (update.Type == UpdateType.Message)
|
||||
{
|
||||
if ((mr.MessageType == MessageType.Contact)
|
||||
| (mr.MessageType == MessageType.Document)
|
||||
| (mr.MessageType == MessageType.Location)
|
||||
| (mr.MessageType == MessageType.Photo)
|
||||
| (mr.MessageType == MessageType.Video)
|
||||
| (mr.MessageType == MessageType.Audio))
|
||||
{
|
||||
await activeForm.SentData(new DataResult(ur));
|
||||
}
|
||||
}
|
||||
|
||||
//Message edited ?
|
||||
if (update.Type == UpdateType.EditedMessage)
|
||||
{
|
||||
await activeForm.Edited(mr);
|
||||
}
|
||||
|
||||
//Action Event
|
||||
if (!session.FormSwitched && mr.IsAction)
|
||||
{
|
||||
//Send Action event to controls
|
||||
await activeForm.ActionControls(mr);
|
||||
|
||||
//Send Action event to form itself
|
||||
await activeForm.Action(mr);
|
||||
|
||||
if (!mr.Handled)
|
||||
{
|
||||
var handled = await ExternalActionManager?.ManageCall(ur, mr);
|
||||
|
||||
if (handled)
|
||||
{
|
||||
mr.Handled = true;
|
||||
if (!session.FormSwitched)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var uhc = new UnhandledCallEventArgs(ur.Message.Text, mr.RawData, session.DeviceId, mr.MessageId, ur.Message, session);
|
||||
|
||||
OnUnhandledCall(uhc);
|
||||
|
||||
if (uhc.Handled)
|
||||
{
|
||||
mr.Handled = true;
|
||||
if (!session.FormSwitched)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (!session.FormSwitched)
|
||||
{
|
||||
//Render Event
|
||||
await activeForm.RenderControls(mr);
|
||||
|
||||
await activeForm.Render(mr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will be called if no form handled this call
|
||||
/// </summary>
|
||||
public event EventHandler<UnhandledCallEventArgs> UnhandledCall
|
||||
{
|
||||
add => _events.AddHandler(EvUnhandledCall, value);
|
||||
remove => _events.RemoveHandler(EvUnhandledCall, value);
|
||||
}
|
||||
|
||||
public void OnUnhandledCall(UnhandledCallEventArgs e)
|
||||
{
|
||||
(_events[EvUnhandledCall] as EventHandler<UnhandledCallEventArgs>)?.Invoke(this, e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Builder.Interfaces;
|
||||
using TelegramBotBase.Interfaces;
|
||||
|
||||
namespace TelegramBotBase.Experiments.ActionManager
|
||||
{
|
||||
public static class StaticExtensions
|
||||
{
|
||||
public static IStartFormSelectionStage ActionMessageLoop(this IMessageLoopSelectionStage loopSelection, ExternalActionManager managerInstance)
|
||||
{
|
||||
var cfb = new CustomFormBaseMessageLoop();
|
||||
|
||||
cfb.ExternalActionManager = managerInstance;
|
||||
|
||||
return loopSelection.CustomMessageLoop(cfb);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user