Добавьте файлы проекта.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using Telegrator.Hosting.Components;
|
||||
|
||||
namespace Telegrator.Hosting.Web.Components
|
||||
{
|
||||
public interface ITelegramBotWebHost : ITelegramBotHost//, IEndpointRouteBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Telegram.Bot;
|
||||
using Telegrator.Hosting.Web.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
|
||||
namespace Telegrator.Hosting.Web.Polling
|
||||
{
|
||||
public class HostedUpdateWebhooker : IHostedService
|
||||
{
|
||||
private readonly ITelegramBotWebHost _botHost;
|
||||
private readonly ITelegramBotClient _botClient;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly TelegramBotWebOptions _options;
|
||||
|
||||
public HostedUpdateWebhooker(ITelegramBotWebHost botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<TelegramBotWebOptions> options)
|
||||
{
|
||||
if (string.IsNullOrEmpty(options.Value.WebhookUri))
|
||||
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
|
||||
|
||||
if (string.IsNullOrEmpty(options.Value.WebhookPattern))
|
||||
throw new ArgumentNullException(nameof(options), "Option \"WebhookPattern\" must be set to subscribe for update recieving");
|
||||
|
||||
_botHost = botHost;
|
||||
_botClient = botClient;
|
||||
_updateRouter = updateRouter;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_botClient.SetWebhook(
|
||||
url: _options.WebhookUri,
|
||||
maxConnections: _options.MaxConnections,
|
||||
allowedUpdates: _botHost.UpdateRouter.HandlersProvider.AllowedTypes,
|
||||
dropPendingUpdates: _options.DropPendingUpdates,
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
//botHost.MapGet(_options.WebhookPattern, async (Update update) => await _updateRouter.HandleUpdateAsync(_botClient, update, cancellationToken));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_botClient.DeleteWebhook(_options.DropPendingUpdates, cancellationToken);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Telegrator.Hosting.Components;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
{
|
||||
public class TelegramBotWebHost //: ITelegramBotWebHost
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Telegrator.Hosting.Components;
|
||||
using Telegrator.Hosting.Web.Components;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
{
|
||||
public class TelegramBotWebHostBuilder //: ITelegramBotHostBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Telegrator.Configuration;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
{
|
||||
public class TelegramBotWebOptions : TelegramBotOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets uri for webhook update receiving
|
||||
/// </summary>
|
||||
public required string WebhookUri { get; set; }
|
||||
|
||||
public required string WebhookPattern { get; set; }
|
||||
|
||||
public int MaxConnections { get; set; }
|
||||
|
||||
public bool DropPendingUpdates { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Telegrator.Hosting\Telegrator.Hosting.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.3.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Telegram.Bot;
|
||||
using Telegrator.Hosting.Web.Polling;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
{
|
||||
public static class ServicesCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddTelegramWebhook(this IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient<ITelegramBotClient>("tgwebhook").RemoveAllLoggers().AddTypedClient(TypedTelegramBotClientFactory);
|
||||
services.AddHostedService<HostedUpdateWebhooker>();
|
||||
return services;
|
||||
}
|
||||
|
||||
private static ITelegramBotClient TypedTelegramBotClientFactory(HttpClient httpClient, IServiceProvider provider)
|
||||
=> new TelegramBotClient(provider.GetRequiredService<IOptions<TelegramBotClientOptions>>().Value, httpClient);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user