Skip to content

Commit 1284be7

Browse files
committed
Add BotConfiguration to control receiving updates
Introduced BotConfiguration class to manage bot settings, including the ReceiveUpdates property. Updated Program.cs to configure BotBuilder with ReceiveUpdates set to false. Modified BotApp and BotBuilder to utilize BotConfiguration for conditional update handling. Refactored UseApiKey method in BotBuilder.
1 parent a720b3a commit 1284be7

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

Sources/TelegramBot.ConsoleTest/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Program
1111
public static void Main(string[] args)
1212
{
1313
BotBuilder builder = new BotBuilder(args)
14+
.Setup(x => x.ReceiveUpdates = false)
1415
.UseApiKey(x => x.FromConfiguration());
1516

1617
builder.Services

Sources/TelegramBot/BotApp.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class BotApp : IBot
2828
private readonly ILogger<BotApp> _logger;
2929
private readonly TelegramBotClient _client;
3030
private readonly ServiceProvider _serviceProvider;
31+
private readonly BotConfiguration _botConfiguration;
3132
private IReadOnlyCollection<MethodInfo> _controllerMethods;
3233
private readonly CancellationTokenSource _cancellationTokenSource;
3334

@@ -41,10 +42,12 @@ public class BotApp : IBot
4142
/// </summary>
4243
/// <param name="client">Telegram bot client.</param>
4344
/// <param name="serviceProvider">Service provider.</param>
44-
public BotApp(TelegramBotClient client, ServiceProvider serviceProvider)
45+
/// <param name="botConfiguration">Bot configuration.</param>
46+
public BotApp(TelegramBotClient client, ServiceProvider serviceProvider, BotConfiguration botConfiguration)
4547
{
4648
_client = client;
4749
_serviceProvider = serviceProvider;
50+
_botConfiguration = botConfiguration;
4851
_controllerMethods = new List<MethodInfo>();
4952
_cancellationTokenSource = new CancellationTokenSource();
5053
_logger = serviceProvider.GetRequiredService<ILogger<BotApp>>();
@@ -104,8 +107,15 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
104107
try
105108
{
106109
var botUser = _client.GetMe().Result;
107-
_client.StartReceiving(UpdateHandler, ErrorHandler, cancellationToken: mergedToken);
108-
_logger.LogInformation("Bot '{botUser}' started - receiving updates.", botUser.Username);
110+
if (_botConfiguration.ReceiveUpdates)
111+
{
112+
_client.StartReceiving(UpdateHandler, ErrorHandler, cancellationToken: mergedToken);
113+
_logger.LogInformation("Bot '{botUser}' started - receiving updates.", botUser.Username);
114+
}
115+
else
116+
{
117+
_logger.LogInformation("Bot '{botUser}' started - not receiving updates.", botUser.Username);
118+
}
109119
}
110120
catch (Exception ex)
111121
{

Sources/TelegramBot/Builders/BotBuilder.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public BotBuilder(params string[] args)
5555
Services.AddSingleton<IConfigurationRoot>(Configuration);
5656
}
5757

58+
private readonly BotConfiguration _botConfiguration = new BotConfiguration();
5859
private string _baseApiUrl = Constants.DefaultBaseApiUrl;
5960
private string? _token;
6061

@@ -88,13 +89,13 @@ public BotBuilder UseTelegramServer(Action<TelegramServerBuilder> configure)
8889
/// <summary>
8990
/// Use the API key for the Telegram bot.
9091
/// </summary>
91-
/// <param name="value">The configuration for the Telegram API key.</param>
92+
/// <param name="setupApiKey">The configuration for the Telegram API key.</param>
9293
/// <returns>This instance of <see cref="BotBuilder"/>.</returns>
9394
/// <exception cref="ArgumentNullException">Thrown when the Telegram bot token is not set in the configuration.</exception>
94-
public BotBuilder UseApiKey(Action<TelegramApiKeyBuilder> value)
95+
public BotBuilder UseApiKey(Action<TelegramApiKeyBuilder> setupApiKey)
9596
{
9697
TelegramApiKeyBuilder builder = new TelegramApiKeyBuilder();
97-
value(builder);
98+
setupApiKey(builder);
9899
if (!string.IsNullOrWhiteSpace(builder.ApiKey))
99100
{
100101
_token = builder.ApiKey;
@@ -121,6 +122,17 @@ public BotBuilder UseServices(IServiceCollection services)
121122
return this;
122123
}
123124

125+
/// <summary>
126+
/// Setup the bot parameters.
127+
/// </summary>
128+
/// <param name="setup">The configuration for the bot.</param>
129+
/// <returns>This instance of <see cref="BotBuilder"/>.</returns>
130+
public BotBuilder Setup(Action<BotConfiguration> setup)
131+
{
132+
setup(_botConfiguration);
133+
return this;
134+
}
135+
124136
/// <summary>
125137
/// Build the bot.
126138
/// </summary>
@@ -140,7 +152,7 @@ public IBot Build()
140152
Services.AddSingleton<IKeyValueProvider, InMemoryKeyValueProvider>();
141153
}
142154
Services.AddSingleton<IHostApplicationLifetime, HostApplicationLifetime>();
143-
return new BotApp(client, Services.BuildServiceProvider());
155+
return new BotApp(client, Services.BuildServiceProvider(), _botConfiguration);
144156
}
145157
}
146158
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace TelegramBot.Builders
2+
{
3+
/// <summary>
4+
/// Bot configuration.
5+
/// </summary>
6+
public class BotConfiguration
7+
{
8+
/// <summary>
9+
/// Gets or sets a value indicating whether the bot should receive updates. Default is <see langword="true"/>.
10+
/// </summary>
11+
public bool ReceiveUpdates { get; set; } = true;
12+
}
13+
}

0 commit comments

Comments
 (0)