V17 - Big Update

- 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
This commit is contained in:
FlorianDahn
2021-11-26 17:57:49 +01:00
parent e9c25ea9e4
commit 31e52887ba
16 changed files with 824 additions and 330 deletions
+48 -2
View File
@@ -12,7 +12,7 @@ using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder
{
public class BotBaseBuilder : IAPIKeySelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage
public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage
{
String _apiKey = null;
@@ -25,17 +25,55 @@ namespace TelegramBotBase.Builder
IStateMachine _statemachine = null;
IMessageLoopFactory _messageloopfactory = null;
public static IAPIKeySelectionStage Create()
{
return new BotBaseBuilder();
}
public IStartFormSelectionStage WithAPIKey(string apiKey)
#region "Step 1"
public IMessageLoopSelectionStage WithAPIKey(string apiKey)
{
this._apiKey = apiKey;
return this;
}
#endregion
#region "Step 2"
public IStartFormSelectionStage DefaultMessageLoop()
{
_messageloopfactory = new Factories.MessageLoops.FormBaseMessageLoop();
return this;
}
public IStartFormSelectionStage CustomMessageLoop(Type messageLoopClass)
{
if (messageLoopClass.IsSubclassOf(typeof(IMessageLoopFactory)))
throw new ArgumentException($"Not a subclass of {nameof(IMessageLoopFactory)}");
_messageloopfactory = messageLoopClass.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as IMessageLoopFactory;
return this;
}
public IStartFormSelectionStage CustomMessageLoop<T>()
where T : class, new()
{
_messageloopfactory = typeof(T).GetConstructor(new Type[] { })?.Invoke(new object[] { }) as IMessageLoopFactory;
return this;
}
#endregion
#region "Step 3"
public INetworkingSelectionStage WithStartForm(Type startFormClass)
{
this._factory = new Factories.DefaultStartFormFactory(startFormClass);
@@ -55,6 +93,8 @@ namespace TelegramBotBase.Builder
return this;
}
#endregion
public IBotCommandsStage WithProxy(string proxyAddress)
{
@@ -64,6 +104,7 @@ namespace TelegramBotBase.Builder
return this;
}
public IBotCommandsStage NoProxy()
{
_client = new MessageClient(_apiKey);
@@ -79,6 +120,7 @@ namespace TelegramBotBase.Builder
return this;
}
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
{
_client = new MessageClient(_apiKey, proxyHost, proxyPort);
@@ -139,6 +181,10 @@ namespace TelegramBotBase.Builder
bb.StateMachine = _statemachine;
bb.MessageLoopFactory = _messageloopfactory;
bb.MessageLoopFactory.UnhandledCall += bb.MessageLoopFactory_UnhandledCall;
return bb;
}
@@ -11,7 +11,7 @@ namespace TelegramBotBase.Builder.Interfaces
/// </summary>
/// <param name="apiKey"></param>
/// <returns></returns>
IStartFormSelectionStage WithAPIKey(String apiKey);
IMessageLoopSelectionStage WithAPIKey(String apiKey);
}
}
@@ -0,0 +1,36 @@
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 custom message loop.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
IStartFormSelectionStage CustomMessageLoop(Type startFormClass);
/// <summary>
/// Chooses a custom message loop.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
IStartFormSelectionStage CustomMessageLoop<T>() where T : class, new();
}
}
@@ -8,6 +8,7 @@ namespace TelegramBotBase.Builder.Interfaces
{
public interface INetworkingSelectionStage
{
/// <summary>
/// Chooses a proxy as network configuration.
/// </summary>
@@ -29,6 +30,7 @@ namespace TelegramBotBase.Builder.Interfaces
/// <returns></returns>
IBotCommandsStage WithBotClient(TelegramBotClient client);
/// <summary>
/// Sets the custom proxy host and port.
/// </summary>