From 03e6ccf5d43fd454035352941137168c067b605a Mon Sep 17 00:00:00 2001 From: Dmitry Popov Date: Fri, 15 Dec 2017 12:36:36 +0300 Subject: [PATCH] v4.2.0: string.MarkdownEncode() extension added --- NetTelegramBotApi/NetTelegramBotApi.csproj | 4 ++-- NetTelegramBotApi/StringExtensions.cs | 19 +++++++++++++++++++ TelegramBotDemo-vNext/Program.cs | 2 +- TelegramBotDemo/Program.cs | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 NetTelegramBotApi/StringExtensions.cs diff --git a/NetTelegramBotApi/NetTelegramBotApi.csproj b/NetTelegramBotApi/NetTelegramBotApi.csproj index 4efb3de..056b675 100644 --- a/NetTelegramBotApi/NetTelegramBotApi.csproj +++ b/NetTelegramBotApi/NetTelegramBotApi.csproj @@ -3,13 +3,13 @@ Telegram Bot API library NetTelegramBotApi - 4.1.0 + 4.2.0 net45;net46;netstandard1.3 1.6.0 NetTelegramBotApi NetTelegramBotApi telegram;bot;api - See https://github.com/justdmitry/NetTelegramBotApi/releases/tag/v4.0.0 + See https://github.com/justdmitry/NetTelegramBotApi/releases/tag/v4.2.0 https://raw.githubusercontent.com/justdmitry/NetTelegramBotApi/master/package-icon.gif https://github.com/justdmitry/NetTelegramBotApi git diff --git a/NetTelegramBotApi/StringExtensions.cs b/NetTelegramBotApi/StringExtensions.cs new file mode 100644 index 0000000..3ec5f6c --- /dev/null +++ b/NetTelegramBotApi/StringExtensions.cs @@ -0,0 +1,19 @@ +namespace System +{ + using System.Text.RegularExpressions; + + public static class StringExtensions + { + private static readonly Regex WrongChars = new Regex(@"([\*_`\[\]\(\)])"); + + public static string MarkdownEncode(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return value; + } + + return WrongChars.Replace(value, @"\$1"); + } + } +} diff --git a/TelegramBotDemo-vNext/Program.cs b/TelegramBotDemo-vNext/Program.cs index 808714d..b8e4702 100644 --- a/TelegramBotDemo-vNext/Program.cs +++ b/TelegramBotDemo-vNext/Program.cs @@ -200,7 +200,7 @@ public static void RunBot(string accessToken) { bot.MakeRequestAsync(new SendMessage( update.Message.Chat.Id, - "You wrote *" + update.Message.Text.Length + " characters*") + "You wrote: \r\n" + update.Message.Text.MarkdownEncode()) { ParseMode = SendMessage.ParseModeEnum.Markdown }).Wait(); diff --git a/TelegramBotDemo/Program.cs b/TelegramBotDemo/Program.cs index 49bfa1e..27d85e1 100644 --- a/TelegramBotDemo/Program.cs +++ b/TelegramBotDemo/Program.cs @@ -171,7 +171,7 @@ public static void RunBot(string accessToken) { bot.MakeRequestAsync(new SendMessage( update.Message.Chat.Id, - "You wrote *" + update.Message.Text.Length + " characters*") + "You wrote: \r\n_" + update.Message.Text.MarkdownEncode() + "_") { ParseMode = SendMessage.ParseModeEnum.Markdown }).Wait();