Enhance Telegram channel validation in UserService and Formatings
- Updated UpdateTelegramChannel method to support both numeric channel IDs and Telegram URL formats. - Improved error handling for invalid formats, ensuring clear exceptions for users. - Refactored Formatings class to extract numeric channel IDs from URLs and handle formatting consistently.
This commit is contained in:
@@ -270,13 +270,28 @@ public class UserService : IUserService
|
||||
|
||||
public async Task<User> 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).");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user