diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 38977d6..a7c1263 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -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; diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 403e1a5..d99aa9c 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -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; } diff --git a/TelegramBotBase/Factories/MessageLoops/FormBaseMessageLoop.cs b/TelegramBotBase/MessageLoops/FormBaseMessageLoop.cs similarity index 96% rename from TelegramBotBase/Factories/MessageLoops/FormBaseMessageLoop.cs rename to TelegramBotBase/MessageLoops/FormBaseMessageLoop.cs index 3dcc641..3a6b00c 100644 --- a/TelegramBotBase/Factories/MessageLoops/FormBaseMessageLoop.cs +++ b/TelegramBotBase/MessageLoops/FormBaseMessageLoop.cs @@ -11,8 +11,11 @@ using TelegramBotBase.Enums; using TelegramBotBase.Interfaces; using TelegramBotBase.Sessions; -namespace TelegramBotBase.Factories.MessageLoops +namespace TelegramBotBase.MessageLoops { + /// + /// Thats the default message loop which reacts to Message, EditMessage and CallbackQuery. + /// public class FormBaseMessageLoop : IMessageLoopFactory { private static object __evUnhandledCall = new object(); diff --git a/TelegramBotBase/MessageLoops/FullMessageLoop.cs b/TelegramBotBase/MessageLoops/FullMessageLoop.cs new file mode 100644 index 0000000..63e8160 --- /dev/null +++ b/TelegramBotBase/MessageLoops/FullMessageLoop.cs @@ -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 +{ + /// + /// This message loop reacts to all update types. + /// + 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); + } + + } + + /// + /// Will be called if no form handeled this call + /// + public event EventHandler UnhandledCall + { + add + { + this.__Events.AddHandler(__evUnhandledCall, value); + } + remove + { + this.__Events.RemoveHandler(__evUnhandledCall, value); + } + } + + public void OnUnhandledCall(UnhandledCallEventArgs e) + { + (this.__Events[__evUnhandledCall] as EventHandler)?.Invoke(this, e); + + } + } +} diff --git a/TelegramBotBase/Factories/MessageLoops/MinimalMessageLoop.cs b/TelegramBotBase/MessageLoops/MinimalMessageLoop.cs similarity index 89% rename from TelegramBotBase/Factories/MessageLoops/MinimalMessageLoop.cs rename to TelegramBotBase/MessageLoops/MinimalMessageLoop.cs index 2e3965e..0be394b 100644 --- a/TelegramBotBase/Factories/MessageLoops/MinimalMessageLoop.cs +++ b/TelegramBotBase/MessageLoops/MinimalMessageLoop.cs @@ -11,8 +11,11 @@ using TelegramBotBase.Enums; using TelegramBotBase.Interfaces; using TelegramBotBase.Sessions; -namespace TelegramBotBase.Factories.MessageLoops +namespace TelegramBotBase.MessageLoops { + /// + /// This is a minimal message loop which will react to all update types and just calling the Load method. + /// public class MinimalMessageLoop : IMessageLoopFactory { private static object __evUnhandledCall = new object();