Skip to content

Commit 7218207

Browse files
committed
Refactor BotBuilder and add authorization features
Removed IBotBuilder interface and updated BotBuilder class. Extended BotControllerBase with Void() method. Added UseAuthorizationHandler<THandler> to ServiceCollectionExtensions. Introduced IBotAuthorizationHandler interface. Added EmptyResult class implementing IActionResult. Added AuthorizeAttribute for class/method authorization.
1 parent 3e95666 commit 7218207

7 files changed

Lines changed: 92 additions & 51 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Telegram.Bot.Types;
2+
using TelegramBot.ActionResults;
3+
4+
namespace TelegramBot.Abstractions
5+
{
6+
/// <summary>
7+
/// Defines the bot authorization handler.
8+
/// </summary>
9+
public interface IBotAuthorizationHandler
10+
{
11+
/// <summary>
12+
/// Authorize the user.
13+
/// </summary>
14+
/// <param name="authorizingUser">The user who is trying to perform the action.</param>
15+
/// <returns>True if the user is authorized, otherwise false.</returns>
16+
bool Authorize(User authorizingUser);
17+
18+
/// <summary>
19+
/// Handle the unauthorized user.
20+
/// </summary>
21+
/// <param name="authorizingUser">The user who is trying to perform the action.</param>
22+
/// <returns>Result of the <see cref="IActionResult"/> action, ex. <see cref="TextResult"/> or <see cref="EmptyResult"/>.</returns>
23+
IActionResult HandleUnauthorized(User authorizingUser);
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading.Tasks;
2+
using TelegramBot.Abstractions;
3+
4+
namespace TelegramBot.ActionResults
5+
{
6+
/// <summary>
7+
/// Defines an empty result.
8+
/// </summary>
9+
public class EmptyResult : IActionResult
10+
{
11+
/// <summary>
12+
/// Does nothing.
13+
/// </summary>
14+
/// <param name="context">The context.</param>
15+
/// <returns>Task representing the result of the action.</returns>
16+
public Task ExecuteResultAsync(ActionContext context)
17+
{
18+
return Task.CompletedTask;
19+
}
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TelegramBot.Attributes
6+
{
7+
/// <summary>
8+
///
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
11+
public class AuthorizeAttribute : Attribute
12+
{
13+
14+
}
15+
}

Sources/TelegramBot/Builders/BotBuilder.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
using System;
22
using System.Linq;
33
using Telegram.Bot;
4+
using System.Threading;
5+
using Telegram.Bot.Types;
46
using TelegramBot.Services;
57
using TelegramBot.Providers;
8+
using TelegramBot.Attributes;
69
using TelegramBot.Abstractions;
710
using Microsoft.Extensions.Logging;
811
using Microsoft.Extensions.Hosting;
912
using Microsoft.Extensions.Configuration;
1013
using Microsoft.Extensions.DependencyInjection;
11-
using System.Threading;
14+
using TelegramBot.Controllers;
1215

1316
namespace TelegramBot.Builders
1417
{
1518
/// <summary>
1619
/// A builder for bot application and services.
1720
/// </summary>
18-
public class BotBuilder : IBotBuilder
21+
public class BotBuilder
1922
{
2023
/// <summary>
2124
/// A collection of services for the application to compose. This is useful for adding user provided or framework provided services.
@@ -60,7 +63,7 @@ public BotBuilder(params string[] args)
6063
/// Use the Telegram server with the specified base URL.
6164
/// </summary>
6265
/// <param name="configure">The configuration for the Telegram server.</param>
63-
/// <returns>This instance of <see cref="IBotBuilder"/>.</returns>
66+
/// <returns>This instance of <see cref="BotBuilder"/>.</returns>
6467
/// <exception cref="ArgumentException">Thrown when the base URL is not a valid URI.</exception>
6568
public BotBuilder UseTelegramServer(Action<TelegramServerBuilder> configure)
6669
{
@@ -87,7 +90,7 @@ public BotBuilder UseTelegramServer(Action<TelegramServerBuilder> configure)
8790
/// Use the API key for the Telegram bot.
8891
/// </summary>
8992
/// <param name="value">The configuration for the Telegram API key.</param>
90-
/// <returns>This instance of <see cref="IBotBuilder"/>.</returns>
93+
/// <returns>This instance of <see cref="BotBuilder"/>.</returns>
9194
/// <exception cref="ArgumentNullException">Thrown when the Telegram bot token is not set in the configuration.</exception>
9295
public BotBuilder UseApiKey(Action<TelegramApiKeyBuilder> value)
9396
{
@@ -109,7 +112,7 @@ public BotBuilder UseApiKey(Action<TelegramApiKeyBuilder> value)
109112
/// Use custom services for the bot additionally to the built-in services.
110113
/// </summary>
111114
/// <param name="services">The services to use.</param>
112-
/// <returns>This instance of <see cref="IBotBuilder"/>.</returns>
115+
/// <returns>This instance of <see cref="BotBuilder"/>.</returns>
113116
public BotBuilder UseServices(IServiceCollection services)
114117
{
115118
foreach (var service in services)

Sources/TelegramBot/Builders/IBotBuilder.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

Sources/TelegramBot/Controllers/BotControllerBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public IActionResult Inline(string prompt, InlineKeyboardMarkup keyboard, bool u
8080
return new InlineResult(prompt, keyboard, useMarkdown);
8181
}
8282

83+
/// <summary>
84+
/// Do nothing.
85+
/// </summary>
86+
/// <returns>Result of the <see cref="IActionResult"/> action.</returns>
87+
public IActionResult Void()
88+
{
89+
return new EmptyResult();
90+
}
91+
8392
/// <summary>
8493
/// Sets the value of the key in the key-value provider.
8594
/// </summary>

Sources/TelegramBot/Extensions/ServiceCollectionExtensions.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using TelegramBot.Attributes;
23
using TelegramBot.Abstractions;
34
using Microsoft.Extensions.DependencyInjection;
45

@@ -9,6 +10,17 @@ namespace TelegramBot.Extensions
910
/// </summary>
1011
public static class ServiceCollectionExtensions
1112
{
13+
/// <summary>
14+
/// Use the authorization predicate for methods and controllers with <see cref="AuthorizeAttribute"/>
15+
/// </summary>
16+
/// <typeparam name="THandler">The type of the authorization handler.</typeparam>
17+
/// <param name="services">The service collection.</param>
18+
/// <returns>The modified service collection.</returns>
19+
public static IServiceCollection UseAuthorizationHandler<THandler>(this IServiceCollection services) where THandler : class, IBotAuthorizationHandler
20+
{
21+
return services.AddSingleton<IBotAuthorizationHandler, THandler>();
22+
}
23+
1224
/// <summary>
1325
/// Registers the specified <see cref="IKeyValueProvider"/> instance as a singleton service in the service collection.
1426
/// </summary>
@@ -17,8 +29,7 @@ public static class ServiceCollectionExtensions
1729
/// <returns>The modified service collection.</returns>
1830
public static IServiceCollection UseKeyValueProvider(this IServiceCollection services, IKeyValueProvider provider)
1931
{
20-
services.AddSingleton(provider);
21-
return services;
32+
return services.AddSingleton(provider);
2233
}
2334

2435
/// <summary>
@@ -41,8 +52,7 @@ public static IServiceCollection UseKeyValueProvider(this IServiceCollection ser
4152
/// <returns>The modified service collection.</returns>
4253
public static IServiceCollection UseKeyValueProvider<TImplementation>(this IServiceCollection services) where TImplementation : class, IKeyValueProvider
4354
{
44-
services.AddSingleton<IKeyValueProvider, TImplementation>();
45-
return services;
55+
return services.AddSingleton<IKeyValueProvider, TImplementation>();
4656
}
4757
}
4858
}

0 commit comments

Comments
 (0)