* Added building delegate to UseTelegrator method
* Fixed Webhooker trying to resolve IEndpointRouteBuildr from DI
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
|
||||
<Title>Telegrator.RedisStateStorage</Title>
|
||||
<Version>1.16.5</Version>
|
||||
<Version>1.16.6</Version>
|
||||
<Authors>Rikitav Tim4ik</Authors>
|
||||
<Company>Rikitav Tim4ik</Company>
|
||||
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
|
||||
|
||||
@@ -39,11 +39,13 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
|
||||
/// </summary>
|
||||
public ILogger<TelegramBotWebHost> Logger => _logger;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IDictionary<string, object?> Properties => ((IApplicationBuilder)_innerApp).Properties;
|
||||
|
||||
// Private interface fields
|
||||
IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
|
||||
IServiceProvider IApplicationBuilder.ApplicationServices { get => Services; set => throw new NotImplementedException(); }
|
||||
IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures;
|
||||
IDictionary<string, object?> IApplicationBuilder.Properties => ((IApplicationBuilder)_innerApp).Properties;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WebApplicationBuilder"/> class.
|
||||
@@ -68,9 +70,7 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings);
|
||||
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramWebhook();
|
||||
builder.AddTelegratorWeb();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -83,9 +83,7 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings);
|
||||
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramWebhook();
|
||||
builder.AddTelegratorWeb();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -97,7 +95,9 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings);
|
||||
return new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
builder.AddTelegratorWeb();
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Telegrator.Hosting.Web;
|
||||
/// <summary>
|
||||
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
|
||||
/// </summary>
|
||||
public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingProvider
|
||||
public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
|
||||
{
|
||||
private readonly WebApplicationBuilder _innerBuilder;
|
||||
private readonly WebApplicationOptions _settings;
|
||||
@@ -48,8 +48,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
this.AddTelegratorWeb();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -62,8 +60,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
this.AddTelegratorWeb(options, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -76,8 +72,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
this.AddTelegratorWeb(null, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -91,8 +85,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
this.AddTelegratorWeb(options, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -19,7 +19,6 @@ public class HostedUpdateWebhooker : IHostedService
|
||||
{
|
||||
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token";
|
||||
|
||||
private readonly IEndpointRouteBuilder _botHost;
|
||||
private readonly ITelegramBotClient _botClient;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly WebhookerOptions _options;
|
||||
@@ -32,12 +31,11 @@ public class HostedUpdateWebhooker : IHostedService
|
||||
/// <param name="updateRouter"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options)
|
||||
public HostedUpdateWebhooker(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options)
|
||||
{
|
||||
if (string.IsNullOrEmpty(options.Value.WebhookUri))
|
||||
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
|
||||
|
||||
_botHost = botHost;
|
||||
_botClient = botClient;
|
||||
_updateRouter = updateRouter;
|
||||
_options = options.Value;
|
||||
@@ -52,9 +50,6 @@ public class HostedUpdateWebhooker : IHostedService
|
||||
|
||||
private async void StartInternal(CancellationToken cancellationToken)
|
||||
{
|
||||
string pattern = new UriBuilder(_options.WebhookUri).Path;
|
||||
_botHost.MapPost(pattern, (Delegate)ReceiveUpdate);
|
||||
|
||||
await _botClient.SetWebhook(
|
||||
url: _options.WebhookUri,
|
||||
maxConnections: _options.MaxConnections,
|
||||
@@ -71,6 +66,16 @@ public class HostedUpdateWebhooker : IHostedService
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps bot webhook to application builder
|
||||
/// </summary>
|
||||
/// <param name="routeBuilder"></param>
|
||||
public void MapWebhook(IEndpointRouteBuilder routeBuilder)
|
||||
{
|
||||
string pattern = new UriBuilder(_options.WebhookUri).Path;
|
||||
routeBuilder.MapPost(pattern, (Delegate)ReceiveUpdate);
|
||||
}
|
||||
|
||||
private async Task<IResult> ReceiveUpdate(HttpContext ctx)
|
||||
{
|
||||
if (_options.SecretToken != null)
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
|
||||
<Title>Telegrator.Hosting.Web</Title>
|
||||
<Version>1.16.5</Version>
|
||||
<Version>1.16.6</Version>
|
||||
<Authors>Rikitav Tim4ik</Authors>
|
||||
<Company>Rikitav Tim4ik</Company>
|
||||
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
@@ -42,7 +43,27 @@ namespace Telegrator
|
||||
/// <summary>
|
||||
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
|
||||
/// </summary>
|
||||
public static IHostApplicationBuilder AddTelegratorWeb(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
|
||||
public static ITelegramBotHostBuilder AddTelegratorWeb(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
|
||||
{
|
||||
builder.AddTelegratorWebInternal(options, handlers);
|
||||
action?.Invoke(builder);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
|
||||
/// </summary>
|
||||
public static IHostApplicationBuilder AddTelegratorWeb(this WebApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
|
||||
{
|
||||
builder.AddTelegratorWebInternal(options, handlers);
|
||||
action?.Invoke(new TelegramBotWebHostBuilder(builder));
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
|
||||
/// </summary>
|
||||
internal static IHostApplicationBuilder AddTelegratorWebInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
|
||||
{
|
||||
IServiceCollection services = builder.Services;
|
||||
IConfigurationManager configuration = builder.Configuration;
|
||||
@@ -103,11 +124,12 @@ namespace Telegrator
|
||||
/// Replaces the initialization logic from TelegramBotWebHost constructor.
|
||||
/// Initializes the bot and logs handlers on application startup.
|
||||
/// </summary>
|
||||
public static WebApplication UseTelegratorWeb(this WebApplication app)
|
||||
public static T UseTelegratorWeb<T>(this T app) where T : IEndpointRouteBuilder, IHost
|
||||
{
|
||||
ITelegramBotInfo info = app.Services.GetRequiredService<ITelegramBotInfo>();
|
||||
IHandlersCollection handlers = app.Services.GetRequiredService<IHandlersCollection>();
|
||||
ILoggerFactory loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
|
||||
HostedUpdateWebhooker webhooker = app.ServiceProvider.GetRequiredService<HostedUpdateWebhooker>();
|
||||
ITelegramBotInfo info = app.ServiceProvider.GetRequiredService<ITelegramBotInfo>();
|
||||
IHandlersCollection handlers = app.ServiceProvider.GetRequiredService<IHandlersCollection>();
|
||||
ILoggerFactory loggerFactory = app.ServiceProvider.GetRequiredService<ILoggerFactory>();
|
||||
ILogger logger = loggerFactory.CreateLogger("Telegrator.Hosting.Web.TelegratorHost");
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
@@ -117,6 +139,7 @@ namespace Telegrator
|
||||
logger.LogHandlers(handlers);
|
||||
}
|
||||
|
||||
webhooker.MapWebhook(app);
|
||||
return app;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Telegrator.Core;
|
||||
|
||||
namespace Telegrator.Hosting;
|
||||
|
||||
public interface ITelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider
|
||||
{
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ namespace Telegrator.Hosting;
|
||||
/// <summary>
|
||||
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
|
||||
/// </summary>
|
||||
public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider
|
||||
public class TelegramBotHostBuilder : ITelegramBotHostBuilder
|
||||
{
|
||||
private readonly HostApplicationBuilder _innerBuilder;
|
||||
private readonly HostApplicationBuilderSettings _settings;
|
||||
@@ -47,8 +47,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
this.AddTelegrator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,8 +59,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
this.AddTelegrator(options, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -75,8 +71,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
this.AddTelegrator(null, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -90,8 +84,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
this.AddTelegrator(options, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -108,6 +100,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
|
||||
/// <inheritdoc/>
|
||||
public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
|
||||
{
|
||||
this.ConfigureContainer(factory, configure);
|
||||
_innerBuilder.ConfigureContainer(factory, configure);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
|
||||
<Title>Telegrator.Hosting</Title>
|
||||
<Version>1.16.5</Version>
|
||||
<Version>1.16.6</Version>
|
||||
<Authors>Rikitav Tim4ik</Authors>
|
||||
<Company>Rikitav Tim4ik</Company>
|
||||
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
|
||||
|
||||
@@ -30,7 +30,7 @@ public static class HostBuilderExtensions
|
||||
/// </summary>
|
||||
public const string HandlersCollectionPropertyKey = nameof(IHandlersCollection);
|
||||
|
||||
extension (IHostApplicationBuilder builder)
|
||||
extension (HostApplicationBuilder builder)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IHandlersCollection"/> from the builder properties.
|
||||
@@ -39,18 +39,35 @@ public static class HostBuilderExtensions
|
||||
{
|
||||
get
|
||||
{
|
||||
if (builder is TelegramBotHostBuilder botHostBuilder)
|
||||
return botHostBuilder.Handlers;
|
||||
|
||||
return (IHandlersCollection)builder.Properties[HandlersCollectionPropertyKey];
|
||||
return (IHandlersCollection)((IHostApplicationBuilder)builder).Properties[HandlersCollectionPropertyKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
|
||||
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
|
||||
/// </summary>
|
||||
public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
|
||||
public static ITelegramBotHostBuilder AddTelegrator(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
|
||||
{
|
||||
builder.AddTelegratorInternal(options, handlers);
|
||||
action?.Invoke(builder);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
|
||||
/// </summary>
|
||||
public static IHostApplicationBuilder AddTelegrator(this HostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
|
||||
{
|
||||
builder.AddTelegratorInternal(options, handlers);
|
||||
action?.Invoke(new TelegramBotHostBuilder(builder));
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
|
||||
/// </summary>
|
||||
public static IHostApplicationBuilder AddTelegratorInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
|
||||
{
|
||||
IServiceCollection services = builder.Services;
|
||||
IConfigurationManager configuration = builder.Configuration;
|
||||
@@ -133,7 +150,6 @@ public static class ServicesCollectionExtensions
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services)
|
||||
{
|
||||
services.AddLogging(builder => builder.AddConsole().AddDebug());
|
||||
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
|
||||
services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
|
||||
services.AddSingleton<IUpdateRouter, HostUpdateRouter>();
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
|
||||
<Title>Telegrator : Telegram.Bot mediator framework</Title>
|
||||
<Version>1.16.5</Version>
|
||||
<Version>1.16.6</Version>
|
||||
<Authors>Rikitav Tim4ik</Authors>
|
||||
<Company>Rikitav Tim4ik</Company>
|
||||
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
|
||||
|
||||
Reference in New Issue
Block a user