| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using TeleFlow.Framework.Updates; |
| | | 4 | | using TeleFlow.Telegram.Internal; |
| | | 5 | | using TeleFlow.Telegram.Schema.Types; |
| | | 6 | | |
| | | 7 | | namespace TeleFlow.Telegram; |
| | | 8 | | |
| | | 9 | | public static class UpdateContextTelegramExtensions |
| | | 10 | | { |
| | | 11 | | public static bool TryGetTelegramUpdate( |
| | | 12 | | this UpdateContext context, |
| | | 13 | | [NotNullWhen(true)] out Update? update) |
| | | 14 | | { |
| | 1 | 15 | | ArgumentNullException.ThrowIfNull(context); |
| | | 16 | | |
| | 1 | 17 | | if (context.Payload is TelegramUpdatePayload payload) |
| | | 18 | | { |
| | 1 | 19 | | update = payload.Update; |
| | 1 | 20 | | return true; |
| | | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | update = null; |
| | 0 | 24 | | return false; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public static TelegramUpdateContext GetTelegramContext(this UpdateContext context) |
| | | 28 | | { |
| | 9 | 29 | | ArgumentNullException.ThrowIfNull(context); |
| | | 30 | | |
| | 9 | 31 | | return context.Services |
| | 9 | 32 | | .GetRequiredService<TelegramContextFactory>() |
| | 9 | 33 | | .CreateTelegramContext(context); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public static MessageContext GetMessageContext(this UpdateContext context) |
| | | 37 | | { |
| | 303 | 38 | | ArgumentNullException.ThrowIfNull(context); |
| | | 39 | | |
| | 303 | 40 | | return context.Services |
| | 303 | 41 | | .GetRequiredService<TelegramContextFactory>() |
| | 303 | 42 | | .CreateMessageContext(context); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public static CallbackQueryContext GetCallbackQueryContext(this UpdateContext context) |
| | | 46 | | { |
| | 89 | 47 | | ArgumentNullException.ThrowIfNull(context); |
| | | 48 | | |
| | 89 | 49 | | return context.Services |
| | 89 | 50 | | .GetRequiredService<TelegramContextFactory>() |
| | 89 | 51 | | .CreateCallbackQueryContext(context); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public static ChatMemberUpdatedContext GetChatMemberUpdatedContext(this UpdateContext context) |
| | | 55 | | { |
| | 15 | 56 | | ArgumentNullException.ThrowIfNull(context); |
| | | 57 | | |
| | 15 | 58 | | return context.Services |
| | 15 | 59 | | .GetRequiredService<TelegramContextFactory>() |
| | 15 | 60 | | .CreateChatMemberUpdatedContext(context); |
| | | 61 | | } |
| | | 62 | | } |