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:
2025-11-24 00:23:42 +07:00
parent 47bea1b9b7
commit 4e797c615b
2 changed files with 48 additions and 11 deletions

View File

@@ -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;
}
}