Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion TgBotFrame.Commands.Help/Extensions/InjectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,4 +12,10 @@ public static FrameCommandsBuilder AddHelpCommand(this FrameCommandsBuilder buil
builder.TryAddCommandController<HelpCommandController>();
return builder;
}

public static IServiceCollection AddHelpServices(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<HelpCommandService>();
return serviceCollection;
}
}
19 changes: 19 additions & 0 deletions TgBotFrame.Commands.Help/Services/HelpCommandService.cs
Original file line number Diff line number Diff line change
@@ -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)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно попробовать реализовать через IHostedService, тогда его будет проще вызывать DI

{
public async Task SetMyCommands()
{
IEnumerable<BotCommand> commands = commandExplorer.Commands.Keys
.Select(it => new BotCommand(it.ToLower(), it));
BotCommandScopeAllPrivateChats allPrivateChats = new ();
//BotCommandScopeAllChatAdministrators
//BotCommandScopeAllChatAdministrators
await botClient.SetMyCommands(commands,allPrivateChats);
}
}
2 changes: 1 addition & 1 deletion TgBotFrame.Example/ExampleCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тогда лучше разделить команды для чата и пользователя, если это необходимо

int? messageId = Context.GetMessageId();
await botClient.SendMessage(
Context.GetChatId()!,
Expand Down
4 changes: 2 additions & 2 deletions TgBotFrame.Example/ExtendedHelpController.cs

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Его можно не наследовать от HelpCommandController и тогда справка отработает коррректно

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Telegram.Bot;
/*using Telegram.Bot;
using Telegram.Bot.Types.Enums;
using TgBotFrame.Commands.Attributes;
using TgBotFrame.Commands.Extensions;
Expand Down Expand Up @@ -29,4 +29,4 @@ await _botClient.SendMessage(
}
: null, cancellationToken: CancellationToken).ConfigureAwait(false);
}
}
}*/
7 changes: 7 additions & 0 deletions TgBotFrame.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,13 +57,16 @@
{
commandsBuilder.AddStartCommand("Hello, world\\!");
commandsBuilder.AddHelpCommand();


commandsBuilder.TryAddCommandMiddleware<RateLimitMiddleware>();
commandsBuilder.AddAuthorization();

commandsBuilder.TryAddControllers(Assembly.GetEntryAssembly()!);
});

builder.Services.AddHelpServices();

builder.Services.AddHealthChecks()
.AddUrlGroup(new Uri("https://api.telegram.org/"), HttpMethod.Head)
.AddSqlite(sqliteConnectionString);
Expand All @@ -73,6 +77,9 @@
AsyncServiceScope scope = scopeFactory.CreateAsyncScope();
await using (scope.ConfigureAwait(false))
{
var helpService = scope.ServiceProvider.GetRequiredService<HelpCommandService>();
await helpService.SetMyCommands().ConfigureAwait(false);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Явный вызов после builder.Build() выглядит несколько странно, но мб сойдет


ExampleDataContext dbContext = scope.ServiceProvider.GetRequiredService<ExampleDataContext>();
Directory.CreateDirectory("../data/sqlite");
await dbContext.Database.MigrateAsync().ConfigureAwait(false);
Expand Down
11 changes: 6 additions & 5 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"sdk": {
"version": "10.0.101",
"rollForward": "latestFeature"
}
{
"sdk": {
"version": "10.0.101",
"rollForward": "latestFeature",
"allowPrerelease": true

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мы не должны использовать пререлизный SDK — библиотека используется в проме

}
}
Loading