* Added WideBot csproj data

* Added TelegratorWideClient
* Added WClient extension property for handlers
* Code cleanup
This commit is contained in:
gutii
2026-04-27 16:28:20 +04:00
parent aba9cf4037
commit 0e0a280308
15 changed files with 201 additions and 78 deletions
-47
View File
@@ -1,47 +0,0 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Telegrator.Hosting;
using Telegrator.Hosting.Web;
namespace Telegrator;
internal static class Program
{
public static void WebApplicationBuilder_Example(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
Args = args,
ApplicationName = "WebApplication example",
});
builder.AddTelegratorWeb(action: builder => builder.Handlers
.CollectHandlersAssemblyWide());
builder.Build()
.UseTelegratorWeb(dontMap: true)
.RemapWebhook("https://amazing-butt-sex.cloudpub.ru/")
.AddLoggingAdapter()
.SetBotCommands()
.Run();
}
public static void HostApplicationBuilder_Example(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings()
{
Args = args,
ApplicationName = "Host example",
});
builder.AddTelegrator(action: builder => builder.Handlers
.CollectHandlersAssemblyWide());
builder.Build()
.UseTelegrator()
.AddLoggingAdapter()
.SetBotCommands()
.Run();
}
}
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting.Web</Title>
<Version>1.16.9</Version>
<Version>1.17.0</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -40,4 +40,5 @@
<None Include="..\..\LICENSE" Pack="True" PackagePath="\" />
<None Include="..\..\resources\telegrator_nuget.png" Pack="True" PackagePath="\" />
</ItemGroup>
</Project>
+18 -14
View File
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -8,7 +7,6 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegrator.Core;
using Telegrator.Hosting;
using Telegrator.Hosting.Web;
@@ -18,17 +16,10 @@ using Telegrator.Providers;
namespace Telegrator;
/// <summary>
/// Contains extensions for <see cref="IServiceCollection"/>
/// Provides method to configure Telegram Bot WebHost
/// Provides extension methods for <see cref="IHostApplicationBuilder"/> to configure Telegrator.
/// </summary>
public static class ServicesCollectionExtensions
public static class WebHostBuilderExtensions
{
public static IServiceCollection ConfigureWebhooker(this IServiceCollection services, WebhookerOptions options)
{
services.AddSingleton(Options.Create(options));
return services;
}
/// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary>
@@ -103,10 +94,17 @@ public static class ServicesCollectionExtensions
}
/// <summary>
/// Provides useful methods to adjust Telegram bot Host
/// Contains extensions for <see cref="IServiceCollection"/>
/// Provides method to configure Telegram Bot WebHost
/// </summary>
public static class TelegramBotHostExtensions
public static class WebServicesCollectionExtensions
{
public static IServiceCollection ConfigureWebhooker(this IServiceCollection services, WebhookerOptions options)
{
services.AddSingleton(Options.Create(options));
return services;
}
/// <summary>
/// Searchs for <see cref="HostedUpdateWebhooker"/> hosted service inside hosts services
/// </summary>
@@ -118,7 +116,13 @@ public static class TelegramBotHostExtensions
webhooker = services.GetServices<IHostedService>().FirstOrDefault(s => s is HostedUpdateWebhooker) as HostedUpdateWebhooker;
return webhooker != null;
}
}
/// <summary>
/// Provides useful methods to adjust Telegram bot Host
/// </summary>
public static class WebTelegramBotHostExtensions
{
/// <summary>
/// Replaces the initialization logic from TelegramBotWebHost constructor.
/// Initializes the bot and logs handlers on application startup.
+1
View File
@@ -0,0 +1 @@
@@ -2,9 +2,28 @@
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Telegrator</RootNamespace>
<LangVersion>latest</LangVersion>
<RootNamespace>Telegrator</RootNamespace>
<BaseOutputPath>..\..\bin</BaseOutputPath>
<DocumentationFile>..\..\docs\$(AssemblyName).xml</DocumentationFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting.WideBot</Title>
<Version>1.17.0</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
<PackageTags>telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers;wtelegrambot;userbot</PackageTags>
<PackageIcon>telegrator_nuget.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
@@ -15,4 +34,10 @@
<ProjectReference Include="..\..\src\Telegrator.Hosting\Telegrator.Hosting.csproj" />
</ItemGroup>
<ItemGroup>
<None Include=".\README.md" Pack="True" PackagePath="\" />
<None Include="..\..\LICENSE" Pack="True" PackagePath="\" />
<None Include="..\..\resources\telegrator_nuget.png" Pack="True" PackagePath="\" />
</ItemGroup>
</Project>
@@ -1,6 +1,5 @@
// Maybe later...
/*
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Net.Http;
using System.Threading;
@@ -55,8 +54,8 @@ public class TelegratorWClient : WTelegramBotClient, ITelegratorBot, ICollecting
{
try
{
await new HostedWideBotUpdateReceiver(this)
.ReceiveAsync(UpdateRouter, cancellationToken)
await new HostedWideBotUpdateReceiver(NullLoggerFactory.Instance.CreateLogger<HostedWideBotUpdateReceiver>(), this, UpdateRouter, null)
.StartAsync(cancellationToken)
.ConfigureAwait(false);
}
catch (Exception exception)
@@ -72,5 +71,4 @@ public class TelegratorWClient : WTelegramBotClient, ITelegratorBot, ICollecting
TelegratorLogging.LogInformation("Telegrator bot stopped (cancelled)");
}
}
}
*/
}
@@ -27,13 +27,26 @@ public static class HandlersExtensions
{
extension<TUpdate>(AbstractUpdateHandler<TUpdate> handler) where TUpdate : class
{
public WUpdate WUpdate
public WTelegramBotClient WClient
{
get
{
object? update = typeof(AbstractUpdateHandler<TUpdate>).GetField("HandlingUpdate")?.GetValue(handler);
object? client = handler.GetType().GetField("Client")?.GetValue(handler);
if (client is not WTelegramBotClient wideClient)
throw new InvalidCastException("Client is not assignable to `WTelegram.Bot.WTelegramBotClient`");
return wideClient;
}
}
public WUpdate WideUpdate
{
get
{
object? update = handler.GetType().GetField("HandlingUpdate")?.GetValue(handler);
if (update is not WUpdate wUpdate)
throw new InvalidCastException();
throw new InvalidCastException("Update is not assignable to `WTelegram.Types.Update`");
return wUpdate;
}
@@ -41,7 +54,7 @@ public static class HandlersExtensions
public TLUpdate? TLUpdate
{
get => handler.WUpdate.TLUpdate;
get => handler.WideUpdate.TLUpdate;
}
}
@@ -55,14 +68,8 @@ public static class HandlersExtensions
/// <summary>
/// Provides extension methods for <see cref="IHostApplicationBuilder"/> to configure Telegrator.
/// </summary>
public static class ServiceCollectionExtensions
public static class WideHostBuilderExtensions
{
public static IServiceCollection ConfigureWideTelegram(this IServiceCollection services, WTelegramBotClientOptions options)
{
services.AddSingleton(Options.Create(options));
return services;
}
/// <summary>
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary>
@@ -133,6 +140,19 @@ public static class ServiceCollectionExtensions
services.AddTelegramBotHostDefaults();
services.AddMTProtoUpdateReceiver();
}
}
/// <summary>
/// Contains extensions for <see cref="IServiceCollection"/>
/// Provides method to configure Telegram Bot WebHost
/// </summary>
public static class WideBotServiceCollectionExtensions
{
public static IServiceCollection ConfigureWideTelegram(this IServiceCollection services, WTelegramBotClientOptions options)
{
services.AddSingleton(Options.Create(options));
return services;
}
public static IServiceCollection AddMTProtoUpdateReceiver(this IServiceCollection services)
{
@@ -149,8 +169,12 @@ public static class ServiceCollectionExtensions
/// <summary>
/// Provides useful methods to adjust Telegram bot Host
/// </summary>
public static class TelegramBotHostExtensions
public static class WideTelegramBotHostExtensions
{
/// <summary>
/// Replaces the initialization logic from TelegramBotWebHost constructor.
/// Initializes the bot and logs handlers on application startup.
/// </summary>
public static IHost UseWideTelegrator(this IHost botHost)
{
if (!botHost.Services.TryFindWTelegramBotClient())
@@ -32,11 +32,13 @@ public class MicrosoftLoggingAdapter : ITelegratorLogger
if (exception != null)
{
_logger.Log(msLogLevel, default, message, exception, (str, exc) => string.Format("{0} : {1}", str, exc));
_logger.Log(msLogLevel, default, message, exception,
(str, exc) => string.Format("{0} : {1}", str, exc));
}
else
{
_logger.Log(msLogLevel, default, message, null, (str, _) => str);
_logger.Log(msLogLevel, default, message, null,
(str, _) => str);
}
}
}
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting</Title>
<Version>1.16.9</Version>
<Version>1.17.0</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
+1 -1
View File
@@ -127,7 +127,7 @@ public static class HostBuilderExtensions
/// Contains extensions for <see cref="IServiceCollection"/>
/// Provides method to configure Telegram Bot Host
/// </summary>
public static class ServicesCollectionExtensions
public static class HostServicesCollectionExtensions
{
public static IServiceCollection ConfigureTelegram(this IServiceCollection services, TelegramBotClientOptions options)
{
+1 -2
View File
@@ -14,7 +14,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator : Telegram.Bot mediator framework</Title>
<Version>1.16.9</Version>
<Version>1.17.0</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -23,7 +23,6 @@
<PackageIcon>telegrator_nuget.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<UserSecretsId>b78bc62d-e49f-4ef0-b2de-ff4ceb3971af</UserSecretsId>
</PropertyGroup>
<ItemGroup>