FlorianDahn a22ede0f4f Changing BotBase behaviour to fluent api
- removing unecessary constructors from BotBase
- removing generics from BotBase
- removing generics from SessionBase
- adding StartFormFactory interface
- adding DefaultStartFormFactory
- adding multiple methods to BotBaseBuilder
2021-10-17 17:25:17 +02:00

106 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Telegram.Bot;
using TelegramBotBase.Base;
using TelegramBotBase.Builder.Interfaces;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder
{
public class BotBaseBuilder : IAPIKeySelectionStage, IStartFormSelectionPage, IBuildingStage, INetworkingSelectionStage
{
String apiKey = null;
IStartFormFactory factory = null;
MessageClient client = null;
public static IAPIKeySelectionStage Create()
{
return new BotBaseBuilder();
}
public IStartFormSelectionPage WithAPIKey(string apiKey)
{
this.apiKey = apiKey;
return this;
}
public INetworkingSelectionStage WithStartForm(Type startFormClass)
{
this.factory = new Factories.DefaultStartFormFactory(startFormClass);
return this;
}
public INetworkingSelectionStage WithStartForm<T>()
where T : FormBase, new()
{
this.factory = new Factories.DefaultStartFormFactory(typeof(T));
return this;
}
public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
{
this.factory = factory;
return this;
}
public IBuildingStage WithProxy(string proxyAddress)
{
var url = new Uri(proxyAddress);
client = new MessageClient(apiKey, url);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage NoProxy()
{
client = new MessageClient(apiKey);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithBotClient(TelegramBotClient tgclient)
{
client = new MessageClient(apiKey, tgclient);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithHostAndPort(string proxyHost, int proxyPort)
{
client = new MessageClient(apiKey, proxyHost, proxyPort);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithHttpClient(HttpClient tgclient)
{
client = new MessageClient(apiKey, tgclient);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public BotBase Build()
{
var bb = new BotBase();
bb.APIKey = apiKey;
bb.StartFormFactory = factory;
bb.Client = client;
bb.Sessions.Client = bb.Client;
return bb;
}
}
}