diff --git a/src/Managing.Application/Users/UserService.cs b/src/Managing.Application/Users/UserService.cs index 25b39c17..20d24ed2 100644 --- a/src/Managing.Application/Users/UserService.cs +++ b/src/Managing.Application/Users/UserService.cs @@ -270,13 +270,28 @@ public class UserService : IUserService public async Task UpdateTelegramChannel(User user, string telegramChannel) { - // Validate Telegram channel format (numeric channel ID only) + // Validate Telegram channel format (numeric channel ID or URL) if (!string.IsNullOrEmpty(telegramChannel)) { - string pattern = @"^[0-9]{5,15}$"; - if (!Regex.IsMatch(telegramChannel, pattern)) + // Check if it's a numeric channel ID (5-15 digits) + string numericPattern = @"^[0-9]{5,15}$"; + if (Regex.IsMatch(telegramChannel, numericPattern)) { - throw new Exception("Invalid Telegram channel format. Must be numeric channel ID (5-15 digits)."); + // Valid numeric format + } + // Check if it's a Telegram URL format + else if (telegramChannel.StartsWith("https://web.telegram.org/k/#", StringComparison.OrdinalIgnoreCase)) + { + // Extract the channel ID from the URL (format: https://web.telegram.org/k/#-2224918667) + var match = Regex.Match(telegramChannel, @"#-?(\d{5,15})$"); + if (!match.Success) + { + throw new Exception("Invalid Telegram URL format. URL must contain a valid channel ID (5-15 digits)."); + } + } + else + { + throw new Exception("Invalid Telegram channel format. Must be numeric channel ID (5-15 digits) or Telegram URL (https://web.telegram.org/k/#-CHANNEL_ID)."); } } diff --git a/src/Managing.Common/Formatings.cs b/src/Managing.Common/Formatings.cs index 9d95ba14..438206f3 100644 --- a/src/Managing.Common/Formatings.cs +++ b/src/Managing.Common/Formatings.cs @@ -1,3 +1,5 @@ +using System.Text.RegularExpressions; + namespace Managing.Common; public static class Formatings @@ -9,15 +11,35 @@ public static class Formatings return string.Empty; } - if (telegramChannel.StartsWith("100")) + // If it's a URL format, extract the numeric channel ID + if (telegramChannel.StartsWith("https://web.telegram.org/k/#", StringComparison.OrdinalIgnoreCase)) { - return telegramChannel.Substring(3); - } - else if (telegramChannel.StartsWith("-100")) - { - return telegramChannel.Substring(4); + var match = Regex.Match(telegramChannel, @"#-?(\d{5,15})$"); + if (match.Success) + { + // Extract the numeric ID (without the leading minus if present) + var channelId = match.Groups[1].Value; + return FormatNumericChannelId(channelId); + } + // If URL format but can't extract ID, return as-is (shouldn't happen if validation is correct) + return telegramChannel; } - return telegramChannel; + // Handle numeric channel IDs + return FormatNumericChannelId(telegramChannel); + } + + private static string FormatNumericChannelId(string channelId) + { + if (channelId.StartsWith("100")) + { + return channelId.Substring(3); + } + else if (channelId.StartsWith("-100")) + { + return channelId.Substring(4); + } + + return channelId; } } \ No newline at end of file