diff --git a/docs/Telegrator.Hosting.Web.xml b/docs/Telegrator.Hosting.Web.xml
index 36a48b9..a4e606a 100644
--- a/docs/Telegrator.Hosting.Web.xml
+++ b/docs/Telegrator.Hosting.Web.xml
@@ -84,9 +84,7 @@
-
- Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
-
+
@@ -109,37 +107,18 @@
-
+
Initializes a new instance of the class.
-
-
-
- Initializes a new instance of the class.
-
-
-
-
-
-
+
Initializes a new instance of the class.
-
-
-
-
- Initializes a new instance of the class.
-
-
-
-
-
@@ -188,7 +167,6 @@
Initiallizes new instance of
-
@@ -200,6 +178,16 @@
+
+
+ Allows to remap receiving webhook endpoint and map new route to webhost.
+
+
+
+
+
+
+
Maps bot webhook to application builder
@@ -230,17 +218,34 @@
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
-
+
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
-
+
+
+ Searchs for hosted service inside hosts services
+
+
+
+
+
+
Replaces the initialization logic from TelegramBotWebHost constructor.
Initializes the bot and logs handlers on application startup.
+
+
+ Allows to remap receiving webhook endpoint and map new route to webhost.
+
+
+
+
+
+
Registers service with to receive updates using webhook
diff --git a/docs/Telegrator.Hosting.xml b/docs/Telegrator.Hosting.xml
index d9a9352..e66dabb 100644
--- a/docs/Telegrator.Hosting.xml
+++ b/docs/Telegrator.Hosting.xml
@@ -35,6 +35,11 @@
Provides access to configuration of this Hosted telegram bot
+
+
+ Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
+
+
Represents a hosted telegram bot
@@ -93,9 +98,7 @@
-
- Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
-
+
@@ -118,37 +121,18 @@
-
+
Initializes a new instance of the class.
-
-
-
- Initializes a new instance of the class.
-
-
-
-
-
-
+
Initializes a new instance of the class.
-
-
-
-
- Initializes a new instance of the class.
-
-
-
-
-
@@ -268,7 +252,17 @@
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
-
+
+
+ Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
+
+
+
+
+ Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
+
+
+
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
diff --git a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj
index 67db6f8..bff2542 100644
--- a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj
+++ b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj
@@ -31,7 +31,7 @@
-
+
diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs
index 13eab68..38b96c4 100644
--- a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs
+++ b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs
@@ -65,11 +65,11 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// Creates new with default services and webhook update receiving scheme
///
///
- public static TelegramBotWebHostBuilder CreateBuilder(WebApplicationOptions settings)
+ public static TelegramBotWebHostBuilder CreateBuilder(WebApplicationOptions? settings)
{
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings);
- TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
+ TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp);
builder.AddTelegratorWeb();
return builder;
}
@@ -78,11 +78,11 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// Creates new SLIM with default services and webhook update receiving scheme
///
///
- public static TelegramBotWebHostBuilder CreateSlimBuilder(WebApplicationOptions settings)
+ public static TelegramBotWebHostBuilder CreateSlimBuilder(WebApplicationOptions? settings)
{
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings);
- TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
+ TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp);
builder.AddTelegratorWeb();
return builder;
}
@@ -91,11 +91,11 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// Creates new EMPTY WITHOUT any services or update receiving schemes
///
///
- public static TelegramBotWebHostBuilder CreateEmptyBuilder(WebApplicationOptions settings)
+ public static TelegramBotWebHostBuilder CreateEmptyBuilder(WebApplicationOptions? settings)
{
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings);
- TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
+ TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp);
builder.AddTelegratorWeb();
return builder;
}
diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs
index 97f5b9b..fb813ba 100644
--- a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs
+++ b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs
@@ -9,13 +9,10 @@ using Telegrator.Core;
#pragma warning disable IDE0001
namespace Telegrator.Hosting.Web;
-///
-/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
-///
+///
public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
{
private readonly WebApplicationBuilder _innerBuilder;
- private readonly WebApplicationOptions _settings;
internal IHandlersCollection _handlers = null!;
///
@@ -43,23 +40,9 @@ public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
/// Initializes a new instance of the class.
///
///
- ///
- public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions? settings = null)
+ public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder)
{
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
- _settings = settings ?? throw new ArgumentNullException(nameof(settings));
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- ///
- ///
- ///
- public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegratorOptions? options, WebApplicationOptions? settings)
- {
- _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
- _settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
///
@@ -67,24 +50,10 @@ public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
///
///
///
- ///
- public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, WebApplicationOptions settings)
+ public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers)
{
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
- _settings = settings ?? throw new ArgumentNullException(nameof(settings));
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- ///
- ///
- ///
- ///
- public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, TelegratorOptions? options, WebApplicationOptions settings)
- {
- _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
- _settings = settings ?? throw new ArgumentNullException(nameof(settings));
+ _handlers = handlers ?? throw new ArgumentNullException(nameof(handlers));
}
///
diff --git a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs
index dca6cbf..938b445 100644
--- a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs
+++ b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs
@@ -26,7 +26,6 @@ public class HostedUpdateWebhooker : IHostedService
///
/// Initiallizes new instance of
///
- ///
///
///
///
@@ -48,17 +47,6 @@ public class HostedUpdateWebhooker : IHostedService
return Task.CompletedTask;
}
- private async void StartInternal(CancellationToken cancellationToken)
- {
- await _botClient.SetWebhook(
- url: _options.WebhookUri,
- maxConnections: _options.MaxConnections,
- allowedUpdates: _updateRouter.HandlersProvider.AllowedTypes,
- dropPendingUpdates: _options.DropPendingUpdates,
- secretToken: _options.SecretToken,
- cancellationToken: cancellationToken);
- }
-
///
public Task StopAsync(CancellationToken cancellationToken)
{
@@ -66,16 +54,50 @@ public class HostedUpdateWebhooker : IHostedService
return Task.CompletedTask;
}
+ ///
+ /// Allows to remap receiving webhook endpoint and map new route to webhost.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public async Task RemapWebhook(IEndpointRouteBuilder routeBuilder, string webhookUri, CancellationToken cancellationToken = default)
+ {
+ if (!Uri.TryCreate(webhookUri, UriKind.Absolute, out Uri? result))
+ throw new ArgumentException("invalid URL");
+
+ _options.WebhookUri = result.ToString();
+ await SetWebhook(cancellationToken);
+ MapWebhook(routeBuilder);
+ }
+
///
/// Maps bot webhook to application builder
///
///
- public void MapWebhook(IEndpointRouteBuilder routeBuilder)
+ internal void MapWebhook(IEndpointRouteBuilder routeBuilder)
{
string pattern = new UriBuilder(_options.WebhookUri).Path;
routeBuilder.MapPost(pattern, (Delegate)ReceiveUpdate);
}
+ private async void StartInternal(CancellationToken cancellationToken)
+ {
+ await SetWebhook(cancellationToken);
+ }
+
+ private async Task SetWebhook(CancellationToken cancellationToken)
+ {
+ await _botClient.SetWebhook(
+ url: _options.WebhookUri,
+ maxConnections: _options.MaxConnections,
+ allowedUpdates: _updateRouter.HandlersProvider.AllowedTypes,
+ dropPendingUpdates: _options.DropPendingUpdates,
+ secretToken: _options.SecretToken,
+ cancellationToken: cancellationToken);
+ }
+
private async Task ReceiveUpdate(HttpContext ctx)
{
if (_options.SecretToken != null)
diff --git a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj
index e7f1904..3597694 100644
--- a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj
+++ b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj
@@ -15,7 +15,7 @@
True
Telegrator.Hosting.Web
- 1.16.7
+ 1.16.8
Rikitav Tim4ik
Rikitav Tim4ik
https://github.com/Rikitav/Telegrator
diff --git a/src/Telegrator.Hosting.Web/TypesExtensions.cs b/src/Telegrator.Hosting.Web/TypesExtensions.cs
index 710af70..d4ff7e1 100644
--- a/src/Telegrator.Hosting.Web/TypesExtensions.cs
+++ b/src/Telegrator.Hosting.Web/TypesExtensions.cs
@@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
+using System.Diagnostics.CodeAnalysis;
using Telegram.Bot;
using Telegrator.Core;
using Telegrator.Hosting;
@@ -45,7 +46,10 @@ namespace Telegrator
///
public static ITelegramBotHostBuilder AddTelegratorWeb(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null)
{
- builder.AddTelegratorWebInternal(options, handlers);
+ AddTelegratorWebInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options);
+ if (builder is TelegramBotWebHostBuilder telegramBotHostBuilder)
+ telegramBotHostBuilder._handlers = handlers;
+
action?.Invoke(builder);
return builder;
}
@@ -55,7 +59,7 @@ namespace Telegrator
///
public static IHostApplicationBuilder AddTelegratorWeb(this WebApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null)
{
- builder.AddTelegratorWebInternal(options, handlers);
+ AddTelegratorWebInternal(builder.Services, builder.Configuration, ((IHostApplicationBuilder)builder).Properties, ref handlers, options);
action?.Invoke(new TelegramBotWebHostBuilder(builder));
return builder;
}
@@ -63,11 +67,8 @@ namespace Telegrator
///
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
///
- internal static IHostApplicationBuilder AddTelegratorWebInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
+ internal static void AddTelegratorWebInternal(IServiceCollection services, IConfiguration configuration, IDictionary