* moved WelcomeAttribute to a separate namespace for special case-targetted attribute
* Added "MethodHandlerDescriptor" a class that provides ability to implicitly create handlers from methods that matches the AbstractHandlerAction<T> delegate * Added extension method for HandlersCollection for adding methods as handlers
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
using Telegrator.Filters;
|
using Telegrator.Filters;
|
||||||
|
|
||||||
namespace Telegrator.Annotations
|
namespace Telegrator.Annotations.Targetted
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attribute for filtering message with command "start" in bot's private chats.
|
/// Attribute for filtering message with command "start" in bot's private chats.
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
using Telegrator.Providers;
|
|
||||||
using Telegrator.Handlers.Building.Components;
|
using Telegrator.Handlers.Building.Components;
|
||||||
using Telegrator.MadiatorCore;
|
using Telegrator.MadiatorCore;
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,53 @@
|
|||||||
using System;
|
using System.Reflection;
|
||||||
using System.Collections.Generic;
|
using Telegram.Bot.Types;
|
||||||
using System.Reflection;
|
using Telegram.Bot.Types.Enums;
|
||||||
using System.Text;
|
using Telegrator.Attributes.Components;
|
||||||
|
using Telegrator.Filters.Components;
|
||||||
using Telegrator.Handlers;
|
using Telegrator.Handlers;
|
||||||
|
using Telegrator.Handlers.Building;
|
||||||
using Telegrator.Handlers.Components;
|
using Telegrator.Handlers.Components;
|
||||||
|
|
||||||
namespace Telegrator.MadiatorCore.Descriptors
|
namespace Telegrator.MadiatorCore.Descriptors
|
||||||
{
|
{
|
||||||
/*
|
public class MethodHandlerDescriptor<TUpdate> : HandlerDescriptor where TUpdate : class
|
||||||
public class MethodHandlerDescriptor : HandlerDescriptor
|
|
||||||
{
|
{
|
||||||
public MethodHandlerDescriptor() : base(DescriptorType.General, )
|
public MethodHandlerDescriptor(AbstractHandlerAction<TUpdate> action) : base(DescriptorType.General)
|
||||||
|
{
|
||||||
|
UpdateHandlerAttributeBase handlerAttribute = HandlerInspector.GetHandlerAttribute(action.Method);
|
||||||
|
StateKeeperAttributeBase? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(action.Method);
|
||||||
|
IFilter<Update>[] filters = HandlerInspector.GetFilterAttributes(action.Method, handlerAttribute.Type).ToArray();
|
||||||
|
|
||||||
|
UpdateType = handlerAttribute.Type;
|
||||||
|
Indexer = handlerAttribute.GetIndexer();
|
||||||
|
Filters = new DescriptorFiltersSet(handlerAttribute, stateKeeperAttribute, filters);
|
||||||
|
DisplayString = HandlerInspector.GetDisplayName(action.Method);
|
||||||
|
InstanceFactory = () => new MethodHandler(action.Method, UpdateType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MethodHandler<TUpdate> : AbstractUpdateHandler<TUpdate>
|
private class MethodHandler(MethodInfo method, UpdateType updateType) : AbstractUpdateHandler<TUpdate>(updateType)
|
||||||
{
|
|
||||||
public override Task Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
|
|
||||||
{
|
{
|
||||||
|
private readonly MethodInfo Method = method;
|
||||||
|
|
||||||
|
public override async Task Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
|
||||||
|
{
|
||||||
|
if (Method is null)
|
||||||
|
throw new Exception();
|
||||||
|
|
||||||
|
if (Method.ReturnType == typeof(void))
|
||||||
|
{
|
||||||
|
Method.Invoke(this, [container, cancellation]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
object branchReturn = Method.Invoke(this, [container, cancellation]);
|
||||||
|
if (branchReturn == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (branchReturn is Task branchTask)
|
||||||
|
await branchTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
public UpdateType UpdateType
|
public UpdateType UpdateType
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
private set;
|
protected set;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -79,7 +79,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
public DescriptorFiltersSet Filters
|
public DescriptorFiltersSet Filters
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
private set;
|
protected set;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -88,7 +88,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
public object? ServiceKey
|
public object? ServiceKey
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
private set;
|
protected set;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -97,7 +97,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
public Func<UpdateHandlerBase>? InstanceFactory
|
public Func<UpdateHandlerBase>? InstanceFactory
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
private set;
|
protected set;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -106,7 +106,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
public UpdateHandlerBase? SingletonInstance
|
public UpdateHandlerBase? SingletonInstance
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
set;
|
protected set;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -127,6 +127,13 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
set;
|
set;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal HandlerDescriptor(DescriptorType descriptorType)
|
||||||
|
{
|
||||||
|
Type = descriptorType;
|
||||||
|
HandlerType = null!;
|
||||||
|
Filters = new DescriptorFiltersSet(null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="HandlerDescriptor"/> class with the specified descriptor type and handler type.
|
/// Initializes a new instance of the <see cref="HandlerDescriptor"/> class with the specified descriptor type and handler type.
|
||||||
/// Automatically inspects the handler type to extract attributes, filters, and configuration.
|
/// Automatically inspects the handler type to extract attributes, filters, and configuration.
|
||||||
@@ -413,6 +420,14 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory));
|
InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetInstance(UpdateHandlerBase instance)
|
||||||
|
{
|
||||||
|
if (SingletonInstance != null)
|
||||||
|
throw new Exception();
|
||||||
|
|
||||||
|
SingletonInstance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
=> DisplayString ?? HandlerType.Name;
|
=> DisplayString ?? HandlerType.Name;
|
||||||
|
|||||||
@@ -356,12 +356,19 @@ namespace Telegrator
|
|||||||
public static HandlerBuilder<CallbackQuery> CreateCallbackQuery(this IHandlersCollection handlers)
|
public static HandlerBuilder<CallbackQuery> CreateCallbackQuery(this IHandlersCollection handlers)
|
||||||
=> handlers.CreateHandler<CallbackQuery>(UpdateType.CallbackQuery);
|
=> handlers.CreateHandler<CallbackQuery>(UpdateType.CallbackQuery);
|
||||||
|
|
||||||
/*
|
/// <summary>
|
||||||
public static IHandlersCollection AddMethod<TUpdate>(this IHandlersCollection handlers, Func<IAbstractHandlerContainer<TUpdate>, CancellationToken, Task> method)
|
/// Creates implicit handler from method
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TUpdate"></typeparam>
|
||||||
|
/// <param name="handlers"></param>
|
||||||
|
/// <param name="method"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IHandlersCollection AddMethod<TUpdate>(this IHandlersCollection handlers, AbstractHandlerAction<TUpdate> method) where TUpdate : class
|
||||||
{
|
{
|
||||||
|
MethodHandlerDescriptor<TUpdate> descriptor = new MethodHandlerDescriptor<TUpdate>(method);
|
||||||
|
handlers.AddDescriptor(descriptor);
|
||||||
|
return handlers;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user