Adding Configure method and simplify usability

This commit is contained in:
Florian Zevedei 2024-01-22 03:23:45 +01:00
parent 161f191c43
commit aee7562424
3 changed files with 122 additions and 33 deletions

View File

@ -38,5 +38,19 @@ namespace DemoBot.ActionManager
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;
}
}
}

View File

@ -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<HiddenForm>("n_", (a, b) =>
//Example 1: Long version
var eam = ExternalActionManager.Configure(config =>
{
a.value = b;
//Waiting for input starting with 'n_'
config.AddStartsWithAction<HiddenForm>("n_", (a, b) =>
{
a.value = b;
});
//Minimal version, using reflection right now
config.AddStartsWithAction<HiddenForm>("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<HiddenTicketForm>("tickets", (a, b) =>
{
a.ticketId = b;
});
//Minimal version, using reflection right now
config.AddGuidAction<HiddenLetterForm>("letters", (a, b) =>
{
a.letterId = b;
});
//Deserialize input and waiting for the method property to has value 'open'
config.AddGuidAction<HiddenOpenForm>("open", (a, b) =>
{
a.guid = b;
});
});
//Minimal version, using reflection right now
eam.AddStartsWithAction<HiddenForm>("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<HiddenForm>("n_", a => a.value);
//Waiting for input starting with 'a_'
config.AddStartsWithAction<HiddenForm>("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<HiddenTicketForm>("tickets", a => a.ticketId);
//Minimal version, using reflection right now
config.AddGuidAction<HiddenLetterForm>("letters", a => a.letterId);
//Deserialize input and waiting for the method property to has value 'open'
config.AddGuidAction<HiddenOpenForm>("open", a => a.guid);
hf.value = b;
});
eam.AddGuidAction<HiddenTicketForm>("tickets", (a, b) =>
{
a.ticketId = b;
});
//Minimal version, using reflection right now
eam.AddGuidAction<HiddenLetterForm>("letters", a => a.letterId);
eam.AddGuidAction<HiddenOpenForm>("open", (a, b) =>
{
a.guid = b;
});
cfb.ExternalActionManager = eam;
var bb = BotBaseBuilder.Create()
.WithAPIKey(Token)
.CustomMessageLoop(cfb)
.ActionMessageLoop(eam2) //.CustomMessageLoop(cfb)
.WithStartForm<Forms.StartForm>()
.NoProxy()
.CustomCommands(a =>

View File

@ -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);
}
}
}