* Improved "MessageChatTypeFilter", now accepts flagged version of "ChatType" enum. "ChatTypeFlags"

* Added classes to reactive update filter annotations implementation "FilterAnnotation"
* "CollectHandlersDowmainWide" method moved as extension to IHandlersCollection and added method "CollectHandlerAssemblyWide" named respectfully
* Added "DefaultRouterExceptionHandler" to reactive implement "IRouterExceptionHandler" from function delegate
* Small bug fixes in API overview generator logic
This commit is contained in:
2025-07-26 21:03:42 +04:00
parent cec7c88b6a
commit b86699a65e
17 changed files with 307 additions and 93 deletions
+30 -15
View File
@@ -1,10 +1,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace Telegrator.Generators
@@ -71,7 +68,7 @@ namespace Telegrator.Generators
if (!string.IsNullOrWhiteSpace(typeSummary))
sourceBuilder.AppendFormat("> {0}\n\n", typeSummary);
// Writing members
// Writing fields
if (type.TypeKind == TypeKind.Enum)
{
WriteEnumValues(sourceBuilder, type);
@@ -105,7 +102,7 @@ namespace Telegrator.Generators
sourceBuilder.AppendLine("**Constructors:**");
foreach (IMethodSymbol ctor in ctors)
{
// Формируем строку вида ClassName<Type1, T>(Param1, Param2) с generic-аргументами
// Formatting constructor signature
string genericArgs = type.FormatGenericTypes();
string parameters = string.Join(", ", ctor.Parameters.Select(p => p.Type.GetShortName()));
string signature = string.Format("{0}{1}({2})", type.Name, genericArgs, parameters);
@@ -114,7 +111,9 @@ namespace Telegrator.Generators
// Writing summary
string? propSummary = ctor.ExtractSummary();
if (!string.IsNullOrWhiteSpace(propSummary))
sourceBuilder.Append(" > ").Append(propSummary).AppendLine();
sourceBuilder.Append(" > ").Append(propSummary);
sourceBuilder.AppendLine();
}
sourceBuilder.AppendLine();
@@ -122,19 +121,32 @@ namespace Telegrator.Generators
private static void WriteEnumValues(StringBuilder sourceBuilder, INamedTypeSymbol type)
{
var members = type.GetMembers().OfType<IFieldSymbol>().Where(f => f.HasConstantValue && f.DeclaredAccessibility == Accessibility.Public).ToList();
if (members.Count == 0)
// Getting enum values
List<IFieldSymbol> fields = type
.GetMembers()
.OfType<IFieldSymbol>()
.Where(f => f.HasConstantValue && f.DeclaredAccessibility == Accessibility.Public)
.ToList();
// Checking for any
if (fields.Count == 0)
return;
// Writing
sourceBuilder.AppendLine("**Values:**");
foreach (IFieldSymbol field in members)
foreach (IFieldSymbol field in fields)
{
// Writing value
sourceBuilder.Append("- `").Append(field.Name).Append("`");
// Writing summary
string? summary = field.ExtractSummary();
if (!string.IsNullOrWhiteSpace(summary))
sourceBuilder.Append(" — ").Append(summary);
sourceBuilder.AppendLine();
}
sourceBuilder.AppendLine();
}
@@ -187,9 +199,9 @@ namespace Telegrator.Generators
sourceBuilder.AppendLine("**Methods:**");
foreach (IMethodSymbol method in methods)
{
// Формируем generic-параметры для метода
// Formating method signature
string genericArgs = method.FormatGenericTypes();
string parameters = string.Join(", ", method.Parameters.Select(p => p.Type.GetShortName()));
string parameters = string.Join(", ", method.Parameters.Select(p => p.Type.GetShortName()).Where(p => !string.IsNullOrEmpty(p)));
sourceBuilder.AppendFormat(" - `{0}{1}({2})`\n", method.Name, genericArgs, parameters);
// Writing summary
@@ -215,17 +227,20 @@ namespace Telegrator.Generators
try
{
XDocument doc = XDocument.Parse(xmlDoc);
XElement? summary = doc.Root?.Element("summary");
XElement? xSummary = doc.Root?.Element("summary");
if (summary == null)
if (xSummary == null)
return null;
// Убираем лишние пробелы и переносы строк
return summary.Value.Trim().Replace("\n", " ").Replace(" ", " ");
string summary = xSummary.Value.Trim().Replace("\n", " ");
while (summary.Contains(" "))
summary = summary.Replace(" ", " ");
return summary;
}
catch
{
// Игнорируем ошибки парсинга XML
return null;
}
}
@@ -18,7 +18,6 @@ namespace Telegrator.Hosting.Polling
/// </summary>
protected readonly ILogger<HostUpdateRouter> Logger;
// Ehat a mess :/
/// <inheritdoc/>
public HostUpdateRouter(
IHandlersProvider handlersProvider,
@@ -28,7 +27,7 @@ namespace Telegrator.Hosting.Polling
ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, options.Value, handlersPool)
{
Logger = logger;
ExceptionHandler = new HostExceptionHandler(logger);
ExceptionHandler = new DefaultRouterExceptionHandler(HandleException);
}
/// <inheritdoc/>
@@ -41,21 +40,21 @@ namespace Telegrator.Hosting.Polling
/// <summary>
/// Default exception handler of this router
/// </summary>
/// <param name="logger"></param>
private class HostExceptionHandler(ILogger<HostUpdateRouter> logger) : IRouterExceptionHandler
{
/// <param name="botClient"></param>
/// <param name="exception"></param>
/// <param name="source"></param>
/// <param name="cancellationToken"></param>
public void HandleException(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken)
{
if (exception is HandlerFaultedException handlerFaultedException)
{
logger.LogError("\"{handler}\" handler's execution was faulted :\n{exception}",
Logger.LogError("\"{handler}\" handler's execution was faulted :\n{exception}",
handlerFaultedException.HandlerInfo.ToString(),
handlerFaultedException.InnerException?.ToString() ?? "No inner exception");
return;
}
logger.LogError("Exception was thrown during update routing faulted :\n{exception}", exception.ToString());
}
Logger.LogError("Exception was thrown during update routing faulted :\n{exception}", exception.ToString());
}
}
}
+1 -1
View File
@@ -15,7 +15,7 @@
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
</PropertyGroup>
<ItemGroup>
+1 -1
View File
@@ -43,7 +43,7 @@ namespace Telegrator.Hosting
/// <returns></returns>
public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services)
{
services.AddLogging(builder => builder.AddConsole());
services.AddLogging(builder => builder.AddConsole().AddDebug());
services.AddSingleton<IUpdateHandlersPool, HostUpdateHandlersPool>();
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
@@ -21,10 +21,22 @@ namespace Telegrator.Annotations
/// <summary>
/// Attribute for filtering messages sent in chats of a specific type.
/// </summary>
/// <param name="type">The chat type to match</param>
public class ChatTypeAttribute(ChatType type)
: MessageFilterAttribute(new MessageChatTypeFilter(type))
{ }
public class ChatTypeAttribute : MessageFilterAttribute
{
/// <summary>
/// Initialize new instance of <see cref="ChatTypeAttribute"/> to filter messages from chat from specific chats
/// </summary>
/// <param name="type"></param>
public ChatTypeAttribute(ChatType type)
: base(new MessageChatTypeFilter(type)) { }
/// <summary>
/// Initialize new instance of <see cref="ChatTypeAttribute"/> to filter messages from chat from specific chats (with flags)
/// </summary>
/// <param name="flags"></param>
public ChatTypeAttribute(ChatTypeFlags flags)
: base(new MessageChatTypeFilter(flags)) { }
}
/// <summary>
/// Attribute for filtering messages based on the chat title.
+49
View File
@@ -0,0 +1,49 @@
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegrator.Filters;
using Telegrator.Filters.Components;
namespace Telegrator.Attributes
{
/// <summary>
/// Reactive way to implement a new <see cref="UpdateFilterAttribute{T}"/> of type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class FilterAnnotation<T> : UpdateFilterAttribute<T>, IFilter<T> where T : class
{
/// <inheritdoc/>
public bool IsCollectible => false;
/// <summary>
/// Initializes new instance of <see cref="FilterAnnotation{T}"/>
/// </summary>
public FilterAnnotation() : base()
{
UpdateFilter = Filter<T>.If(CanPass);
AnonymousFilter = AnonymousTypeFilter.Compile(UpdateFilter, GetFilterringTarget);
}
/// <inheritdoc/>
public abstract bool CanPass(FilterExecutionContext<T> context);
}
/// <inheritdoc/>
public abstract class MessageFilterAnnotation() : FilterAnnotation<Message>()
{
/// <inheritdoc/>
public override UpdateType[] AllowedTypes => [UpdateType.Message, UpdateType.EditedMessage, UpdateType.ChannelPost, UpdateType.EditedChannelPost, UpdateType.BusinessMessage, UpdateType.EditedBusinessMessage];
/// <inheritdoc/>
public override Message? GetFilterringTarget(Update update) => update.Message;
}
/// <inheritdoc/>
public abstract class CallbackQueryFilterAnnotation() : FilterAnnotation<CallbackQuery>()
{
/// <inheritdoc/>
public override UpdateType[] AllowedTypes => [UpdateType.CallbackQuery];
/// <inheritdoc/>
public override CallbackQuery? GetFilterringTarget(Update update) => update.CallbackQuery;
}
}
+11 -1
View File
@@ -20,7 +20,17 @@ namespace Telegrator.Attributes
/// <summary>
/// Gets the compiled filter logic for the update target.
/// </summary>
public Filter<T> UpdateFilter { get; private set; }
public Filter<T> UpdateFilter { get; protected set; }
/// <summary>
/// Empty constructor for internal using
/// </summary>
internal UpdateFilterAttribute()
{
AnonymousFilter = null!;
UpdateFilter = null!;
_ = 0xBAD + 0xC0DE;
}
/// <summary>
/// Initializes the attribute with one or more filters for the update target.
+60
View File
@@ -36,4 +36,64 @@
/// </summary>
Casino
}
/// <summary>
/// Flags version of <see cref="Telegram.Bot.Types.Enums.ChatType"/>
/// Type of the <see cref="Telegram.Bot.Types.Chat"/>, from which the message or inline query was sent
/// </summary>
[Flags]
public enum ChatTypeFlags
{
/// <summary>
/// Normal one-to-one chat with a user or bot
/// </summary>
Private = 0x1,
/// <summary>
/// Normal group chat
/// </summary>
Group = 0x2,
/// <summary>
/// A channel
/// </summary>
Channel = 0x4,
/// <summary>
/// A supergroup
/// </summary>
Supergroup = 0x8,
/// <summary>
/// Value possible only in <see cref="Telegram.Bot.Types.InlineQuery.ChatType"/>: private chat with the inline query sender
/// </summary>
Sender
}
/// <summary>
/// Levels of debug writing
/// </summary>
[Flags]
public enum DebugLevel
{
/// <summary>
/// Write debug messages from filters execution
/// </summary>
Filters = 0x1,
/// <summary>
/// Write debug messages from handlers providers execution
/// </summary>
Providers = 0x2,
/// <summary>
/// Write debug messages from update router's execution
/// </summary>
Router = 0x4,
/// <summary>
/// Write debug messages from handlers pool execution
/// </summary>
HandlersPool = 0x8
}
}
+6
View File
@@ -57,6 +57,12 @@ namespace Telegrator.Filters
/// <param name="context">The filter execution context.</param>
/// <returns>True if the filter passes; otherwise, false.</returns>
public abstract bool CanPass(FilterExecutionContext<T> context);
/// <summary>
/// Implicitly creates <see cref="IFilter{T}"/> from function
/// </summary>
/// <param name="filter"></param>
public static implicit operator Filter<T>(Func<FilterExecutionContext<T>, bool> filter) => Filter<T>.If(filter);
}
/// <summary>
+40 -3
View File
@@ -1,5 +1,6 @@
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using Telegrator.Filters.Components;
namespace Telegrator.Filters
@@ -54,13 +55,49 @@ namespace Telegrator.Filters
/// <summary>
/// Filters messages whose chat type matches the specified value.
/// </summary>
public class MessageChatTypeFilter(ChatType type) : MessageChatFilter
public class MessageChatTypeFilter : MessageChatFilter
{
private readonly ChatType Type = type;
private readonly ChatType? Type;
private readonly ChatTypeFlags? Flags;
/// <summary>
/// Initialize new instance of <see cref="MessageChatTypeFilter"/>
/// </summary>
/// <param name="type"></param>
public MessageChatTypeFilter(ChatType type)
=> Type = type;
/// <summary>
/// Initialize new instance of <see cref="MessageChatTypeFilter"/> with <see cref="ChatTypeFlags"/>
/// </summary>
/// <param name="type"></param>
public MessageChatTypeFilter(ChatTypeFlags type)
=> Flags = type;
/// <inheritdoc/>
protected override bool CanPassNext(FilterExecutionContext<Chat> _)
=> Chat.Type == Type;
{
if (Type.HasValue)
return Chat.Type == Type.Value;
if (Flags != null)
{
ChatTypeFlags? asFlag = ToFlag(Chat.Type);
return asFlag.HasValue && Flags.Value.HasFlag(asFlag.Value);
}
return false;
}
private static ChatTypeFlags? ToFlag(ChatType type) => type switch
{
ChatType.Channel => ChatTypeFlags.Channel,
ChatType.Group => ChatTypeFlags.Group,
ChatType.Supergroup => ChatTypeFlags.Supergroup,
ChatType.Sender => ChatTypeFlags.Sender,
ChatType.Private => ChatTypeFlags.Private,
_ => null
};
}
/// <summary>
+1 -27
View File
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Telegram.Bot.Types.Enums;
namespace Telegrator
{
@@ -91,31 +92,4 @@ namespace Telegrator
Debug.WriteLine(message, args);
}
}
/// <summary>
/// Levels of debug writing
/// </summary>
[Flags]
public enum DebugLevel
{
/// <summary>
/// Write debug messages from filters execution
/// </summary>
Filters = 0x1,
/// <summary>
/// Write debug messages from handlers providers execution
/// </summary>
Providers = 0x2,
/// <summary>
/// Write debug messages from update router's execution
/// </summary>
Router = 0x4,
/// <summary>
/// Write debug messages from handlers pool execution
/// </summary>
HandlersPool = 0x8
}
}
@@ -32,11 +32,13 @@ namespace Telegrator.MadiatorCore
/// <returns>The handler descriptor list for the given update type.</returns>
public HandlerDescriptorList this[UpdateType updateType] { get; }
/*
/// <summary>
/// Collects all handlers domain-wide and returns a new <see cref="IHandlersCollection"/>.
/// </summary>
/// <returns>A new <see cref="IHandlersCollection"/> with all handlers collected.</returns>
public IHandlersCollection CollectHandlersDomainWide();
*/
/// <summary>
/// Adds a <see cref="HandlerDescriptor"/> to the collection and returns the updated collection.
@@ -1,5 +1,6 @@
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegrator.Polling;
namespace Telegrator.MadiatorCore
{
@@ -0,0 +1,36 @@
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegrator.MadiatorCore;
namespace Telegrator.Polling
{
/// <summary>
/// Delegate used to handle <see cref="IUpdateRouter"/> exception
/// </summary>
/// <param name="botClient"></param>
/// <param name="exception"></param>
/// <param name="source"></param>
/// <param name="cancellationToken"></param>
public delegate void RouterExceptionHandler(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken);
/// <summary>
/// Realizes <see cref="IRouterExceptionHandler"/> using function delegate
/// </summary>
/// <param name="handler"></param>
public sealed class DefaultRouterExceptionHandler(RouterExceptionHandler handler) : IRouterExceptionHandler
{
private readonly RouterExceptionHandler _handler = handler;
/// <inheritdoc/>
public void HandleException(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken)
{
try
{
_handler.Invoke(botClient, exception, source, cancellationToken);
}
finally
{
_ = 0xBAD + 0xC0DE;
}
}}
}
@@ -1,6 +1,5 @@
using System.Reflection;
using Telegram.Bot.Types.Enums;
using Telegrator;
using Telegrator.Annotations;
using Telegrator.Attributes;
using Telegrator.Configuration;
@@ -60,25 +59,6 @@ namespace Telegrator.Providers
get => InnerDictionary[updateType];
}
/// <inheritdoc/>
/// <summary>
/// Collects all handlers from the entry assembly domain-wide.
/// Scans for types that implement handlers and adds them to the collection.
/// </summary>
/// <returns>This collection instance for method chaining.</returns>
/// <exception cref="Exception">Thrown when the entry assembly cannot be found.</exception>
public virtual IHandlersCollection CollectHandlersDomainWide()
{
Assembly? entryAssembly = Assembly.GetEntryAssembly() ?? throw new Exception();
entryAssembly.GetExportedTypes()
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
.Where(type => type.IsHandlerRealization())
.ForEach(type => AddHandler(type));
return this;
}
/// <inheritdoc/>
/// <summary>
/// Adds a handler descriptor to the collection.
/// </summary>
@@ -100,7 +80,6 @@ namespace Telegrator.Providers
return this;
}
/// <inheritdoc/>
/// <summary>
/// Adds a handler type to the collection.
/// </summary>
@@ -112,7 +91,6 @@ namespace Telegrator.Providers
return this;
}
/// <inheritdoc/>
/// <summary>
/// Adds a handler type to the collection.
/// </summary>
@@ -138,7 +116,6 @@ namespace Telegrator.Providers
return this;
}
/// <inheritdoc/>
/// <summary>
/// Gets or creates a descriptor list for the specified update type.
/// </summary>
@@ -155,7 +132,6 @@ namespace Telegrator.Providers
return list;
}
/// <inheritdoc/>
/// <summary>
/// Checks for intersecting command aliases and handles them according to configuration.
/// </summary>
+1 -1
View File
@@ -17,7 +17,7 @@
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
</PropertyGroup>
<ItemGroup>
+39 -2
View File
@@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Runtime.CompilerServices;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.Payments;
@@ -166,8 +165,46 @@ namespace Telegrator
/// Extension methods for handlers collections.
/// Provides convenient methods for creating implicit handlers.
/// </summary>
public static class HandlersCollectionExtensions
public static partial class HandlersCollectionExtensions
{
private static readonly string[] skippingAssemblies = ["System", "Microsoft", "Telegrator"];
/// <summary>
/// Collects all handlers from the current app domain.
/// Scans for types that implement handlers and adds them to the collection.
/// </summary>
/// <returns>This collection instance for method chaining.</returns>
/// <exception cref="Exception">Thrown when the entry assembly cannot be found.</exception>
public static IHandlersCollection CollectHandlersDomainWide(this IHandlersCollection handlers)
{
AppDomain.CurrentDomain
.GetAssemblies()
.Where(ass => skippingAssemblies.All(skip => !ass.FullName.Contains(skip)))
.SelectMany(ass => ass.GetExportedTypes())
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
.Where(type => type.IsHandlerRealization())
.ForEach(type => handlers.AddHandler(type));
return handlers;
}
/// <summary>
/// Collects all handlers from the calling this function assembly.
/// Scans for types that implement handlers and adds them to the collection.
/// </summary>
/// <returns>This collection instance for method chaining.</returns>
/// <exception cref="Exception">Thrown when the entry assembly cannot be found.</exception>
public static IHandlersCollection CollectHandlersAssemblyWide(this IHandlersCollection handlers)
{
Assembly.GetCallingAssembly()
.GetExportedTypes()
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
.Where(type => type.IsHandlerRealization())
.ForEach(type => handlers.AddHandler(type));
return handlers;
}
/// <summary>
/// Creates a handler builder for a specific update type.
/// </summary>