From a814325e9a50fce477449754607faddf0d8567a5 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Wed, 14 Oct 2020 14:08:07 +0200 Subject: [PATCH] feat: Added EscapeMarkdown and Client#SendMarkdownMessage --- client.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/client.go b/client.go index c8258b2..7f3ce9a 100644 --- a/client.go +++ b/client.go @@ -7,8 +7,11 @@ import ( "net/http" "net/url" "strconv" + "strings" ) +const badChars = "_*[]()~`>#+-=|{}.!" + type Client struct { Key string Offset int64 @@ -37,6 +40,18 @@ func (c *Client) GetUpdates() (Response, error) { return res, err } +func EscapeMarkdown(msg string) string { + for _, char := range badChars { + ch := string(char) + msg = strings.Replace(msg, ch, "\\"+ch, -1) + } + return msg +} + +func (c *Client) SendMarkdownMessage(chatId, body string) (*SendMessageResponse, error) { + return c.SendMessage(chatId, body, url.Values{"parse_mode": {"MarkdownV2"}}) +} + func (c *Client) SendMessage(chatId, body string, options ...url.Values) (ret *SendMessageResponse, err error) { values := url.Values{ "chat_id": {chatId},