Skip to content

Commit 0a7e3d4

Browse files
committed
Fix warnings and change versions of dependencies for better compatability
1 parent 7b7b2ef commit 0a7e3d4

2 files changed

Lines changed: 40 additions & 33 deletions

File tree

Telegram.Net/Services/TelegramHostedService.cs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Reflection;
3-
using System.Runtime.InteropServices.JavaScript;
43
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.Extensions.Hosting;
65
using Microsoft.Extensions.Logging;
@@ -15,31 +14,32 @@
1514

1615
namespace Telegram.Net.Services;
1716

17+
[SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")]
1818
public class TelegramHostedService : IHostedService
1919
{
20-
private IServiceCollection isc { get; }
21-
internal TelegramBotClient Client { get; set; }
22-
private ITelegramBotConfig Config { get; }
23-
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>> CommandHandler { get; set; } = new();
24-
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>> EditedMessageHandler { get; set; } = new();
25-
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery,CancellationToken, Task>> CallbackQueryHandler { get; set; } = new();
26-
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery ,CancellationToken, Task>> InlineHandler { get; set; } = new();
20+
private IServiceCollection ServiceCollection { get; } = null!;
21+
internal TelegramBotClient Client { get; set; } = null!;
22+
private ITelegramBotConfig Config { get; } = null!;
23+
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>?> CommandHandler { get; set; } = new();
24+
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>?> EditedMessageHandler { get; set; } = new();
25+
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery, CancellationToken, Task>?> CallbackQueryHandler { get; set; } = new();
26+
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery, CancellationToken, Task>?> InlineHandler { get; set; } = new();
2727
internal Func<ITelegramBotClient, PreCheckoutQuery,CancellationToken, Task>? PreCheckoutHandler { get; set; }
28-
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>> DefaultUpdateHandler { get; set; } = new();
29-
internal static ILogger<TelegramHostedService> _logger;
28+
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>?> DefaultUpdateHandler { get; set; } = new();
29+
internal static ILogger<TelegramHostedService> Logger = null!;
3030

31-
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection isc, ILogger<TelegramHostedService> logger)
31+
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection serviceCollection, ILogger<TelegramHostedService> logger)
3232
{
3333
try
3434
{
35-
_logger = logger;
35+
Logger = logger;
3636
Client = new TelegramBotClient(config.Token);
3737
Config = config;
38-
this.isc = isc;
38+
this.ServiceCollection = serviceCollection;
3939
}
4040
catch (Exception ex)
4141
{
42-
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception when creating TelegramHostedService: ");
42+
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception when creating TelegramHostedService: ");
4343
}
4444
}
4545
internal static bool IsValidHandlerMethod(MethodInfo method, Type parameterType)
@@ -55,12 +55,12 @@ internal static bool IsValidHandlerMethod(MethodInfo method, Type parameterType)
5555
}
5656
catch (Exception ex)
5757
{
58-
_logger.LogError(ex, "Catched exception in parsing and checking params.");
58+
Logger.LogError(ex, "Catched exception in parsing and checking params.");
5959
return false;
6060
}
6161
}
6262

