From cbf45891bd272c8a840476fd11f921e7c0ec7cf9 Mon Sep 17 00:00:00 2001 From: voed Date: Tue, 31 Oct 2023 23:03:15 +0200 Subject: [PATCH] Custom bot api server configuration (#177) --- TGBotFramework/BotFramework/Bot.cs | 11 ++++++++--- TGBotFramework/BotFramework/Config/BotConfig.cs | 2 ++ TGBotFramework/BotFramework/Config/DefaultConfig.json | 4 +++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/TGBotFramework/BotFramework/Bot.cs b/TGBotFramework/BotFramework/Bot.cs index a48f542..6008503 100644 --- a/TGBotFramework/BotFramework/Bot.cs +++ b/TGBotFramework/BotFramework/Bot.cs @@ -32,7 +32,7 @@ public class Bot: IHostedService, IBotInstance private TelegramBotClient client; private EventHandlerFactory factory; - + public Bot(IServiceProvider services, IServiceScopeFactory scopeFactory, Type startupType = null) { this.services = services; @@ -64,6 +64,11 @@ private async Task StartListen(CancellationToken cancellationToken) try { + var apiUrl = _config.BotApiUrl; + if(string.IsNullOrWhiteSpace(apiUrl)) + apiUrl = null; + var options = new TelegramBotClientOptions(_config.Token, apiUrl, _config.UseTestEnv); + if(_config.UseSOCKS5) { var proxy = new HttpToSocks5Proxy(_config.SOCKS5Address, _config.SOCKS5Port, _config.SOCKS5User, @@ -71,11 +76,11 @@ private async Task StartListen(CancellationToken cancellationToken) var handler = new HttpClientHandler { Proxy = proxy }; var httpClient = new HttpClient(handler, true); - client = new TelegramBotClient(_config.Token, httpClient); + client = new TelegramBotClient(options, httpClient); } else { - client = new TelegramBotClient(_config.Token); + client = new TelegramBotClient(options); } factory = new EventHandlerFactory(); diff --git a/TGBotFramework/BotFramework/Config/BotConfig.cs b/TGBotFramework/BotFramework/Config/BotConfig.cs index 5de94b5..5038632 100644 --- a/TGBotFramework/BotFramework/Config/BotConfig.cs +++ b/TGBotFramework/BotFramework/Config/BotConfig.cs @@ -12,5 +12,7 @@ public class BotConfig public string SOCKS5User { get; set; } public string SOCKS5Password { get; set; } public bool UseSOCKS5 { get; set; } + public bool UseTestEnv { get; set; } + public string BotApiUrl { get; set; } } } diff --git a/TGBotFramework/BotFramework/Config/DefaultConfig.json b/TGBotFramework/BotFramework/Config/DefaultConfig.json index ddabbf1..d6a3e93 100644 --- a/TGBotFramework/BotFramework/Config/DefaultConfig.json +++ b/TGBotFramework/BotFramework/Config/DefaultConfig.json @@ -8,5 +8,7 @@ "sockS5Port": null, "sockS5User": null, "sockS5Password": null, - "useSOCKS5": false + "useSOCKS5": false, + "useTestEnv": false, + "botApiUrl": null } \ No newline at end of file