Skip to content

Commit cbc0ecd

Browse files
committed
some cleaning
1 parent bb4cc5e commit cbc0ecd

27 files changed

+124
-460
lines changed

.editorconfig

+46
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,52 @@ dotnet_diagnostic.CA1062.severity = silent
5555
# CA1031: Do not catch general exception types
5656
dotnet_diagnostic.CA1031.severity = silent
5757

58+
csharp_align_multiline_parameter = true
59+
csharp_align_multiline_extends_list = true
60+
csharp_align_linq_query = true
61+
csharp_place_attribute_on_same_line = false
62+
csharp_empty_block_style = together
63+
csharp_style_namespace_declarations = file_scoped
64+
csharp_style_var_when_type_is_apparent = true
65+
csharp_prefer_braces = when_multiline
66+
dotnet_diagnostic.IDE0011.severity = silent
67+
csharp_style_expression_bodied_constructors = when_on_single_line
68+
csharp_style_expression_bodied_methods = when_on_single_line
69+
csharp_style_expression_bodied_operators = when_on_single_line
70+
dotnet_diagnostic.IDE0046.severity = silent
71+
csharp_style_var_for_built_in_types = false
72+
dotnet_diagnostic.IDE0008.severity = silent
73+
74+
# IDE0022: Use expression body for method
75+
dotnet_diagnostic.IDE0022.severity = none
76+
77+
# IDE0040: Add accessibility modifiers
78+
dotnet_style_require_accessibility_modifiers = never
79+
80+
# CA1515: Consider making public types internal
81+
dotnet_diagnostic.CA1515.severity = none
82+
83+
# IDE0058: Expression value is never used
84+
dotnet_diagnostic.IDE0058.severity = none
85+
86+
# IDE0010: Add missing cases
87+
dotnet_diagnostic.IDE0010.severity = none
88+
89+
# CA2007: Consider calling ConfigureAwait on the awaited task
90+
dotnet_diagnostic.CA2007.severity = silent
91+
92+
# IDE0055: Fix formatting
93+
dotnet_diagnostic.IDE0055.severity = none
94+
95+
# IDE0021: Use expression body for constructor
96+
dotnet_diagnostic.IDE0021.severity = silent
97+
98+
# CS1998: Async method lacks 'await' operators and will run synchronously
99+
dotnet_diagnostic.CS1998.severity = silent
100+
101+
# SYSLIB1045: Convert to 'GeneratedRegexAttribute'.
102+
dotnet_diagnostic.SYSLIB1045.severity = silent
103+
58104
[*.md]
59105
trim_trailing_whitespace = false
60106
indent_size = 2

Console.Advanced/Abstract/IReceiverService.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
namespace Console.Advanced.Abstract;
22