63-
internal static Func<ITelegramBotClient, T, CancellationToken, Task> CreateDelegate<T>(MethodInfo method)
63+
internal static Func<ITelegramBotClient, T, CancellationToken, Task>? CreateDelegate<T>(MethodInfo method)
6464
{
6565
try
6666
{
@@ -70,7 +70,7 @@ internal static Func<ITelegramBotClient, T, CancellationToken, Task> CreateDeleg
7070
}
7171
catch (Exception ex)
7272
{
73-
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception in CreateDelegate function: ");
73+
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception in CreateDelegate function: ");
7474
return null;
7575
}
7676

@@ -106,10 +106,10 @@ await Task.Run(async () =>
106106

107107
if (methods.Count == 0)
108108
{
109-
_logger.LogWarning("No methods found with required attributes");
109+
Logger.LogWarning("No methods found with required attributes");
110110
}
111111

112-
var isp = isc.BuildServiceProvider();
112+
var isp = ServiceCollection.BuildServiceProvider();
113113
foreach (var method in methods)
114114
{
115115
var declaringType = method.DeclaringType!;
@@ -156,7 +156,7 @@ await Task.Run(async () =>
156156
}
157157
catch (Exception ex)
158158
{
159-
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched new exception when added methods: ");
159+
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched new exception when added methods: ");
160160
}
161161
}, cancellationToken);
162162
}
@@ -171,39 +171,44 @@ internal async Task UpdateHandler(ITelegramBotClient client, Update update, Canc
171171
case { Message: { } message }:
172172
CommandHandler.Where(k => message.Text!.StartsWith(k.Key)).Select(async k =>
173173
{
174-
await k.Value(client, message, ctx);
174+
await k.Value!(client, message, ctx);
175175
return k;
176176
});
177177
break;
178178
case { EditedMessage: { } message }:
179-
EditedMessageHandler.ForEach(async k => await k(client, message, ctx));
179+
// ReSharper disable once AsyncVoidLambda
180+
EditedMessageHandler.ForEach(async k => await k!(client, message, ctx));
180181
break;
181182
case { CallbackQuery: { } callbackQuery }:
182183
CallbackQueryHandler.Where(k => callbackQuery.Data!.StartsWith(k.Key))
183184
.Select(async k =>
184185
{
185-
await k.Value(client, callbackQuery, ctx);
186+
await k.Value!(client, callbackQuery, ctx);
186187
return k;
187188
});
188189
break;
189190
case { InlineQuery: { } inlineQuery }:
190191
InlineHandler.Where(k => inlineQuery.Id.StartsWith(k.Key)).Select(async k =>
191192
{
192-
await k.Value(client, inlineQuery, ctx);
193+
await k.Value!(client, inlineQuery, ctx);
193194
return k;
194195
});
195196
break;
196197
case { PreCheckoutQuery: { } preCheckoutQuery }:
197198
if (PreCheckoutHandler != null) await PreCheckoutHandler(client, preCheckoutQuery, ctx);
198199
break;
199200
default:
200-
DefaultUpdateHandler.ForEach(async k => await k(client, update, ctx));
201+
// ReSharper disable once AsyncVoidLambda
202+
DefaultUpdateHandler.ForEach(async k => await k!(client, update, ctx));
201203
break;
202204
}
203205
}
204206
catch (Exception ex)
205207
{
206-
_logger.Log(LogLevel.Error, new EventId(), ex, "Catched exception in UpdateHandler: ");
208+
if (ex is KeyNotFoundException)
209+
Logger.Log(LogLevel.Warning, new EventId(), ex, "Key not found: ");
210+
else
211+
Logger.Log(LogLevel.Error, new EventId(), ex, "Caught exception in UpdateHandler: ");
207212
}
208213
}
209214

@@ -220,15 +225,15 @@ public async Task StartAsync(CancellationToken cancellationToken)
220225
UpdateHandler,
221226
Config.errorHandler ?? ((_, ex, _) =>
222227
{
223-
_logger.LogError(ex, "Catched error in telegram bot working: ");
228+
Logger.LogError(ex, "Catched error in telegram bot working: ");
224229
return Task.CompletedTask;
225230
}),
226231
Config.ReceiverOptions,
227232
cancellationToken);
228233
}
229234
catch (Exception ex)
230235
{
231-
_logger.Log(LogLevel.Critical, new EventId(), ex, "Failed to start. Catched exception: ");
236+
Logger.Log(LogLevel.Critical, new EventId(), ex, "Failed to start. Catched exception: ");
232237
}
233238
}
234239

@@ -240,7 +245,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
240245
}
241246
catch (Exception ex)
242247
{
243-
_logger.LogCritical(ex, "Failed to stop. Exception: ");
248+
Logger.LogCritical(ex, "Failed to stop. Exception: ");
244249
}
245250
}
246251
}

Telegram.Net/Telegram.Net.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8-
<Version>1.0.2</Version>
8+
<Version>1.0.3</Version>
99
<Authors>yawaflua</Authors>
1010
<Title>yawaflua.Telegram.Net</Title>
1111
<Description>Telegram.Bots extender pack, what provides to user attributes, which can used with services</Description>
@@ -19,8 +19,10 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.3" />
23-
<PackageReference Include="Telegram.Bot" Version="22.4.4" />
22+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version=">=7.0.0" />
23+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version=">=7.0.0" />
24+
<PackageReference Include="Microsoft.Extensions.Logging" Version=">=7.0.0" />
25+
<PackageReference Include="Telegram.Bot" Version="22.4.*" />
2426
<None Include="..\README.md" Pack="true" PackagePath="\"/>
2527
<None Include="..\LICENSE" Pack="true" PackagePath=""/>
2628
</ItemGroup>

0 commit comments

Comments
 (0)