Skip to content

Commit 12d6588

Browse files
committed
inject CommandContext
1 parent bec3645 commit 12d6588

7 files changed

Lines changed: 97 additions & 76 deletions

File tree

ZiziBot.TelegramBot.Framework/Extensions/ClientExtension.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public static IServiceCollection AddZiziBotTelegramBot(this IServiceCollection s
103103
services.AddSingleton<BotEngineHandler>();
104104
services.AddSingleton<BotClientCollection>();
105105
services.AddScoped<BotUpdateHandler>();
106+
services.AddScoped<CommandContext>();
106107

107108
return services;
108109
}

ZiziBot.TelegramBot.Framework/Handlers/BotUpdateHandler.cs

Lines changed: 61 additions & 60 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace ZiziBot.TelegramBot.Framework.Models;
2+
3+
public partial class CommandContext
4+
{
5+
internal void SetContext(CommandContext commandContext)
6+
{
7+
BotToken = commandContext.BotToken;
8+
BotClient = commandContext.BotClient;
9+
Update = commandContext.Update;
10+
EngineConfig = commandContext.EngineConfig;
11+
}
12+
}

ZiziBot.TelegramBot.Framework/Models/CommandContext.Request.cs renamed to ZiziBot.TelegramBot.Framework/Models/Context/CommandContext.Request.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public async Task<Message> SendMessage(
2828
ArgumentNullException.ThrowIfNull(Chat);
2929
ArgumentException.ThrowIfNullOrEmpty(text);
3030

31-
replyParameters ??= new ReplyParameters() {
31+
replyParameters ??= new ReplyParameters()
32+
{
3233
AllowSendingWithoutReply = true
3334
};
3435

ZiziBot.TelegramBot.Framework/Models/CommandContext.cs renamed to ZiziBot.TelegramBot.Framework/Models/Context/CommandContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace ZiziBot.TelegramBot.Framework.Models;
88

99
public partial class CommandContext
1010
{
11-
public string? BotToken { get; init; }
12-
public ITelegramBotClient? BotClient { get; set; }
13-
public Update? Update { get; set; }
14-
public BotEngineConfig? EngineConfig { get; set; }
11+
public string? BotToken { get; internal set; }
12+
public ITelegramBotClient? BotClient { get; internal set; }
13+
public Update? Update { get; internal set; }
14+
public BotEngineConfig? EngineConfig { get; internal set; }
1515

1616
#region Join Request
1717

ZiziBot.TelegramBot.Sample/Commands/InlineQueryCommands.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
using Telegram.Bot.Types.InlineQueryResults;
1+
using Telegram.Bot.Types.InlineQueryResults;
22
using ZiziBot.TelegramBot.Framework.Attributes;
33
using ZiziBot.TelegramBot.Framework.Models;
44

55
namespace ZiziBot.TelegramBot.Sample.Commands;
66

7-
public class InlineQueryCommands : BotCommandController
7+
public class InlineQueryCommands(CommandContext context) : BotCommandController
88
{
99
[InlineQuery]
1010
public async Task InlineQueryCommand()
1111
{
12-
await AnswerInlineQuery(new List<InlineQueryResult>() {
12+
await AnswerInlineQuery(new List<InlineQueryResult>()
13+
{
1314
new InlineQueryResultArticle("default-inline", "Default Inline", new InputTextMessageContent("Default Inline Content")),
1415
new InlineQueryResultArticle("hello-inline", "Hello Inline", new InputTextMessageContent("Hello Inline Content"))
1516
});
@@ -18,18 +19,20 @@ await AnswerInlineQuery(new List<InlineQueryResult>() {
1819
[InlineQuery("hello")]
1920
public async Task InlineQueryCommandHello()
2021
{
21-
await AnswerInlineQuery(new List<InlineQueryResult>() {
22+
await AnswerInlineQuery(new List<InlineQueryResult>()
23+
{
2224
new InlineQueryResultArticle("aa576dec-0727-4ea1-99ae-3c7cb20ea3c8", "Hello Inline", new InputTextMessageContent("Hello Inline Content"))
2325
});
2426
}
25-
27+
2628
[InlineQuery("id")]
2729
public async Task InlineQueryCommandId()
2830
{
2931
var message = $"ID: {Context.UserId}" +
3032
$"\nUsername: {Context.UserUsername}";
31-
32-
await AnswerInlineQuery(new List<InlineQueryResult>() {
33+
34+
await AnswerInlineQuery(new List<InlineQueryResult>()
35+
{
3336
new InlineQueryResultArticle("aa576dec-0727-4ea1-99ae-3c7cb20ea3c8", "Select to see your ID", new InputTextMessageContent(message))
3437
});
3538
}

ZiziBot.TelegramBot.Sample/Commands/SampleCommands.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
namespace ZiziBot.TelegramBot.Sample.Commands;
66

7-
public class SampleCommands : BotCommandController
7+
public class SampleCommands(CommandContext context) : BotCommandController
88
{
99
[Command("ping")]
1010
[TextCommand("ping")]
1111
public async Task PingCommand()
1212
{
1313
var replyMarkup = new InlineKeyboardMarkup([
1414
[
15-
new InlineKeyboardButton("Ping") {
15+
new InlineKeyboardButton("Ping")
16+
{
1617
CallbackData = "ping"
1718
},
18-
new InlineKeyboardButton("Foo Bar") {
19+
new InlineKeyboardButton("Foo Bar")
20+
{
1921
CallbackData = "foo bar"
2022
},
21-
new InlineKeyboardButton("lorem ipsum") {
23+
new InlineKeyboardButton("lorem ipsum")
24+
{
2225
CallbackData = "lorem ipsum dolor sit amet"
2326
}
2427
]

0 commit comments

Comments
 (0)