| | | 1 | | using Microsoft.AspNetCore.Builder; |
| | | 2 | | using Microsoft.AspNetCore.Http; |
| | | 3 | | using Microsoft.AspNetCore.Routing; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using TeleFlow.Telegram.Webhooks.Internal; |
| | | 7 | | |
| | | 8 | | namespace TeleFlow.Telegram.Webhooks; |
| | | 9 | | |
| | | 10 | | public static class TelegramRawWebhookEndpointRouteBuilderExtensions |
| | | 11 | | { |
| | | 12 | | public static RouteHandlerBuilder MapTelegramWebhook( |
| | | 13 | | this IEndpointRouteBuilder endpoints, |
| | | 14 | | string pattern, |
| | | 15 | | TelegramRawWebhookHandler handler, |
| | | 16 | | Action<TelegramRawWebhookOptions>? configure = null) |
| | | 17 | | { |
| | 24 | 18 | | ArgumentNullException.ThrowIfNull(endpoints); |
| | 24 | 19 | | ArgumentException.ThrowIfNullOrWhiteSpace(pattern); |
| | 24 | 20 | | ArgumentNullException.ThrowIfNull(handler); |
| | | 21 | | |
| | 24 | 22 | | var options = new TelegramRawWebhookOptions(); |
| | 24 | 23 | | configure?.Invoke(options); |
| | 24 | 24 | | TelegramRawWebhookOptionsValidator.Validate(options); |
| | | 25 | | |
| | 22 | 26 | | var normalizedPattern = pattern.Trim(); |
| | 22 | 27 | | if (!normalizedPattern.StartsWith('/')) |
| | | 28 | | { |
| | 0 | 29 | | throw new InvalidOperationException("Telegram webhook path must start with '/'."); |
| | | 30 | | } |
| | | 31 | | |
| | 22 | 32 | | var logger = endpoints.ServiceProvider |
| | 22 | 33 | | .GetService<ILoggerFactory>() |
| | 22 | 34 | | ?.CreateLogger<TelegramRawWebhookEndpoint>(); |
| | 22 | 35 | | var endpoint = new TelegramRawWebhookEndpoint(handler, options, logger); |
| | 22 | 36 | | return endpoints.MapPost( |
| | 22 | 37 | | normalizedPattern, |
| | 22 | 38 | | (Func<HttpContext, Task<IResult>>)endpoint.HandleAsync); |
| | | 39 | | } |
| | | 40 | | } |