| | | 1 | | using System.Reflection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | using TeleFlow.Framework.Application; |
| | | 5 | | using TeleFlow.Framework.Callbacks; |
| | | 6 | | using TeleFlow.Framework.Dispatching; |
| | | 7 | | using TeleFlow.Framework.States; |
| | | 8 | | using TeleFlow.Framework.Updates; |
| | | 9 | | using TeleFlow.Telegram.Internal; |
| | | 10 | | using TeleFlow.Telegram.Internal.Handlers; |
| | | 11 | | using TeleFlow.Telegram.Internal.Options; |
| | | 12 | | |
| | | 13 | | namespace TeleFlow.Telegram; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Registers Telegram bot framework services, handler metadata, and Telegram-specific extension points. |
| | | 17 | | /// </summary> |
| | | 18 | | public static class TelegramServiceCollectionExtensions |
| | | 19 | | { |
| | | 20 | | private const string ReflectionAssemblyRegistrationObsoleteDiagnosticId = "TLF900"; |
| | | 21 | | |
| | | 22 | | private const string ReflectionAssemblyRegistrationObsoleteMessage = |
| | | 23 | | "Reflection-based assembly handler registration is deprecated and will be removed before 1.0. " + |
| | | 24 | | "Use AddTelegramHandlersFromAssembly with IWF.TeleFlow.Generators, or register handlers explicitly with AddTeleg |
| | | 25 | | |
| | | 26 | | public static IServiceCollection AddTelegramBot( |
| | | 27 | | this IServiceCollection services, |
| | | 28 | | Action<TelegramBotOptions> configure) |
| | | 29 | | { |
| | 481 | 30 | | ArgumentNullException.ThrowIfNull(services); |
| | 481 | 31 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 32 | | |
| | 481 | 33 | | EnsureNotRegistered<TelegramBotOptions>(services, nameof(AddTelegramBot)); |
| | | 34 | | |
| | 481 | 35 | | var options = new TelegramBotOptions(); |
| | 481 | 36 | | configure(options); |
| | 481 | 37 | | TelegramBotOptionsValidator.Validate(options); |
| | 480 | 38 | | var stateKeyOptions = TelegramStateKeyOptions.FromToken(options.Token); |
| | | 39 | | |
| | 480 | 40 | | services.AddTelegramClient(clientOptions => |
| | 480 | 41 | | { |
| | 480 | 42 | | clientOptions.Token = options.Token; |
| | 480 | 43 | | clientOptions.BotUsername = options.BotUsername; |
| | 480 | 44 | | clientOptions.BaseUrl = options.BaseUrl; |
| | 480 | 45 | | clientOptions.Environment = options.Environment; |
| | 480 | 46 | | clientOptions.Defaults = options.Defaults; |
| | 480 | 47 | | clientOptions.RetryAfter = options.RetryAfter; |
| | 960 | 48 | | }); |
| | | 49 | | |
| | 471 | 50 | | services.AddSingleton(options); |
| | 471 | 51 | | services.AddSingleton(stateKeyOptions); |
| | 471 | 52 | | services.AddSingleton<TelegramBotIdentity>(); |
| | 471 | 53 | | services.AddUpdateContextAccessor(); |
| | 471 | 54 | | services.TryAddSingleton(options.RoleFilter); |
| | 471 | 55 | | services.AddSingleton<TelegramContextFactory>(); |
| | 471 | 56 | | services.TryAddScoped<ITelegramCurrentUpdateAccessor, TelegramCurrentUpdateAccessor>(); |
| | 471 | 57 | | services.TryAddSingleton<IStateStorageKeyBuilder, DefaultStateStorageKeyBuilder>(); |
| | 471 | 58 | | services.TryAddSingleton<IStateKeyFactory, TelegramStateKeyFactory>(); |
| | 471 | 59 | | services.TryAddSingleton<ICallbackDataSerializer, JsonCallbackDataSerializer>(); |
| | 471 | 60 | | services.TryAddSingleton<ITelegramChatMemberStatusResolver, TelegramChatMemberStatusResolver>(); |
| | 471 | 61 | | services.TryAddSingleton<ITelegramChatMemberStatusCache, MemoryTelegramChatMemberStatusCache>(); |
| | 471 | 62 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<ITeleFlowRuntimeValidator, TelegramRuntimeDependencyValida |
| | | 63 | | |
| | 471 | 64 | | return services; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public static IServiceCollection AddTelegramChatMemberStatusResolver<TResolver>( |
| | | 68 | | this IServiceCollection services) |
| | | 69 | | where TResolver : class, ITelegramChatMemberStatusResolver |
| | | 70 | | { |
| | 0 | 71 | | ArgumentNullException.ThrowIfNull(services); |
| | | 72 | | |
| | 0 | 73 | | services.RemoveAll<ITelegramChatMemberStatusResolver>(); |
| | 0 | 74 | | services.AddSingleton<ITelegramChatMemberStatusResolver, TResolver>(); |
| | 0 | 75 | | return services; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public static IServiceCollection AddTelegramChatMemberStatusCache<TCache>( |
| | | 79 | | this IServiceCollection services) |
| | | 80 | | where TCache : class, ITelegramChatMemberStatusCache |
| | | 81 | | { |
| | 0 | 82 | | ArgumentNullException.ThrowIfNull(services); |
| | | 83 | | |
| | 0 | 84 | | services.RemoveAll<ITelegramChatMemberStatusCache>(); |
| | 0 | 85 | | services.AddSingleton<ITelegramChatMemberStatusCache, TCache>(); |
| | 0 | 86 | | return services; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public static IServiceCollection AddAutoCallbackAnswer( |
| | | 90 | | this IServiceCollection services, |
| | | 91 | | Action<TelegramAutoAnswerCallbackOptions>? configure = null) |
| | | 92 | | { |
| | 6 | 93 | | ArgumentNullException.ThrowIfNull(services); |
| | | 94 | | |
| | 6 | 95 | | EnsureTelegramBotRegistered(services, nameof(AddAutoCallbackAnswer)); |
| | | 96 | | |
| | 6 | 97 | | var options = new TelegramAutoAnswerCallbackOptions(); |
| | 6 | 98 | | configure?.Invoke(options); |
| | 6 | 99 | | ValidateAutoAnswerCallbackOptions(options); |
| | | 100 | | |
| | 6 | 101 | | services.RemoveAll<TelegramAutoAnswerCallbackOptions>(); |
| | 6 | 102 | | services.AddSingleton(options); |
| | 6 | 103 | | return services; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Registers all generated Telegram handler metadata from an assembly. |
| | | 108 | | /// This API requires build-time metadata from <c>IWF.TeleFlow.Generators</c> and never falls back to assembly scann |
| | | 109 | | /// </summary> |
| | | 110 | | public static IServiceCollection AddTelegramHandlersFromAssembly( |
| | | 111 | | this IServiceCollection services, |
| | | 112 | | Assembly assembly) |
| | | 113 | | { |
| | 31 | 114 | | ArgumentNullException.ThrowIfNull(services); |
| | 31 | 115 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 116 | | |
| | 31 | 117 | | EnsureTelegramBotRegistered(services, nameof(AddTelegramHandlersFromAssembly)); |
| | | 118 | | |
| | 31 | 119 | | var generatedRegistrars = TelegramHandlerAssemblyScanner.GetGeneratedRegistrars(assembly); |
| | | 120 | | |
| | 31 | 121 | | if (generatedRegistrars.Length > 0) |
| | | 122 | | { |
| | 30 | 123 | | TelegramHandlerServiceRegistrar.RegisterGeneratedHandlers(services, assembly, generatedRegistrars); |
| | 29 | 124 | | return services; |
| | | 125 | | } |
| | | 126 | | |
| | 1 | 127 | | throw new InvalidOperationException( |
| | 1 | 128 | | $"Assembly '{assembly.FullName}' does not contain generated Telegram handler metadata. " + |
| | 1 | 129 | | $"Use {nameof(AddTelegramHandler)}<THandler>() or {nameof(AddTelegramModule)}<TModule>() for explicit direct |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Registers all Telegram handler types discovered by scanning an assembly at startup. |
| | | 134 | | /// This API is a deprecated migration path and should not be used by new applications. |
| | | 135 | | /// </summary> |
| | | 136 | | [Obsolete( |
| | | 137 | | ReflectionAssemblyRegistrationObsoleteMessage, |
| | | 138 | | DiagnosticId = ReflectionAssemblyRegistrationObsoleteDiagnosticId)] |
| | | 139 | | public static IServiceCollection AddTelegramHandlersFromAssemblyReflection( |
| | | 140 | | this IServiceCollection services, |
| | | 141 | | Assembly assembly) |
| | | 142 | | { |
| | 13 | 143 | | ArgumentNullException.ThrowIfNull(services); |
| | 13 | 144 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 145 | | |
| | 13 | 146 | | EnsureTelegramBotRegistered(services, nameof(AddTelegramHandlersFromAssemblyReflection)); |
| | 13 | 147 | | TelegramHandlerRegistrationValidator.EnsureAssemblyCanRegister( |
| | 13 | 148 | | services, |
| | 13 | 149 | | assembly, |
| | 13 | 150 | | TelegramHandlerRegistrationMode.Reflection); |
| | | 151 | | |
| | 12 | 152 | | var handlerTypes = TelegramHandlerAssemblyScanner.GetHandlerTypes(assembly); |
| | | 153 | | |
| | 11 | 154 | | TelegramHandlerServiceRegistrar.RegisterReflectionHandlers(services, assembly, handlerTypes); |
| | 10 | 155 | | return services; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Registers one explicitly named handler type by building its metadata at startup without scanning its assembly. |
| | | 160 | | /// </summary> |
| | | 161 | | public static IServiceCollection AddTelegramHandler<THandler>(this IServiceCollection services) |
| | | 162 | | where THandler : class |
| | | 163 | | { |
| | 334 | 164 | | ArgumentNullException.ThrowIfNull(services); |
| | | 165 | | |
| | 334 | 166 | | EnsureTelegramBotRegistered(services, nameof(AddTelegramHandler)); |
| | 334 | 167 | | TelegramHandlerServiceRegistrar.RegisterHandlerTypes(services, [typeof(THandler)]); |
| | | 168 | | |
| | 307 | 169 | | return services; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Registers one explicitly named Telegram module type. Generated metadata is used when available; otherwise |
| | | 174 | | /// metadata is built at startup for the named module type only. |
| | | 175 | | /// </summary> |
| | | 176 | | public static IServiceCollection AddTelegramModule<TModule>(this IServiceCollection services) |
| | | 177 | | where TModule : class |
| | | 178 | | { |
| | 8 | 179 | | ArgumentNullException.ThrowIfNull(services); |
| | | 180 | | |
| | 8 | 181 | | EnsureTelegramBotRegistered(services, nameof(AddTelegramModule)); |
| | | 182 | | |
| | 8 | 183 | | var moduleType = typeof(TModule); |
| | 8 | 184 | | var generatedRegistrars = TelegramHandlerAssemblyScanner.GetGeneratedRegistrars(moduleType.Assembly); |
| | | 185 | | |
| | 8 | 186 | | if (generatedRegistrars.Length > 0 && |
| | 8 | 187 | | TelegramHandlerServiceRegistrar.TryRegisterGeneratedHandlerType(services, moduleType, generatedRegistrars)) |
| | | 188 | | { |
| | 2 | 189 | | return services; |
| | | 190 | | } |
| | | 191 | | |
| | 5 | 192 | | TelegramHandlerServiceRegistrar.RegisterModuleType(services, moduleType); |
| | | 193 | | |
| | 4 | 194 | | return services; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | private static void EnsureTelegramBotRegistered(IServiceCollection services, string apiName) |
| | | 198 | | { |
| | 4836 | 199 | | if (services.All(static descriptor => descriptor.ServiceType != typeof(TelegramBotOptions))) |
| | | 200 | | { |
| | 0 | 201 | | throw new InvalidOperationException( |
| | 0 | 202 | | $"{nameof(AddTelegramBot)} must be called before {apiName}."); |
| | | 203 | | } |
| | 392 | 204 | | } |
| | | 205 | | |
| | | 206 | | private static void EnsureNotRegistered<TService>(IServiceCollection services, string apiName) |
| | | 207 | | { |
| | 1741 | 208 | | if (services.Any(descriptor => descriptor.ServiceType == typeof(TService))) |
| | | 209 | | { |
| | 0 | 210 | | throw new InvalidOperationException( |
| | 0 | 211 | | $"{apiName} cannot be called more than once for service '{typeof(TService).Name}'."); |
| | | 212 | | } |
| | 481 | 213 | | } |
| | | 214 | | |
| | | 215 | | private static void ValidateAutoAnswerCallbackOptions(TelegramAutoAnswerCallbackOptions options) |
| | | 216 | | { |
| | 6 | 217 | | ArgumentNullException.ThrowIfNull(options); |
| | | 218 | | |
| | 6 | 219 | | if (options.Text is not null && string.IsNullOrWhiteSpace(options.Text)) |
| | | 220 | | { |
| | 0 | 221 | | throw new InvalidOperationException("Auto callback answer text must not be empty."); |
| | | 222 | | } |
| | | 223 | | |
| | 6 | 224 | | options.Text = options.Text?.Trim(); |
| | 6 | 225 | | } |
| | | 226 | | |
| | | 227 | | } |