Initial branch creation with 1. experiment upload.

This commit is contained in:
Florian Zevedei
2024-01-18 19:41:03 +01:00
parent 2d3393aa05
commit 9f41ab352f
14 changed files with 892 additions and 0 deletions
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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<FormBase, Guid> SetProperty { get; set; }
Guid? _lastValue { get; set; }
public GuidAction(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, DeviceSession session)
{
await mr.ConfirmAction();
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
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<TForm> : IExternalAction
where TForm : FormBase
{
public String Method { get; set; }
public Action<TForm, Guid> SetProperty { get; set; }
Guid? _lastValue { get; set; }
public GuidAction(string method, Action<TForm, Guid> setProperty)
{
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, 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 static CallbackData GetCallback(String method, Guid guid)
{
return new CallbackData(method, guid.ToString());
}
}
public static class GuidAction_Extensions
{
public static void AddGuidAction<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 GuidAction<TForm>(method, action));
}
public static void AddGuidAction(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 GuidAction(formType, method, action));
}
}
}
@@ -0,0 +1,128 @@
using System;
using System.Diagnostics;
using TelegramBotBase.Base;
using TelegramBotBase.DependencyInjection;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace DemoBot.ActionManager.Actions
{
public class StartWithAction : IExternalAction
{
public Type FormType { get; }
public string Value { get; set; }
public Action<FormBase, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public StartWithAction(Type formType, string value, Action<FormBase, string> setProperty)
{
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<TForm> : IExternalAction
where TForm : FormBase
{
public string Value { get; set; }
public Action<TForm, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public StartWithAction(string value, Action<TForm, String> 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);
}
}
public static class StartWithAction_Extensions
{
public static void AddStartsWithAction<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 StartWithAction<TForm>(value, setProperty));
}
public static void AddStartsWithAction(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 StartWithAction(formType, value, setProperty));
}
}
}
@@ -0,0 +1,42 @@
using DemoBot.ActionManager.Actions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace DemoBot.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, DeviceSession session)
{
foreach (var action in actions)
{
if (!action.DoesFit(mr.RawData))
continue;
await action.DoAction(ur, mr, session);
return true;
}
return false;
}
}
}
@@ -0,0 +1,15 @@
using TelegramBotBase.Base;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace DemoBot.ActionManager
{
public interface IExternalAction
{
bool DoesFit(string raw_action);
Task DoAction(UpdateResult ur, MessageResult mr, DeviceSession session);
}
}