MessageLoop updates

- moving MessageLoop folders from Subfolder Factories one level up
- changing namespace and botbuilder methods to change
- adding FullMessageLoop which allows to get all updates (like MinimalMessageLoop but with all methods)
This commit is contained in:
FlorianDahn 2022-06-06 20:16:33 +02:00
parent 2a3d8c1b30
commit 78b3fbebc8
5 changed files with 141 additions and 5 deletions

View File

@ -13,7 +13,7 @@ using TelegramBotBase.Args;
using TelegramBotBase.Attributes;
using TelegramBotBase.Base;
using TelegramBotBase.Enums;
using TelegramBotBase.Factories.MessageLoops;
using TelegramBotBase.MessageLoops;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;

View File

@ -116,7 +116,7 @@ namespace TelegramBotBase.Builder
public IStartFormSelectionStage DefaultMessageLoop()
{
_messageloopfactory = new Factories.MessageLoops.FormBaseMessageLoop();
_messageloopfactory = new MessageLoops.FormBaseMessageLoop();
return this;
}
@ -124,7 +124,7 @@ namespace TelegramBotBase.Builder
public IStartFormSelectionStage MinimalMessageLoop()
{
_messageloopfactory = new Factories.MessageLoops.MinimalMessageLoop();
_messageloopfactory = new MessageLoops.MinimalMessageLoop();
return this;
}

View File

@ -11,8 +11,11 @@ using TelegramBotBase.Enums;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Factories.MessageLoops
namespace TelegramBotBase.MessageLoops
{
/// <summary>
/// Thats the default message loop which reacts to Message, EditMessage and CallbackQuery.
/// </summary>
public class FormBaseMessageLoop : IMessageLoopFactory
{
private static object __evUnhandledCall = new object();

View File

@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Enums;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.MessageLoops
{
/// <summary>
/// This message loop reacts to all update types.
/// </summary>
public class FullMessageLoop : IMessageLoopFactory
{
private static object __evUnhandledCall = new object();
private EventHandlerList __Events = new EventHandlerList();
public FullMessageLoop()
{
}
public async Task MessageLoop(BotBase Bot, DeviceSession session, UpdateResult ur, MessageResult mr)
{
var update = ur.RawData;
//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;
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 == Telegram.Bot.Types.Enums.UpdateType.Message)
{
if (mr.MessageType == Telegram.Bot.Types.Enums.MessageType.Contact
| mr.MessageType == Telegram.Bot.Types.Enums.MessageType.Document
| mr.MessageType == Telegram.Bot.Types.Enums.MessageType.Location
| mr.MessageType == Telegram.Bot.Types.Enums.MessageType.Photo
| mr.MessageType == Telegram.Bot.Types.Enums.MessageType.Video
| mr.MessageType == Telegram.Bot.Types.Enums.MessageType.Audio)
{
await activeForm.SentData(new DataResult(ur));
}
}
//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 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 handeled this call
/// </summary>
public event EventHandler<UnhandledCallEventArgs> UnhandledCall
{
add
{
this.__Events.AddHandler(__evUnhandledCall, value);
}
remove
{
this.__Events.RemoveHandler(__evUnhandledCall, value);
}
}
public void OnUnhandledCall(UnhandledCallEventArgs e)
{
(this.__Events[__evUnhandledCall] as EventHandler<UnhandledCallEventArgs>)?.Invoke(this, e);
}
}
}

View File

@ -11,8 +11,11 @@ using TelegramBotBase.Enums;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Factories.MessageLoops
namespace TelegramBotBase.MessageLoops
{
/// <summary>
/// This is a minimal message loop which will react to all update types and just calling the Load method.
/// </summary>
public class MinimalMessageLoop : IMessageLoopFactory
{
private static object __evUnhandledCall = new object();