Repository navigation
добавил сервис для изменения команд в боте #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тогда лучше разделить команды для чата и пользователя, если это необходимо |
||
| int? messageId = Context.GetMessageId(); | ||
| await botClient.SendMessage( | ||
| Context.GetChatId()!, | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Его можно не наследовать от |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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); | ||
|
|
@@ -73,6 +77,9 @@ | |
| AsyncServiceScope scope = scopeFactory.CreateAsyncScope(); | ||
| await using (scope.ConfigureAwait(false)) | ||
| { | ||
| var helpService = scope.ServiceProvider.GetRequiredService<HelpCommandService>(); | ||
| await helpService.SetMyCommands().ConfigureAwait(false); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Явный вызов после |
||
|
|
||
| ExampleDataContext dbContext = scope.ServiceProvider.GetRequiredService<ExampleDataContext>(); | ||
| Directory.CreateDirectory("../data/sqlite"); | ||
| await dbContext.Database.MigrateAsync().ConfigureAwait(false); | ||
|
|
||
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мы не должны использовать пререлизный SDK — библиотека используется в проме |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно попробовать реализовать через
IHostedService, тогда его будет проще вызывать DI