diff --git a/TgBotFrame.Commands.Help/Extensions/InjectionExtensions.cs b/TgBotFrame.Commands.Help/Extensions/InjectionExtensions.cs index 8a9edba..41aff63 100644 --- a/TgBotFrame.Commands.Help/Extensions/InjectionExtensions.cs +++ b/TgBotFrame.Commands.Help/Extensions/InjectionExtensions.cs @@ -1,4 +1,7 @@ -using TgBotFrame.Commands.Injection; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using TgBotFrame.Commands.Help.Services; +using TgBotFrame.Commands.Injection; namespace TgBotFrame.Commands.Help.Extensions; @@ -9,4 +12,10 @@ public static FrameCommandsBuilder AddHelpCommand(this FrameCommandsBuilder buil builder.TryAddCommandController(); return builder; } + + public static IServiceCollection AddHelpServices(this IServiceCollection serviceCollection) + { + serviceCollection.TryAddSingleton(); + return serviceCollection; + } } \ No newline at end of file diff --git a/TgBotFrame.Commands.Help/Services/HelpCommandService.cs b/TgBotFrame.Commands.Help/Services/HelpCommandService.cs new file mode 100644 index 0000000..ee4f9a2 --- /dev/null +++ b/TgBotFrame.Commands.Help/Services/HelpCommandService.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Logging; +using Telegram.Bot; +using Telegram.Bot.Types; +using TgBotFrame.Commands.Services; + +namespace TgBotFrame.Commands.Help.Services; + +public class HelpCommandService(ITelegramBotClient botClient, CommandExplorerService commandExplorer) +{ + public async Task SetMyCommands() + { + IEnumerable commands = commandExplorer.Commands.Keys + .Select(it => new BotCommand(it.ToLower(), it)); + BotCommandScopeAllPrivateChats allPrivateChats = new (); + //BotCommandScopeAllChatAdministrators + //BotCommandScopeAllChatAdministrators + await botClient.SetMyCommands(commands,allPrivateChats); + } +} \ No newline at end of file diff --git a/TgBotFrame.Example/ExampleCommands.cs b/TgBotFrame.Example/ExampleCommands.cs index 5a284c5..3b1ed95 100644 --- a/TgBotFrame.Example/ExampleCommands.cs +++ b/TgBotFrame.Example/ExampleCommands.cs @@ -82,7 +82,7 @@ await botClient.SendMessage( [Command(nameof(GetId))] public async Task GetId() { - long? userId = Update.Message?.ReplyToMessage?.From?.Id; + long? userId = Update.Message?.Chat?.Id; int? messageId = Context.GetMessageId(); await botClient.SendMessage( Context.GetChatId()!, diff --git a/TgBotFrame.Example/ExtendedHelpController.cs b/TgBotFrame.Example/ExtendedHelpController.cs index 4125424..587c503 100644 --- a/TgBotFrame.Example/ExtendedHelpController.cs +++ b/TgBotFrame.Example/ExtendedHelpController.cs @@ -1,4 +1,4 @@ -using Telegram.Bot; +/*using Telegram.Bot; using Telegram.Bot.Types.Enums; using TgBotFrame.Commands.Attributes; using TgBotFrame.Commands.Extensions; @@ -29,4 +29,4 @@ await _botClient.SendMessage( } : null, cancellationToken: CancellationToken).ConfigureAwait(false); } -} \ No newline at end of file +}*/ \ No newline at end of file diff --git a/TgBotFrame.Example/Program.cs b/TgBotFrame.Example/Program.cs index 8c3be6e..c927334 100644 --- a/TgBotFrame.Example/Program.cs +++ b/TgBotFrame.Example/Program.cs @@ -5,6 +5,7 @@ using TgBotFrame.Commands.Authorization.Extensions; using TgBotFrame.Commands.Authorization.Interfaces; using TgBotFrame.Commands.Help.Extensions; +using TgBotFrame.Commands.Help.Services; using TgBotFrame.Commands.Injection; using TgBotFrame.Commands.RateLimit.Middleware; using TgBotFrame.Commands.RateLimit.Options; @@ -56,6 +57,7 @@ { commandsBuilder.AddStartCommand("Hello, world\\!"); commandsBuilder.AddHelpCommand(); + commandsBuilder.TryAddCommandMiddleware(); commandsBuilder.AddAuthorization(); @@ -63,6 +65,8 @@ commandsBuilder.TryAddControllers(Assembly.GetEntryAssembly()!); }); +builder.Services.AddHelpServices(); + builder.Services.AddHealthChecks() .AddUrlGroup(new Uri("https://api.telegram.org/"), HttpMethod.Head) .AddSqlite(sqliteConnectionString); @@ -73,6 +77,9 @@ AsyncServiceScope scope = scopeFactory.CreateAsyncScope(); await using (scope.ConfigureAwait(false)) { + var helpService = scope.ServiceProvider.GetRequiredService(); + await helpService.SetMyCommands().ConfigureAwait(false); + ExampleDataContext dbContext = scope.ServiceProvider.GetRequiredService(); Directory.CreateDirectory("../data/sqlite"); await dbContext.Database.MigrateAsync().ConfigureAwait(false); diff --git a/global.json b/global.json index 5c984ae..f252ac3 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,7 @@ -{ - "sdk": { - "version": "10.0.101", - "rollForward": "latestFeature" - } +{ + "sdk": { + "version": "10.0.101", + "rollForward": "latestFeature", + "allowPrerelease": true + } } \ No newline at end of file