Integrate new Message Loop

This commit is contained in:
FlorianDahn 2022-02-08 16:56:35 +01:00
parent e0ec133209
commit f1c626a7ba
2 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Text;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder.Interfaces
{
public interface IMessageLoopSelectionStage
{
/// <summary>
/// Chooses a default message loop.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
IStartFormSelectionStage DefaultMessageLoop();
/// <summary>
/// Chooses a minimalistic message loop, which catches all update types and only calls the Load function.
/// </summary>
/// <returns></returns>
IStartFormSelectionStage MinimalMessageLoop();
/// <summary>
/// Chooses a custom message loop.
/// </summary>
/// <param name="startFormClass"></param>
/// <returns></returns>
IStartFormSelectionStage CustomMessageLoop(IMessageLoopFactory startFormClass);
/// <summary>
/// Chooses a custom message loop.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
IStartFormSelectionStage CustomMessageLoop<T>() where T : class, new();
}
}

View File

@ -0,0 +1,62 @@
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 MinimalMessageLoop : IMessageLoopFactory
{
private static object __evUnhandledCall = new object();
private EventHandlerList __Events = new EventHandlerList();
public MinimalMessageLoop()
{
}
public async Task MessageLoop(BotBase Bot, DeviceSession session, UpdateResult ur, MessageResult mr)
{
var update = ur.RawData;
mr.Device = session;
var activeForm = session.ActiveForm;
//Loading Event
await activeForm.Load(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);
}
}
}