Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Sanitize message string when using MarkdownV2 parse mode #1176

Open
sangxxh opened this issue Dec 19, 2022 · 4 comments
Open

Feature: Sanitize message string when using MarkdownV2 parse mode #1176

sangxxh opened this issue Dec 19, 2022 · 4 comments

Comments

@sangxxh
Copy link

sangxxh commented Dec 19, 2022

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.

@mehrandvd
Copy link

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.

@tuscen
Copy link
Member

tuscen commented Jan 8, 2023

We'll consider adding something to help with this

@gijsbertpieterbrouwer
Copy link

gijsbertpieterbrouwer commented Feb 15, 2023

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

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

        return response;
    }

?

@karb0f0s
Copy link
Member

karb0f0s commented Apr 3, 2023

Try this Tools.EscapeMarkdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants