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)
|
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))
|
if (!string.IsNullOrEmpty(telegramChannel))
|
||||||
{
|
{
|
||||||
string pattern = @"^[0-9]{5,15}$";
|
// Check if it's a numeric channel ID (5-15 digits)
|
||||||
if (!Regex.IsMatch(telegramChannel, pattern))
|
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).");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Managing.Common;
|
namespace Managing.Common;
|
||||||
|
|
||||||
public static class Formatings
|
public static class Formatings
|
||||||
@@ -9,15 +11,35 @@ public static class Formatings
|
|||||||
return string.Empty;
|
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);
|
var match = Regex.Match(telegramChannel, @"#-?(\d{5,15})$");
|
||||||
}
|
if (match.Success)
|
||||||
else if (telegramChannel.StartsWith("-100"))
|
|
||||||
{
|
{
|
||||||
return telegramChannel.Substring(4);
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user