- Adding a message loop interface to build custom message loops - extracting default message loop from BotBase into seperate class - updates to BotBaseBuilder for integration of custom message loops - updating all result classes for using the new Update object of V17 - improving MessageResult and UpdateResult classes - BotBase has been prepared for cleanup (a lot of comments) - Comment cleanup at the next build - updating Readme
132 lines
4.1 KiB
C#
132 lines
4.1 KiB
C#
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.Factories.MessageLoops
|
|
{
|
|
public class FormBaseMessageLoop : IMessageLoopFactory
|
|
{
|
|
private static object __evUnhandledCall = new object();
|
|
|
|
private EventHandlerList __Events = new EventHandlerList();
|
|
|
|
public FormBaseMessageLoop()
|
|
{
|
|
|
|
}
|
|
|
|
public async Task MessageLoop(BotBase Bot, DeviceSession session, UpdateResult ur, MessageResult mr)
|
|
{
|
|
var update = ur.RawData;
|
|
|
|
|
|
if (update.Type != Telegram.Bot.Types.Enums.UpdateType.Message
|
|
&& update.Type != Telegram.Bot.Types.Enums.UpdateType.EditedMessage
|
|
&& update.Type != Telegram.Bot.Types.Enums.UpdateType.CallbackQuery)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//Is this a bot command ?
|
|
if (mr.IsFirstHandler && mr.IsBotCommand && Bot.BotCommands.Count(a => "/" + a.Command == mr.BotCommand) > 0)
|
|
{
|
|
var sce = new BotCommandEventArgs(mr.BotCommand, mr.BotCommandParameters, mr.Message, session.DeviceId, session);
|
|
await Bot.OnBotCommand(sce);
|
|
|
|
if (sce.Handled)
|
|
return;
|
|
}
|
|
|
|
//var mr = new MessageResult(update);
|
|
|
|
mr.Device = session;
|
|
|
|
|
|
//var message = update.Message ?? update.EditedMessage;
|
|
|
|
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)
|
|
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);
|
|
|
|
}
|
|
}
|
|
}
|