- adding MarkdownV2 escape function
- adding MarkdownV2 autoescape feature
This commit is contained in:
parent
fd6f6fef34
commit
2ed030b2fb
@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
@ -156,5 +157,22 @@ namespace TelegramBotBase.Markdown
|
|||||||
}
|
}
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Escapes all characters as stated in the documentation: https://core.telegram.org/bots/api#markdownv2-style
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@ using TelegramBotBase.Args;
|
|||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Exceptions;
|
using TelegramBotBase.Exceptions;
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
|
using TelegramBotBase.Markdown;
|
||||||
|
|
||||||
namespace TelegramBotBase.Sessions
|
namespace TelegramBotBase.Sessions
|
||||||
{
|
{
|
||||||
@ -224,7 +225,7 @@ namespace TelegramBotBase.Sessions
|
|||||||
/// <param name="replyTo"></param>
|
/// <param name="replyTo"></param>
|
||||||
/// <param name="disableNotification"></param>
|
/// <param name="disableNotification"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<Message> Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default)
|
public async Task<Message> Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default, bool MarkdownV2AutoEscape = true)
|
||||||
{
|
{
|
||||||
if (this.ActiveForm == null)
|
if (this.ActiveForm == null)
|
||||||
return null;
|
return null;
|
||||||
@ -238,42 +239,9 @@ namespace TelegramBotBase.Sessions
|
|||||||
throw new MaxLengthException(text.Length);
|
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));
|
text = text.MarkdownV2Escape();
|
||||||
|
|
||||||
OnMessageSent(new MessageSentEventArgs(m));
|
|
||||||
}
|
|
||||||
catch (ApiRequestException)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends a simple text message
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="text"></param>
|
|
||||||
/// <param name="markup"></param>
|
|
||||||
/// <param name="replyTo"></param>
|
|
||||||
/// <param name="disableNotification"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<Message> 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -302,7 +270,7 @@ namespace TelegramBotBase.Sessions
|
|||||||
/// <param name="replyTo"></param>
|
/// <param name="replyTo"></param>
|
||||||
/// <param name="disableNotification"></param>
|
/// <param name="disableNotification"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<Message> Send(String text, ReplyMarkupBase markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default)
|
public async Task<Message> Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default, bool MarkdownV2AutoEscape = true)
|
||||||
{
|
{
|
||||||
if (this.ActiveForm == null)
|
if (this.ActiveForm == null)
|
||||||
return null;
|
return null;
|
||||||
@ -314,6 +282,54 @@ namespace TelegramBotBase.Sessions
|
|||||||
throw new MaxLengthException(text.Length);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a simple text message
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text"></param>
|
||||||
|
/// <param name="markup"></param>
|
||||||
|
/// <param name="replyTo"></param>
|
||||||
|
/// <param name="disableNotification"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<Message> 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
|
try
|
||||||
{
|
{
|
||||||
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
||||||
@ -459,7 +475,7 @@ namespace TelegramBotBase.Sessions
|
|||||||
|
|
||||||
try
|
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));
|
OnMessageSent(new MessageSentEventArgs(m));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user