3-
/// <summary>
4-
/// A marker interface for Update Receiver service
5-
/// </summary>
63
public interface IReceiverService
74
{
85
Task ReceiveAsync(CancellationToken stoppingToken);

Console.Advanced/Abstract/PollingServiceBase.cs

+10-31
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,34 @@
11
namespace Console.Advanced.Abstract;
22

3-
// A background service consuming a scoped service.
4-
// See more: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services#consuming-a-scoped-service-in-a-background-task
5-
/// <summary>
6-
/// An abstract class to compose Polling background service and Receiver implementation classes
7-
/// </summary>
3+
/// <summary>An abstract class to compose Polling background service and Receiver implementation classes</summary>
4+
/// <remarks>See more: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services#consuming-a-scoped-service-in-a-background-task</remarks>
85
/// <typeparam name="TReceiverService">Receiver implementation class</typeparam>
9-
public abstract class PollingServiceBase<TReceiverService> : BackgroundService
10-
where TReceiverService : IReceiverService
6+
public abstract class PollingServiceBase<TReceiverService>(IServiceProvider serviceProvider, ILogger<PollingServiceBase<TReceiverService>> logger)
7+
: BackgroundService where TReceiverService : IReceiverService
118
{
12-
private readonly IServiceProvider _serviceProvider;
13-
private readonly ILogger _logger;
14-
15-
internal PollingServiceBase(
16-
IServiceProvider serviceProvider,
17-
ILogger<PollingServiceBase<TReceiverService>> logger)
18-
{
19-
_serviceProvider = serviceProvider;
20-
_logger = logger;
21-
}
22-
239
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2410
{
25-
_logger.LogInformation("Starting polling service");
26-
11+
logger.LogInformation("Starting polling service");
2712
await DoWork(stoppingToken);
2813
}
2914

3015
private async Task DoWork(CancellationToken stoppingToken)
3116
{
32-
// Make sure we receive updates until Cancellation Requested,
33-
// no matter what errors our ReceiveAsync get
17+
// Make sure we receive updates until Cancellation Requested
3418
while (!stoppingToken.IsCancellationRequested)
3519
{
3620
try
3721
{
38-
// Create new IServiceScope on each iteration.
39-
// This way we can leverage benefits of Scoped TReceiverService
40-
// and typed HttpClient - we'll grab "fresh" instance each time
41-
using var scope = _serviceProvider.CreateScope();
22+
// Create new IServiceScope on each iteration. This way we can leverage benefits
23+
// of Scoped TReceiverService and typed HttpClient - we'll grab "fresh" instance each time
24+
using var scope = serviceProvider.CreateScope();
4225
var receiver = scope.ServiceProvider.GetRequiredService<TReceiverService>();
4326

4427
await receiver.ReceiveAsync(stoppingToken);
4528
}
46-
// Update Handler only captures exception inside update polling loop
47-
// We'll catch all other exceptions here
48-
// see: https://github.com/TelegramBots/Telegram.Bot/issues/1106
4929
catch (Exception ex)
5030
{
51-
_logger.LogError("Polling failed with exception: {Exception}", ex);
52-
31+
logger.LogError("Polling failed with exception: {Exception}", ex);
5332
// Cooldown if something goes wrong
5433
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
5534
}
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,23 @@
11
using Telegram.Bot;
22
using Telegram.Bot.Polling;
3-
using Telegram.Bot.Types.Enums;
43

54
namespace Console.Advanced.Abstract;
65

7-
/// <summary>
8-
/// An abstract class to compose Receiver Service and Update Handler classes
9-
/// </summary>
6+
/// <summary>An abstract class to compose Receiver Service and Update Handler classes</summary>
107
/// <typeparam name="TUpdateHandler">Update Handler to use in Update Receiver</typeparam>
11-
public abstract class ReceiverServiceBase<TUpdateHandler> : IReceiverService
12-
where TUpdateHandler : IUpdateHandler
8+
public abstract class ReceiverServiceBase<TUpdateHandler>(ITelegramBotClient botClient, TUpdateHandler updateHandler, ILogger<ReceiverServiceBase<TUpdateHandler>> logger)
9+
: IReceiverService where TUpdateHandler : IUpdateHandler
1310
{
14-
private readonly ITelegramBotClient _botClient;
15-
private readonly IUpdateHandler _updateHandler;
16-
private readonly ILogger<ReceiverServiceBase<TUpdateHandler>> _logger;
17-
18-
internal ReceiverServiceBase(
19-
ITelegramBotClient botClient,
20-
TUpdateHandler updateHandler,
21-
ILogger<ReceiverServiceBase<TUpdateHandler>> logger)
22-
{
23-
_botClient = botClient;
24-
_updateHandler = updateHandler;
25-
_logger = logger;
26-
}
27-
28-
/// <summary>
29-
/// Start to service Updates with provided Update Handler class
30-
/// </summary>
31-
/// <param name="stoppingToken"></param>
32-
/// <returns></returns>
11+
/// <summary>Start to service Updates with provided Update Handler class</summary>
3312
public async Task ReceiveAsync(CancellationToken stoppingToken)
3413
{
3514
// ToDo: we can inject ReceiverOptions through IOptions container
36-
var receiverOptions = new ReceiverOptions()
37-
{
38-
AllowedUpdates = [],
39-
DropPendingUpdates = true,
40-
};
15+
var receiverOptions = new ReceiverOptions() { DropPendingUpdates = true, AllowedUpdates = [] };
4116

42-
var me = await _botClient.GetMe(stoppingToken);
43-
_logger.LogInformation("Start receiving updates for {BotName}", me.Username ?? "My Awesome Bot");
17+
var me = await botClient.GetMe(stoppingToken);
18+
logger.LogInformation("Start receiving updates for {BotName}", me.Username ?? "My Awesome Bot");
4419

4520
// Start receiving updates
46-
await _botClient.ReceiveAsync(
47-
updateHandler: _updateHandler,
48-
receiverOptions: receiverOptions,
49-
cancellationToken: stoppingToken);
21+
await botClient.ReceiveAsync(updateHandler, receiverOptions, stoppingToken);
5022
}
5123
}

Console.Advanced/Console.Advanced.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
14-
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
14+
<PackageReference Include="Telegram.Bot" Version="22.1.3" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

Console.Advanced/Program.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
// Register Bot configuration
1010
services.Configure<BotConfiguration>(context.Configuration.GetSection("BotConfiguration"));
1111

12-
// Register named HttpClient to benefits from IHttpClientFactory
13-
// and consume it with ITelegramBotClient typed client.
14-
// More read:
15-
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-5.0#typed-clients
16-
// https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
12+
// Register named HttpClient to benefits from IHttpClientFactory and consume it with ITelegramBotClient typed client.
13+
// See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-5.0#typed-clients
14+
// and https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
1715
services.AddHttpClient("telegram_bot_client").RemoveAllLoggers()
1816
.AddTypedClient<ITelegramBotClient>((httpClient, sp) =>
1917
{

Console/Console.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
11+
<PackageReference Include="Telegram.Bot" Version="22.1.3" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

FSharp.Example/FSharp.Example.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
2222
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
23-
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
23+
<PackageReference Include="Telegram.Bot" Version="22.1.3" />
2424
</ItemGroup>
2525
</Project>

InlineQueries/InlineQueries.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
11+
<PackageReference Include="Telegram.Bot" Version="22.1.3" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

InlineQueries/Program.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Text.RegularExpressions;
22
using Telegram.Bot;
3-
using Telegram.Bot.Polling;
43
using Telegram.Bot.Types;
54
using Telegram.Bot.Types.Enums;
65
using Telegram.Bot.Types.InlineQueryResults;

MiniApp/MiniApp.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<UserSecretsId>a7997d6b-e2b6-456a-a171-14fb92006b82</UserSecretsId>
8+
<NoWarn>IDE0005;IDE0161;IDE0052;IDE0021;IDE0290</NoWarn>
89
</PropertyGroup>
910

1011
<ItemGroup>
11-
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
12+
<PackageReference Include="Telegram.Bot" Version="22.1.3" />
1213
</ItemGroup>
1314

1415
</Project>

Serverless/.editorconfig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*.cs]
2+
3+
# CA1848: Use the LoggerMessage delegates
4+
dotnet_diagnostic.CA1848.severity = none
5+
6+
# IDE0130: Namespace does not match folder structure
7+
dotnet_diagnostic.IDE0130.severity = none

Serverless/AwsLambda.Webhook/lambda-bot.Tests/lambda-bot.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
</PropertyGroup>

Serverless/AwsLambda.Webhook/lambda-bot/UpdateService.cs

+7-25
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,26 @@ namespace LambdaBot;
88

99
public class UpdateService
1010
{
11-
private readonly TelegramBotClient botClient;
12-
13-
public UpdateService()
14-
{
15-
// replace with your bot token
16-
botClient = new TelegramBotClient("<token>");
17-
}
11+
private readonly TelegramBotClient botClient = new("<token>"); // replace with your bot token
1812

1913
public async Task EchoAsync(Update update)
2014
{
21-
if (update is null)
22-
{
23-
return;
24-
}
25-
26-
if (!(update.Message is { } message))
27-
{
28-
return;
29-
}
15+
if (update is not { Message: { } message }) return;
3016

3117
LambdaLogger.Log("Received Message from " + message.Chat.Id);
32-
3318
switch (message.Type)
3419
{
35-
case MessageType.Text:
36-
// Echo each Message
20+
case MessageType.Text: // Let's echo text messages
3721
await botClient.SendMessage(message.Chat.Id, message.Text!);
3822
break;
3923

40-
case MessageType.Photo:
41-
// Download Photo
42-
string fileId = message.Photo![^1].FileId;
43-
File file = await botClient.GetFile(fileId);
24+
case MessageType.Photo: // Let's download the photo
25+
File file = await botClient.GetFile(message.Photo![^1].FileId);
4426

45-
string filename = file.FileId + "." + file.FilePath!.Split('.').Last();
27+
string filename = file.FileId + Path.GetExtension(file.FilePath);
4628
await using (FileStream saveImageStream = System.IO.File.Open(filename, FileMode.Create))
4729
{
48-
await botClient.DownloadFile(file.FilePath, saveImageStream);
30+
await botClient.DownloadFile(file.FilePath!, saveImageStream);
4931
}
5032

5133
await botClient.SendMessage(message.Chat.Id, "Thx for the Pics");

Serverless/AwsLambda.Webhook/lambda-bot/lambda-bot.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
@@ -14,6 +14,6 @@
1414
<PackageReference Include="AWSSDK.Core" Version="3.7.101" />
1515
<PackageReference Include="Amazon.Lambda.Core" Version="2.2.0" />
1616
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.2.0" />
17-
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
17+
<PackageReference Include="Telegram.Bot" Version="22.1.3" />
1818
</ItemGroup>
1919
</Project>

Serverless/AzureFunctions.IsolatedProcess.Webhook/.editorconfig

-4
This file was deleted.

0 commit comments

Comments
 (0)