Skip to content

Commit 1d1d453

Browse files
committed
Refactor methods and update package versions
Replaced `SendTextMessageAsync` with `SendMessage` across multiple files. Changed `ParseMode` in `InlineResult.cs` to non-nullable with default `ParseMode.None`. Updated `Microsoft.EntityFrameworkCore.Sqlite` and `Microsoft.EntityFrameworkCore.Tools` to `9.0.0` in `TelegramBot.ConsoleTest.csproj`. Updated `Telegram.Bot` to `22.0.2` and other packages to `9.0.0` in `TelegramBot.csproj`. Added using directives in `BotApp.cs`. Replaced `GetMeAsync` with `GetMe` in `BotApp.cs`. Adjusted order of operations and refactored `HostApplicationLifetime` logic in `BotApp.cs`. Removed unused using directives.
1 parent 6d0333f commit 1d1d453

9 files changed

Lines changed: 35 additions & 33 deletions

File tree

Sources/TelegramBot.ConsoleTest/Controllers/CafeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<IActionResult> HandleBurgersAsync(int burgers, bool drink)
4848
$"Total: ${totalPrice}.00\n" +
4949
"Send /burgersdone when you are ready to pick up";
5050

51-
await telegramBotClient.SendTextMessageAsync(adminId, orderText);
51+
await telegramBotClient.SendMessage(adminId, orderText);
5252
SetValue("customerId", User.Id);
5353
Delete();
5454
return Text(text);
@@ -67,7 +67,7 @@ public async Task<IActionResult> HandleBurgersDoneAsync()
6767
{
6868
return Text("You have no active orders");
6969
}
70-
await telegramBotClient.SendTextMessageAsync(customerId, text);
70+
await telegramBotClient.SendMessage(customerId, text);
7171
return Text(text);
7272
}
7373

Sources/TelegramBot.ConsoleTest/TelegramBot.ConsoleTest.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<Content Include="appsettings.json">
1212
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1313
</Content>
14-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
1616
<PrivateAssets>all</PrivateAssets>
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
</PackageReference>

Sources/TelegramBot/ActionResults/FileResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void Dispose()
6060
public Task ExecuteResultAsync(ActionContext context)
6161
{
6262
InputFile file = InputFile.FromStream(_fileStream, _fileName);
63-
return context.Bot.SendDocumentAsync(context.ChatId, document: file);
63+
return context.Bot.SendDocument(context.ChatId, document: file);
6464
}
6565
}
6666
}

Sources/TelegramBot/ActionResults/InlineResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public InlineResult(string text, InlineKeyboardMarkup keyboard, bool useMarkdown
4646
/// <returns>The task representing the result of the action.</returns>
4747
public async Task ExecuteResultAsync(ActionContext context)
4848
{
49-
ParseMode? parseMode = null;
49+
ParseMode parseMode = ParseMode.None;
5050
if (UseMarkdown)
5151
{
5252
parseMode = ParseMode.MarkdownV2;
5353
}
54-
await context.Bot.SendTextMessageAsync(context.ChatId, Text, replyMarkup: Keyboard, parseMode: parseMode);
54+
await context.Bot.SendMessage(context.ChatId, Text, replyMarkup: Keyboard, parseMode: parseMode);
5555
}
5656
}
5757
}

Sources/TelegramBot/ActionResults/MarkdownResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MarkdownResult(string text)
3131
/// <returns>The task representing the result of the action.</returns>
3232
public async Task ExecuteResultAsync(ActionContext context)
3333
{
34-
await context.Bot.SendTextMessageAsync(context.ChatId, Text, parseMode: ParseMode.MarkdownV2);
34+
await context.Bot.SendMessage(context.ChatId, Text, parseMode: ParseMode.MarkdownV2);
3535
}
3636
}
3737
}

Sources/TelegramBot/ActionResults/TextResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public TextResult(string text)
3030
/// <returns>The task representing the result of the action.</returns>
3131
public async Task ExecuteResultAsync(ActionContext context)
3232
{
33-
await context.Bot.SendTextMessageAsync(context.ChatId, Text);
33+
await context.Bot.SendMessage(context.ChatId, Text);
3434
}
3535
}
3636
}

