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:
parent
2a3d8c1b30
commit
78b3fbebc8
@ -13,7 +13,7 @@ using TelegramBotBase.Args;
|
|||||||
using TelegramBotBase.Attributes;
|
using TelegramBotBase.Attributes;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Enums;
|
using TelegramBotBase.Enums;
|
||||||
using TelegramBotBase.Factories.MessageLoops;
|
using TelegramBotBase.MessageLoops;
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
using TelegramBotBase.Sessions;
|
||||||
|
|||||||
@ -116,7 +116,7 @@ namespace TelegramBotBase.Builder
|
|||||||
|
|
||||||
public IStartFormSelectionStage DefaultMessageLoop()
|
public IStartFormSelectionStage DefaultMessageLoop()
|
||||||
{
|
{
|
||||||
_messageloopfactory = new Factories.MessageLoops.FormBaseMessageLoop();
|
_messageloopfactory = new MessageLoops.FormBaseMessageLoop();
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -124,7 +124,7 @@ namespace TelegramBotBase.Builder
|
|||||||
|
|
||||||
public IStartFormSelectionStage MinimalMessageLoop()
|
public IStartFormSelectionStage MinimalMessageLoop()
|
||||||
{
|
{
|
||||||
_messageloopfactory = new Factories.MessageLoops.MinimalMessageLoop();
|
_messageloopfactory = new MessageLoops.MinimalMessageLoop();
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,8 +11,11 @@ using TelegramBotBase.Enums;
|
|||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
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
|
public class FormBaseMessageLoop : IMessageLoopFactory
|
||||||
{
|
{
|
||||||
private static object __evUnhandledCall = new object();
|
private static object __evUnhandledCall = new object();
|
||||||
130
TelegramBotBase/MessageLoops/FullMessageLoop.cs
Normal file
130
TelegramBotBase/MessageLoops/FullMessageLoop.cs
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,8 +11,11 @@ using TelegramBotBase.Enums;
|
|||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
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
|
public class MinimalMessageLoop : IMessageLoopFactory
|
||||||
{
|
{
|
||||||
private static object __evUnhandledCall = new object();
|
private static object __evUnhandledCall = new object();
|
||||||
Loading…
x
Reference in New Issue
Block a user