Adding EndsWith action and example for RTL community

This commit is contained in:
Florian Zevedei 2024-01-24 17:57:17 +01:00
parent 9aab6bca76
commit a26c4ed858
3 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,160 @@
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using TelegramBotBase.Base;
using TelegramBotBase.DependencyInjection;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace DemoBot.ActionManager.Actions
{
public class EndsWithAction : IExternalAction
{
public Type FormType { get; }
public string Value { get; set; }
public Action<FormBase, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public EndsWithAction(Type formType, string value, Action<FormBase, string> setProperty)
{
FormType = formType;
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 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<TForm> : IExternalAction
where TForm : FormBase
{
public string Value { get; set; }
public Action<TForm, String> SetProperty { get; set; }
String? _lastValue { get; set; }
public EndsWithAction(string value, Action<TForm, String> 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 static class EndsWithAction_Extensions
{
public static void AddEndsWithAction<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 EndsWithAction<TForm>(method, setter));
}
public static void AddEndsWithAction<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 EndsWithAction<TForm>(value, setProperty));
}
public static void AddEndsWithAction(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 EndsWithAction(formType, value, setter));
}
public static void AddEndsWithAction(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 EndsWithAction(formType, value, setProperty));
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace DemoBot.Forms
{
public class HiddenForm_EndsWith : AutoCleanForm
{
public String value { get; set; }
public HiddenForm_EndsWith() {
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");
value = value.Replace("_", "\\_");
await Device.Send($"Welcome to Hidden ends with form\n\nThe given value is {value}", bf);
}
}
}

View File

@ -48,6 +48,12 @@ namespace DemoBot
hf.value = b;
});
//Minimal version, using reflection right now
config.AddEndsWithAction<HiddenForm_EndsWith>("_u", (a, b) =>
{
a.value = b;
});
//Deserialize input and waiting for the method property to has value 'tickets'
config.AddGuidAction<HiddenTicketForm>("tickets", (a, b) =>
@ -84,6 +90,10 @@ namespace DemoBot
//Waiting for input starting with 't_' (Non generic version)
config.AddStartsWithAction(typeof(HiddenForm), "t_", a => ((HiddenForm)a).value);
//Waiting for input ending with 'n_'
config.AddEndsWithAction<HiddenForm_EndsWith>("_u", a => a.value);
//Deserialize input and waiting for the method property to has value 'tickets'
config.AddGuidAction<HiddenTicketForm>("tickets", a => a.ticketId);
@ -174,6 +184,8 @@ namespace DemoBot
String max_value3 = "a_".PadRight(32, '5'); //Starts with
String max_value4 = "_u".PadLeft(32, '5'); //Ends with
Guid test_value = Guid.NewGuid(); //Unhandled caller
var callback_guid = GuidAction.GetCallback("open", Guid.NewGuid()); //HiddenOpenForm
@ -198,6 +210,8 @@ namespace DemoBot
bf.AddButtonRow("Test3", max_value3);
bf.AddButtonRow("Test4", max_value4);
bf.AddButtonRow("Test (Guid)", test_value.ToString());
bf.AddButtonRow("Test (Callback Gui)", callback_guid);