Skip to content

Commit cca2ddf

Browse files
committed
Add custom HttpClient support to BotConfiguration
Introduce a new property `HttpClient` in the `BotConfiguration` class, allowing for a custom `HttpClient` to be set. If not set, a default `HttpClient` will be used. Update `BotBuilder` to use this custom `HttpClient` when creating an instance of `TelegramBotClient`. If the `HttpClient` is not provided in the `BotConfiguration`, the default behavior remains unchanged. Include a `using` directive for `System.Net.Http` in `BotConfiguration` to support the new `HttpClient` property.
1 parent 1284be7 commit cca2ddf

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/TelegramBot/Builders/BotBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public IBot Build()
144144
throw new ArgumentNullException("TelegramBotToken", "The Telegram bot token is not set.");
145145
}
146146
TelegramBotClientOptions options = new TelegramBotClientOptions(_token, _baseApiUrl);
147-
TelegramBotClient client = new TelegramBotClient(options);
147+
TelegramBotClient client = new TelegramBotClient(options, httpClient: _botConfiguration.HttpClient);
148148
Services.AddSingleton<ITelegramBotClient>(client);
149149
bool hasKeyValueProvider = Services.Any(service => service.ServiceType == typeof(IKeyValueProvider));
150150
if (!hasKeyValueProvider)

Sources/TelegramBot/Builders/BotConfiguration.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace TelegramBot.Builders
1+
using System.Net.Http;
2+
3+
namespace TelegramBot.Builders
24
{
35
/// <summary>
46
/// Bot configuration.
@@ -9,5 +11,10 @@ public class BotConfiguration
911
/// Gets or sets a value indicating whether the bot should receive updates. Default is <see langword="true"/>.
1012
/// </summary>
1113
public bool ReceiveUpdates { get; set; } = true;
14+
15+
/// <summary>
16+
/// Gets or sets a value for custom HTTP client. If not set, a default <see cref="HttpClient"/> will be used.
17+
/// </summary>
18+
public HttpClient? HttpClient { get; set; } = null;
1219
}
1320
}

0 commit comments

Comments
 (0)