-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
78 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
using Telegram.Bot.Extensions.Markup; | ||
|
||
namespace SpinRallyBot.Utils; | ||
|
||
public static class UserExtensions { | ||
private static string MentionMarkdown(ChatId userId, string name, ParseMode parseMode = ParseMode.Markdown) { | ||
string tgLink = $"tg://user?id={userId}"; | ||
return parseMode == ParseMode.Markdown | ||
? $"[{name}]({tgLink})" | ||
: $"[{StringExtensions.EscapeMarkdown(name, parseMode)}]({tgLink})"; | ||
} | ||
|
||
public static string ToMentionMarkdownV2(this User user) { | ||
return Tools.MentionMarkdown(user.Id, | ||
user.Username is not null ? $"@{user.Username}" : $"{user.FirstName} {user.LastName}".Trim(), | ||
return MentionMarkdown( | ||
user.Id, user.Username is not null ? $"@{user.Username}" : $"{user.FirstName} {user.LastName}".Trim(), | ||
ParseMode.MarkdownV2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="Telegram.Bot.Extensions.Markup" /> | ||
<PackageReference Include="Telegram.Bot" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,47 @@ | ||
using Telegram.Bot.Extensions.Markup; | ||
using System.Collections.Immutable; | ||
using System.Text.RegularExpressions; | ||
using Telegram.Bot.Types.Enums; | ||
|
||
namespace SpinRallyBot; | ||
|
||
public static class StringExtensions { | ||
private static ImmutableDictionary<(ParseMode parseMode, MessageEntityType? entityType), string> Escaped => | ||
new Dictionary<(ParseMode parseMode, MessageEntityType? entityType), string> { | ||
{ (ParseMode.Markdown, null), Regex.Escape("_*`[") }, | ||
{ (ParseMode.MarkdownV2, MessageEntityType.Pre), Regex.Escape("""\`""") }, | ||
{ (ParseMode.MarkdownV2, MessageEntityType.Code), Regex.Escape("""\`""") }, | ||
{ (ParseMode.MarkdownV2, MessageEntityType.TextLink), Regex.Escape("""\)""") }, { | ||
(ParseMode.MarkdownV2, null), | ||
Regex.Escape(str: """\_*()~`>#+-=|{}.![]""") | ||
.Replace("]", "\\]", StringComparison.Ordinal) | ||
.Replace("-", "\\-", StringComparison.Ordinal) | ||
}, | ||
}.ToImmutableDictionary(); | ||
|
||
public static string EscapeMarkdown( | ||
string text, | ||
ParseMode parseMode = ParseMode.Markdown, | ||
MessageEntityType? entityType = default) { | ||
string escaped = (parseMode, entityType) switch { | ||
(ParseMode.Markdown, _) => Escaped[(ParseMode.Markdown, null)], | ||
(ParseMode.MarkdownV2, MessageEntityType.Pre) => Escaped[(ParseMode.MarkdownV2, MessageEntityType.Pre)], | ||
(ParseMode.MarkdownV2, MessageEntityType.Code) => Escaped[(ParseMode.MarkdownV2, MessageEntityType.Code)], | ||
(ParseMode.MarkdownV2, MessageEntityType.TextLink) => Escaped[ | ||
(ParseMode.MarkdownV2, MessageEntityType.TextLink)], | ||
(ParseMode.MarkdownV2, _) => Escaped[(ParseMode.MarkdownV2, null)], | ||
_ => throw new ArgumentException("Only ParseMode.Markdown and ParseMode.MarkdownV2 allowed.", | ||
nameof(parseMode)), | ||
}; | ||
|
||
return Regex.Replace( | ||
input: text, | ||
pattern: $"([{escaped}])", | ||
replacement: """\$1""", | ||
RegexOptions.CultureInvariant, | ||
matchTimeout: TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
public static string ToEscapedMarkdownV2(this string str) { | ||
return Tools.EscapeMarkdown(str, ParseMode.MarkdownV2); | ||
return EscapeMarkdown(str, ParseMode.MarkdownV2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters