diff --git a/TelegramBotBase/Markdown/Generator.cs b/TelegramBotBase/Markdown/Generator.cs index 20363dd..05dbfa7 100644 --- a/TelegramBotBase/Markdown/Generator.cs +++ b/TelegramBotBase/Markdown/Generator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Telegram.Bot.Types.Enums; @@ -156,5 +157,22 @@ namespace TelegramBotBase.Markdown } return text; } + + /// + /// Escapes all characters as stated in the documentation: https://core.telegram.org/bots/api#markdownv2-style + /// + /// + /// + public static String MarkdownV2Escape(this String text, params char[] toKeep) + { + char[] toEscape = new char[] { '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!' }; + + return text.EscapeAll(toEscape.Where(a => !toKeep.Contains(a)).Select(a => a.ToString()).ToArray()); + } + + public static string EscapeAll(this string seed, String[] chars, char escapeCharacter = '\\') + { + return chars.Aggregate(seed, (str, cItem) => str.Replace(cItem, escapeCharacter + cItem)); + } } } diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index 8879d91..7384f04 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -16,6 +16,7 @@ using TelegramBotBase.Args; using TelegramBotBase.Base; using TelegramBotBase.Exceptions; using TelegramBotBase.Form; +using TelegramBotBase.Markdown; namespace TelegramBotBase.Sessions { @@ -224,7 +225,7 @@ namespace TelegramBotBase.Sessions /// /// /// - public async Task Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default) + public async Task Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default, bool MarkdownV2AutoEscape = true) { if (this.ActiveForm == null) return null; @@ -238,42 +239,9 @@ namespace TelegramBotBase.Sessions throw new MaxLengthException(text.Length); } - try + if (parseMode == ParseMode.MarkdownV2 && MarkdownV2AutoEscape) { - m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); - - OnMessageSent(new MessageSentEventArgs(m)); - } - catch (ApiRequestException) - { - return null; - } - catch - { - return null; - } - - return m; - } - - /// - /// Sends a simple text message - /// - /// - /// - /// - /// - /// - public async Task Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default) - { - if (this.ActiveForm == null) - return null; - - Message m = null; - - if (text.Length > Constants.Telegram.MaxMessageLength) - { - throw new MaxLengthException(text.Length); + text = text.MarkdownV2Escape(); } try @@ -302,7 +270,7 @@ namespace TelegramBotBase.Sessions /// /// /// - public async Task Send(String text, ReplyMarkupBase markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default) + public async Task Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default, bool MarkdownV2AutoEscape = true) { if (this.ActiveForm == null) return null; @@ -314,6 +282,54 @@ namespace TelegramBotBase.Sessions throw new MaxLengthException(text.Length); } + if (parseMode == ParseMode.MarkdownV2 && MarkdownV2AutoEscape) + { + text = text.MarkdownV2Escape(); + } + + try + { + m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + + OnMessageSent(new MessageSentEventArgs(m)); + } + catch (ApiRequestException) + { + return null; + } + catch + { + return null; + } + + return m; + } + + /// + /// Sends a simple text message + /// + /// + /// + /// + /// + /// + public async Task Send(String text, ReplyMarkupBase markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default, bool MarkdownV2AutoEscape = true) + { + if (this.ActiveForm == null) + return null; + + Message m = null; + + if (text.Length > Constants.Telegram.MaxMessageLength) + { + throw new MaxLengthException(text.Length); + } + + if (parseMode == ParseMode.MarkdownV2 && MarkdownV2AutoEscape) + { + text = text.MarkdownV2Escape(); + } + try { m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); @@ -459,7 +475,7 @@ namespace TelegramBotBase.Sessions try { - m = await this.Client.TelegramClient.SendVideoAsync(this.DeviceId,new InputOnlineFile(url), parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); + m = await this.Client.TelegramClient.SendVideoAsync(this.DeviceId, new InputOnlineFile(url), parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); OnMessageSent(new MessageSentEventArgs(m)); }