Skip to content

Commit 9319f1e

Browse files
committed
Refactor command registration and service configuration
Refactored `Program.cs` to use new command registration method. Moved `RegisterCommands` from `BotBuilder` to `ServiceCollectionExtensions`. Updated `ServiceCollectionExtensions` to include `RegisterCommands`. Added `AddDbContext` and `UseAuthorizationHandler` to services.
1 parent 1cfd12c commit 9319f1e

3 files changed

Lines changed: 21 additions & 19 deletions

File tree

Sources/TelegramBot.ConsoleTest/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public class Program
1111
public static void Main(string[] args)
1212
{
1313
BotBuilder builder = new BotBuilder(args)
14-
.UseApiKey(x => x.FromConfiguration())
14+
.UseApiKey(x => x.FromConfiguration());
15+
16+
builder.Services
1517
.RegisterCommands(x =>
1618
{
1719
x.RegisterCommand("/start", "initiates the bot")
@@ -27,9 +29,7 @@ public static void Main(string[] args)
2729
.RegisterCommand("/burgers", "показывает меню бургеров")
2830
.RegisterCommand("/burgersdone", "уведомляет о готовности заказа")
2931
.RegisterCommand("/receipt", "показывает чек");
30-
}, "ru");
31-
32-
builder.Services
32+
}, "ru")
3333
.AddDbContext<AppDbContext>(x => x.UseSqlite("Data Source=app.db"))
3434
.UseAuthorizationHandler<AuthorizationHandler>();
3535
var app = builder.Build();

Sources/TelegramBot/Builders/BotBuilder.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,6 @@ public BotBuilder UseServices(IServiceCollection services)
123123
return this;
124124
}
125125

126-
/// <summary>
127-
/// Register commands for the bot. This method only registers the commands in Telegram UI.
128-
/// Controllers should be registered separately. You can use commands even without this method.
129-
/// </summary>
130-
/// <param name="setup">The setup for the command registration.</param>
131-
/// <param name="language">The language for the commands, default is English.</param>
132-
/// <returns>This instance of <see cref="BotBuilder"/>.</returns>
133-
public BotBuilder RegisterCommands(Action<CommandRegistrationBuilder> setup, string language = "en")
134-
{
135-
CommandRegistrationBuilder builder = new CommandRegistrationBuilder(language);
136-
setup(builder);
137-
Services.AddSingleton(builder);
138-
return this;
139-
}
140-
141126
/// <summary>
142127
/// Build the bot.
143128
/// </summary>

Sources/TelegramBot/Extensions/ServiceCollectionExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using TelegramBot.Builders;
23
using TelegramBot.Attributes;
34
using TelegramBot.Abstractions;
45
using Microsoft.Extensions.DependencyInjection;
@@ -10,6 +11,22 @@ namespace TelegramBot.Extensions
1011
/// </summary>
1112
public static class ServiceCollectionExtensions
1213
{
14+
/// <summary>
15+
/// Register commands for the bot. This method only registers the commands in Telegram UI.
16+
/// Controllers should be registered separately. You can use commands even without this method.
17+
/// </summary>
18+
/// <param name="services">The service collection.</param>
19+
/// <param name="setup">The setup for the command registration.</param>
20+
/// <param name="language">The language for the commands, default is English.</param>
21+
/// <returns>This instance of <see cref="BotBuilder"/>.</returns>
22+
public static IServiceCollection RegisterCommands(this IServiceCollection services, Action<CommandRegistrationBuilder> setup, string language = "en")
23+
{
24+
CommandRegistrationBuilder builder = new CommandRegistrationBuilder(language);
25+
setup(builder);
26+
services.AddSingleton(builder);
27+
return services;
28+
}
29+
1330
/// <summary>
1431
/// Use the authorization predicate for methods and controllers with <see cref="AuthorizeAttribute"/>
1532
/// </summary>

0 commit comments

Comments
 (0)