Skip to content

Commit

Permalink
Update configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Neisvestney committed Feb 8, 2023
1 parent dfb36ca commit e66422d
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/guides/getting_started/simple_bot.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var host = Host.CreateDefaultBuilder(args)
{
c.TelegramBotClientOptions = new TelegramBotClientOptions(context.Configuration["Telegram:Token"]);
})
.AddTelegramModulesService((context, c) => {})
.AddTelegramModulesService()
.ConfigureServices(services =>
{
services.AddHostedService<TelegramHandler>();
Expand Down
4 changes: 3 additions & 1 deletion docs/guides/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ host.ConfigureTelegramBotHost((context, c) =>
Used to add [Modules](xref:Guides.TelegramModule) and process updates

```csharp
host.AddTelegramModulesService((context, c) => {})
host.AddTelegramModulesService((context, c) => {
// Configuration
})
```

To add modules, set bot commands, handle post execution you need to create Hosted Service
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/web_hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ builder.Host.ConfigureTelegramBotWebHookHost((context, c) =>
{
c.TelegramBotClientOptions = new TelegramBotClientOptions(context.Configuration["Telegram:Token"] ?? throw new InvalidOperationException("Token must be non null"));
c.SecretToken = context.Configuration["Telegram:WebHook:SecretToken"] ?? throw new InvalidOperationException("Secret must be non null");
}).AddTelegramModulesService((context, c) => {});
}).AddTelegramModulesService();

builder.Services.Configure<TelegramBotWebHookHostConfiguration>(builder.Configuration.GetSection("WebHookConfiguration"));

Expand Down
2 changes: 1 addition & 1 deletion samples/TelegramModularFramework.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{
c.TelegramBotClientOptions = new TelegramBotClientOptions(context.Configuration["Telegram:Token"]);
})
.AddTelegramModulesService((context, c) => {})
.AddTelegramModulesService()
.ConfigureServices(services =>
{
services.AddLocalization(options =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TelegramModularFramework.Services;
using Telegram.Bot.Types;
using TelegramModularFramework.Services;

namespace TelegramModularFramework.WebHook.Services;

Expand All @@ -7,4 +8,10 @@ public class TelegramBotWebHookHostConfiguration : TelegramBotHostConfiguration
public string HostAddress { get; set; }
public string Route { get; set; } = "/telegram";
public string SecretToken { get; set; }

public InputFile? Certificate { get; set; } = default;

public string? IpAddress { get; set; } = default;

public int? MaxConnections { get; set; } = default;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@ public TelegramBotWebHookHostedService(ITelegramBotClient botClient,

public async Task StartAsync(CancellationToken cancellationToken)
{
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types
};

var user = await _botClient.GetMeAsync(cancellationToken);
_telegramBotUser.User = user;
_logger.LogInformation("Connected as {username} with id {id}", user.Username, user.Id);

await _botClient.SetWebhookAsync(
url: $"{_options.HostAddress}{_options.Route}",
allowedUpdates: Array.Empty<UpdateType>(),
certificate: _options.Certificate,
ipAddress: _options.IpAddress,
allowedUpdates: _options.AllowedUpdates,
secretToken: _options.SecretToken,
dropPendingUpdates: _options.DropPendingUpdates,
cancellationToken: cancellationToken
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public static IServiceCollection AddTelegramBotHostBasics(this IServiceCollectio
/// <param name="builder">The host builder to configure.</param>
/// <param name="config">The delegate to configure <see cref="T:TelegramModularFramework.Services.TelegramModulesConfiguration"/></param>
/// <returns>Host builder</returns>
public static IHostBuilder AddTelegramModulesService(this IHostBuilder builder, Action<HostBuilderContext, TelegramModulesConfiguration> config)
public static IHostBuilder AddTelegramModulesService(this IHostBuilder builder, Action<HostBuilderContext, TelegramModulesConfiguration>? config = null)
{
return builder.ConfigureServices((context, services) =>
{
services.Configure<TelegramModulesConfiguration>(c => config(context, c));
if (config != null) services.Configure<TelegramModulesConfiguration>(c => config(context, c));
services.AddTransient(c => c.GetService<IOptions<TelegramModulesConfiguration>>().Value.StateHolder);
services.AddSingleton<TelegramModulesService>();
services.AddTransient<IStringSplitter, StringSplitter>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Telegram.Bot;
using Telegram.Bot.Types.Enums;

namespace TelegramModularFramework.Services;

Expand All @@ -10,4 +11,8 @@ public class TelegramBotHostConfiguration
/// HttpClient for <see cref="T:Telegram.Bot.ITelegramBotClient"/>
/// </summary>
public HttpClient HttpClient { get; set; }

public IEnumerable<UpdateType> AllowedUpdates { get; set; } = Array.Empty<UpdateType>();

public bool? DropPendingUpdates { get; set; } = default;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegram.Bot;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
Expand All @@ -17,20 +18,23 @@ public class TelegramBotHostedService: BackgroundService
private readonly ILogger<TelegramBotHostedService> _logger;
private readonly TelegramBotEvents _events;
private readonly TelegramBotUser _telegramBotUser;
private readonly TelegramBotHostConfiguration _options;

public TelegramBotHostedService(ITelegramBotClient botClient, ILogger<TelegramBotHostedService> logger, TelegramBotEvents events, TelegramBotUser telegramBotUser)
public TelegramBotHostedService(ITelegramBotClient botClient, ILogger<TelegramBotHostedService> logger, TelegramBotEvents events, TelegramBotUser telegramBotUser, IOptions<TelegramBotHostConfiguration> options)
{
_botClient = botClient;
_logger = logger;
_events = events;
_telegramBotUser = telegramBotUser;
_options = options.Value;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types
AllowedUpdates = _options.AllowedUpdates.ToArray(),
ThrowPendingUpdates = _options.DropPendingUpdates ?? default
};

var user = await _botClient.GetMeAsync(stoppingToken);
Expand Down

0 comments on commit e66422d

Please sign in to comment.