Sources/TelegramBot/BotApp.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
using System.Reflection;
66
using Telegram.Bot.Types;
77
using TelegramBot.Handlers;
8+
using TelegramBot.Services;
9+
using TelegramBot.Builders;
10+
using TelegramBot.Attributes;
811
using System.Threading.Tasks;
912
using TelegramBot.Extensions;
1013
using TelegramBot.Controllers;
1114
using TelegramBot.Abstractions;
1215
using System.Collections.Generic;
16+
using Microsoft.Extensions.Hosting;
1317
using Microsoft.Extensions.Logging;
1418
using Microsoft.Extensions.DependencyInjection;
15-
using Microsoft.Extensions.Hosting;
16-
using Newtonsoft.Json.Linq;
17-
using TelegramBot.Attributes;
18-
using TelegramBot.Services;
19-
using TelegramBot.Builders;
2019

2120
namespace TelegramBot
2221
{
@@ -104,7 +103,7 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
104103
CheckDisposed();
105104
try
106105
{
107-
var botUser = _client.GetMeAsync().Result;
106+
var botUser = _client.GetMe().Result;
108107
_client.StartReceiving(UpdateHandler, ErrorHandler, cancellationToken: mergedToken);
109108
_logger.LogInformation("Bot '{botUser}' started - receiving updates.", botUser.Username);
110109
}
@@ -117,28 +116,31 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
117116
_logger.LogError(ex, "Error occurred while starting the bot. Probably the bot token is invalid or the network is not available.");
118117
throw ex;
119118
}
119+
120+
var hostApplicationLifetime = _serviceProvider.GetService<IHostApplicationLifetime>() as HostApplicationLifetime
121+
?? throw new InvalidOperationException("Host application lifetime is not registered.");
122+
hostApplicationLifetime.NotifyStarted();
123+
120124
var hostedServices = _serviceProvider.GetServices<IHostedService>();
121125
foreach (var hostedService in hostedServices)
122126
{
123127
await hostedService.StartAsync(mergedToken);
124128
_logger.LogInformation("Started '{hostedService}'.", hostedService.GetType().Name);
125129
}
126-
var hostApplicationLifetime = _serviceProvider.GetService<IHostApplicationLifetime>()
127-
as HostApplicationLifetime ?? throw new InvalidOperationException("Host application lifetime is not registered.");
130+
128131
var commandRegistrationBuilders = _serviceProvider.GetServices<CommandRegistrationBuilder>();
129132
if (commandRegistrationBuilders != null && commandRegistrationBuilders.Any())
130133
{
131134
foreach (var builder in commandRegistrationBuilders)
132135
{
133136
var commands = builder.Build();
134-
await _client.SetMyCommandsAsync(commands,
137+
await _client.SetMyCommands(commands,
135138
languageCode: builder.Language,
136139
cancellationToken: mergedToken);
137140
_logger.LogInformation("Registered {count} commands for language '{language}'.",
138141
commands.Count(), builder.Language);
139142
}
140143
}
141-
hostApplicationLifetime.NotifyStarted();
142144
}
143145

144146
private void OnProcessExit(object sender, EventArgs e)

Sources/TelegramBot/Controllers/BotControllerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void Delete()
137137
}
138138
try
139139
{
140-
Client.DeleteMessageAsync(User.Id, messageId);
140+
Client.DeleteMessage(User.Id, messageId);
141141
}
142142
catch (Exception) { }
143143
}

Sources/TelegramBot/TelegramBot.csproj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@
2828
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
2929
<None Include="icon.png" Pack="true" PackagePath="\" />
3030
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md" />
31-
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
32-
<PackageReference Include="System.Text.Json" Version="8.0.4" />
33-
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
34-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
35-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
36-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
37-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
38-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
39-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
40-
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />
41-
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" />
42-
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
31+
<PackageReference Include="Telegram.Bot" Version="22.0.2" />
32+
<PackageReference Include="System.Text.Json" Version="9.0.0" />
33+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
34+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
35+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
36+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
37+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
38+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
39+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
40+
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="9.0.0" />
41+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="9.0.0" />
42+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
4343
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
44-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
44+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
4545
</ItemGroup>
4646
</Project>

0 commit comments

Comments
 (0)