| | | 1 | | using System.Globalization; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal.Options; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Stores Telegram-specific state-key settings resolved during Telegram framework registration, |
| | | 7 | | /// before updates reach state middleware and handler dispatch. |
| | | 8 | | /// </summary> |
| | | 9 | | internal sealed record class TelegramStateKeyOptions(long? BotId) |
| | | 10 | | { |
| | | 11 | | public static TelegramStateKeyOptions FromToken(string token) |
| | | 12 | | { |
| | 480 | 13 | | return TryResolveBotId(token, out var botId) |
| | 480 | 14 | | ? new TelegramStateKeyOptions(botId) |
| | 480 | 15 | | : new TelegramStateKeyOptions(BotId: null); |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | private static bool TryResolveBotId(string token, out long botId) |
| | | 19 | | { |
| | 480 | 20 | | botId = default; |
| | | 21 | | |
| | 480 | 22 | | if (string.IsNullOrWhiteSpace(token)) |
| | | 23 | | { |
| | 0 | 24 | | return false; |
| | | 25 | | } |
| | | 26 | | |
| | 480 | 27 | | var separatorIndex = token.IndexOf(':', StringComparison.Ordinal); |
| | | 28 | | |
| | 480 | 29 | | return separatorIndex > 0 && |
| | 480 | 30 | | long.TryParse( |
| | 480 | 31 | | token.AsSpan(0, separatorIndex), |
| | 480 | 32 | | NumberStyles.None, |
| | 480 | 33 | | CultureInfo.InvariantCulture, |
| | 480 | 34 | | out botId); |
| | | 35 | | } |
| | | 36 | | } |