Telegram.Bot icon indicating copy to clipboard operation
Telegram.Bot copied to clipboard

Feature: Sanitize message string when using MarkdownV2 parse mode

Open sangxxh opened this issue 3 years ago • 4 comments

Example:

var message = "A message!";
await botClient.SendTextMessageAsync(chatId, message, parseMode: ParseMode.MarkdownV2);

I frequently bump into this exception:

Telegram.Bot: Bad Request: can't parse entities: Character '!' is reserved and must be escaped with the preceding '\'.

It'd be nice to automatically sanitize the message string so that the code doesn't have to do this manually.

sangxxh avatar Dec 19 '22 11:12 sangxxh

I think this kind of stuff is out of the scope of an SDK. Different ways of sanitization must be opted-out so the developer can decide to use different strategies or libraries of its favor.

mehrandvd avatar Jan 07 '23 23:01 mehrandvd

We'll consider adding something to help with this

tuscen avatar Jan 08 '23 14:01 tuscen

Something like; ``` private string getEscsapedMarkdown(string input) { var response = input; var list = new List<String> { "_", "*", "[", "]", "(", ")", "~", "`", "<", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!" };

        foreach (var special in list)
        {
            response = response.Replace(special, $"\\{special}");
        }

        return response;
    }

?

gijsbertpieterbrouwer avatar Feb 15 '23 09:02 gijsbertpieterbrouwer

Try this Tools.EscapeMarkdown

karb0f0s avatar Apr 03 '23 18:04 karb0f0s