Updating BotBaseBuilder with more functionality

This commit is contained in:
FlorianDahn 2021-10-17 18:21:05 +02:00
parent c4a31e987b
commit d00c5e1799
5 changed files with 138 additions and 37 deletions

View File

@ -3,21 +3,27 @@ using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Telegram.Bot;
using Telegram.Bot.Types;
using TelegramBotBase.Base;
using TelegramBotBase.Builder.Interfaces;
using TelegramBotBase.Commands;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder
{
public class BotBaseBuilder : IAPIKeySelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage
public class BotBaseBuilder : IAPIKeySelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage
{
String apiKey = null;
String _apiKey = null;
IStartFormFactory factory = null;
IStartFormFactory _factory = null;
MessageClient client = null;
MessageClient _client = null;
List<BotCommand> _botcommands = new List<BotCommand>();
IStateMachine _statemachine = null;
public static IAPIKeySelectionStage Create()
{
@ -26,64 +32,95 @@ namespace TelegramBotBase.Builder
public IStartFormSelectionStage WithAPIKey(string apiKey)
{
this.apiKey = apiKey;
this._apiKey = apiKey;
return this;
}
public INetworkingSelectionStage WithStartForm(Type startFormClass)
{
this.factory = new Factories.DefaultStartFormFactory(startFormClass);
this._factory = new Factories.DefaultStartFormFactory(startFormClass);
return this;
}
public INetworkingSelectionStage WithStartForm<T>()
where T : FormBase, new()
{
this.factory = new Factories.DefaultStartFormFactory(typeof(T));
this._factory = new Factories.DefaultStartFormFactory(typeof(T));
return this;
}
public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
{
this.factory = factory;
this._factory = factory;
return this;
}
public IBuildingStage WithProxy(string proxyAddress)
public IBotCommandsStage WithProxy(string proxyAddress)
{
var url = new Uri(proxyAddress);
client = new MessageClient(apiKey, url);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
_client = new MessageClient(_apiKey, url);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage NoProxy()
public IBotCommandsStage NoProxy()
{
client = new MessageClient(apiKey);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
_client = new MessageClient(_apiKey);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithBotClient(TelegramBotClient tgclient)
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient)
{
client = new MessageClient(apiKey, tgclient);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
_client = new MessageClient(_apiKey, tgclient);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithHostAndPort(string proxyHost, int proxyPort)
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
{
client = new MessageClient(apiKey, proxyHost, proxyPort);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
_client = new MessageClient(_apiKey, proxyHost, proxyPort);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithHttpClient(HttpClient tgclient)
public IBotCommandsStage WithHttpClient(HttpClient tgclient)
{
client = new MessageClient(apiKey, tgclient);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
_client = new MessageClient(_apiKey, tgclient);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public ISessionSerializationStage NoCommands()
{
return this;
}
public ISessionSerializationStage DefaultCommands()
{
_botcommands.AddStartCommand("Starts the bot");
_botcommands.AddHelpCommand("Should show you some help");
_botcommands.AddSettingsCommand("Should show you some settings");
return this;
}
public ISessionSerializationStage Configure(Action<List<BotCommand>> action)
{
action?.Invoke(_botcommands);
return this;
}
public IBuildingStage NoSerialization()
{
return this;
}
public IBuildingStage UseSerialization(IStateMachine machine)
{
this._statemachine = machine;
return this;
}
@ -91,13 +128,17 @@ namespace TelegramBotBase.Builder
{
var bb = new BotBase();
bb.APIKey = apiKey;
bb.StartFormFactory = factory;
bb.APIKey = _apiKey;
bb.StartFormFactory = _factory;
bb.Client = client;
bb.Client = _client;
bb.Sessions.Client = bb.Client;
bb.BotCommands = _botcommands;
bb.StateMachine = _statemachine;
return bb;
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
using Telegram.Bot.Types;
namespace TelegramBotBase.Builder.Interfaces
{
public interface IBotCommandsStage
{
/// <summary>
/// Does not create any commands.
/// </summary>
/// <returns></returns>
ISessionSerializationStage NoCommands();
/// <summary>
/// Creates default commands for start, help and settings.
/// </summary>
/// <returns></returns>
ISessionSerializationStage DefaultCommands();
/// <summary>
/// Gives you the ability to add custom commands.
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
ISessionSerializationStage Configure(Action<List<BotCommand>> action);
}
}

View File

@ -13,13 +13,13 @@ namespace TelegramBotBase.Builder.Interfaces
/// </summary>
/// <param name="proxyAddress"></param>
/// <returns></returns>
IBuildingStage WithProxy(String proxyAddress);
IBotCommandsStage WithProxy(String proxyAddress);
/// <summary>
/// Do not choose a proxy as network configuration.
/// </summary>
/// <returns></returns>
IBuildingStage NoProxy();
IBotCommandsStage NoProxy();
/// <summary>
@ -27,7 +27,7 @@ namespace TelegramBotBase.Builder.Interfaces
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
IBuildingStage WithBotClient(TelegramBotClient client);
IBotCommandsStage WithBotClient(TelegramBotClient client);
/// <summary>
/// Sets the custom proxy host and port.
@ -35,14 +35,14 @@ namespace TelegramBotBase.Builder.Interfaces
/// <param name="proxyHost"></param>
/// <param name="Port"></param>
/// <returns></returns>
IBuildingStage WithHostAndPort(String proxyHost, int Port);
IBotCommandsStage WithHostAndPort(String proxyHost, int Port);
/// <summary>
/// Uses a custom http client.
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
IBuildingStage WithHttpClient(HttpClient client);
IBotCommandsStage WithHttpClient(HttpClient client);
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder.Interfaces
{
public interface ISessionSerializationStage
{
/// <summary>
/// Do not uses serialization.
/// </summary>
/// <returns></returns>
IBuildingStage NoSerialization();
/// <summary>
/// Sets the state machine for serialization.
/// </summary>
/// <param name="machine"></param>
/// <returns></returns>
IBuildingStage UseSerialization(IStateMachine machine);
}
}

View File

@ -23,14 +23,19 @@ namespace TelegramBotBaseTest
.WithAPIKey(APIKey)
.WithStartForm<Start>()
.NoProxy()
.Configure(a =>
{
a.AddStartCommand("Starts the bot");
a.AddHelpCommand("Should show you some help");
a.AddSettingsCommand("Should show you some settings");
a.Add(new BotCommand() { Command = "form1", Description = "Opens test form 1" });
a.Add(new BotCommand() { Command = "form2", Description = "Opens test form 2" });
a.Add(new BotCommand() { Command = "params", Description = "Returns all send parameters as a message." });
})
.NoSerialization()
.Build();
bb.BotCommands.AddStartCommand("Starts the bot");
bb.BotCommands.AddHelpCommand("Should show you some help");
bb.BotCommands.AddSettingsCommand("Should show you some settings");
bb.BotCommands.Add(new BotCommand() { Command = "form1", Description = "Opens test form 1" });
bb.BotCommands.Add(new BotCommand() { Command = "form2", Description = "Opens test form 2" });
bb.BotCommands.Add(new BotCommand() { Command = "params", Description = "Returns all send parameters as a message." });
bb.BotCommand += async (s, en) =>
{