mirror of
https://github.com/yawaflua/Telegram.Net.git
synced 2025-12-08 19:49:30 +02:00
Merge pull request #5 from yawaflua/develop
pre-1.0.3 Fix error, when many commands with one base command(/start, /startapp, etc) not working properly. Fix versions of dependencies for better compatability
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices.JavaScript;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -15,31 +14,32 @@ using static System.Reflection.BindingFlags;
|
||||
|
||||
namespace Telegram.Net.Services;
|
||||
|
||||
[SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")]
|
||||
public class TelegramHostedService : IHostedService
|
||||
{
|
||||
private IServiceCollection isc { get; }
|
||||
internal TelegramBotClient Client { get; set; }
|
||||
private ITelegramBotConfig Config { get; }
|
||||
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>> CommandHandler { get; set; } = new();
|
||||
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>> EditedMessageHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery,CancellationToken, Task>> CallbackQueryHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery ,CancellationToken, Task>> InlineHandler { get; set; } = new();
|
||||
private IServiceCollection ServiceCollection { get; } = null!;
|
||||
internal TelegramBotClient Client { get; set; } = null!;
|
||||
private ITelegramBotConfig Config { get; } = null!;
|
||||
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>?> CommandHandler { get; set; } = new();
|
||||
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>?> EditedMessageHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery, CancellationToken, Task>?> CallbackQueryHandler { get; set; } = new();
|
||||
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery, CancellationToken, Task>?> InlineHandler { get; set; } = new();
|
||||
internal Func<ITelegramBotClient, PreCheckoutQuery,CancellationToken, Task>? PreCheckoutHandler { get; set; }
|
||||
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>> DefaultUpdateHandler { get; set; } = new();
|
||||
internal static ILogger<TelegramHostedService> _logger;
|
||||
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>?> DefaultUpdateHandler { get; set; } = new();
|
||||
internal static ILogger<TelegramHostedService> Logger = null!;
|
||||
|
||||
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection isc, ILogger<TelegramHostedService> logger)
|
||||
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection serviceCollection, ILogger<TelegramHostedService> logger)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger = logger;
|
||||
Logger = logger;
|
||||
Client = new TelegramBotClient(config.Token);
|
||||
Config = config;
|
||||
this.isc = isc;
|
||||
this.ServiceCollection = serviceCollection;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception when creating TelegramHostedService: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception when creating TelegramHostedService: ");
|
||||
}
|
||||
}
|
||||
internal static bool IsValidHandlerMethod(MethodInfo method, Type parameterType)
|
||||
@@ -55,12 +55,12 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Catched exception in parsing and checking params.");
|
||||
Logger.LogError(ex, "Catched exception in parsing and checking params.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Func<ITelegramBotClient, T, CancellationToken, Task> CreateDelegate<T>(MethodInfo method)
|
||||
internal static Func<ITelegramBotClient, T, CancellationToken, Task>? CreateDelegate<T>(MethodInfo method)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -70,7 +70,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception in CreateDelegate function: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched exception in CreateDelegate function: ");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -106,10 +106,10 @@ public class TelegramHostedService : IHostedService
|
||||
|
||||
if (methods.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No methods found with required attributes");
|
||||
Logger.LogWarning("No methods found with required attributes");
|
||||
}
|
||||
|
||||
var isp = isc.BuildServiceProvider();
|
||||
var isp = ServiceCollection.BuildServiceProvider();
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var declaringType = method.DeclaringType!;
|
||||
@@ -156,7 +156,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Catched new exception when added methods: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Catched new exception when added methods: ");
|
||||
}
|
||||
}, cancellationToken);
|
||||
}
|
||||
@@ -169,31 +169,46 @@ public class TelegramHostedService : IHostedService
|
||||
switch (update)
|
||||
{
|
||||
case { Message: { } message }:
|
||||
await CommandHandler.FirstOrDefault(k => message.Text!.StartsWith(k.Key))
|
||||
.Value(client, message, ctx);
|
||||
CommandHandler.Where(k => message.Text!.StartsWith(k.Key)).Select(async k =>
|
||||
{
|
||||
await k.Value!(client, message, ctx);
|
||||
return k;
|
||||
});
|
||||
break;
|
||||
case { EditedMessage: { } message }:
|
||||
EditedMessageHandler.ForEach(async k => await k(client, message, ctx));
|
||||
// ReSharper disable once AsyncVoidLambda
|
||||
EditedMessageHandler.ForEach(async k => await k!(client, message, ctx));
|
||||
break;
|
||||
case { CallbackQuery: { } callbackQuery }:
|
||||
await CallbackQueryHandler.FirstOrDefault(k => callbackQuery.Data!.StartsWith(k.Key))
|
||||
.Value(client, callbackQuery, ctx);
|
||||
CallbackQueryHandler.Where(k => callbackQuery.Data!.StartsWith(k.Key))
|
||||
.Select(async k =>
|
||||
{
|
||||
await k.Value!(client, callbackQuery, ctx);
|
||||
return k;
|
||||
});
|
||||
break;
|
||||
case { InlineQuery: { } inlineQuery }:
|
||||
await InlineHandler.FirstOrDefault(k => inlineQuery.Id.StartsWith(k.Key))
|
||||
.Value(client, inlineQuery, ctx);
|
||||
InlineHandler.Where(k => inlineQuery.Id.StartsWith(k.Key)).Select(async k =>
|
||||
{
|
||||
await k.Value!(client, inlineQuery, ctx);
|
||||
return k;
|
||||
});
|
||||
break;
|
||||
case { PreCheckoutQuery: { } preCheckoutQuery }:
|
||||
if (PreCheckoutHandler != null) await PreCheckoutHandler(client, preCheckoutQuery, ctx);
|
||||
break;
|
||||
default:
|
||||
DefaultUpdateHandler.ForEach(async k => await k(client, update, ctx));
|
||||
// ReSharper disable once AsyncVoidLambda
|
||||
DefaultUpdateHandler.ForEach(async k => await k!(client, update, ctx));
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Error, new EventId(), ex, "Catched exception in UpdateHandler: ");
|
||||
if (ex is KeyNotFoundException)
|
||||
Logger.Log(LogLevel.Warning, new EventId(), ex, "Key not found: ");
|
||||
else
|
||||
Logger.Log(LogLevel.Error, new EventId(), ex, "Caught exception in UpdateHandler: ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +225,7 @@ public class TelegramHostedService : IHostedService
|
||||
UpdateHandler,
|
||||
Config.errorHandler ?? ((_, ex, _) =>
|
||||
{
|
||||
_logger.LogError(ex, "Catched error in telegram bot working: ");
|
||||
Logger.LogError(ex, "Catched error in telegram bot working: ");
|
||||
return Task.CompletedTask;
|
||||
}),
|
||||
Config.ReceiverOptions,
|
||||
@@ -218,7 +233,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Log(LogLevel.Critical, new EventId(), ex, "Failed to start. Catched exception: ");
|
||||
Logger.Log(LogLevel.Critical, new EventId(), ex, "Failed to start. Catched exception: ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,7 +245,7 @@ public class TelegramHostedService : IHostedService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogCritical(ex, "Failed to stop. Exception: ");
|
||||
Logger.LogCritical(ex, "Failed to stop. Exception: ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>1.0.2</Version>
|
||||
<Version>1.0.3</Version>
|
||||
<Authors>yawaflua</Authors>
|
||||
<Title>yawaflua.Telegram.Net</Title>
|
||||
<Description>Telegram.Bots extender pack, what provides to user attributes, which can used with services</Description>
|
||||
@@ -19,8 +19,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.3" />
|
||||
<PackageReference Include="Telegram.Bot" Version="22.4.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="(6.0.0,)" />
|
||||
<PackageReference Include="Telegram.Bot" Version="22.4.*" />
|
||||
<None Include="..\README.md" Pack="true" PackagePath="\"/>
|
||||
<None Include="..\LICENSE" Pack="true" PackagePath=""/>
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user