all repos — telegram-bot-api @ 6aa05225a6152ebca3dcb6495e00a6f6c78a2f32

Golang bindings for the Telegram Bot API

Merge branch 'develop' into files
Syfaro syfaro@huefox.com
Fri, 20 Aug 2021 16:11:25 -0400
commit

6aa05225a6152ebca3dcb6495e00a6f6c78a2f32

parent

1a3364aea80373adbdfde143541ebe265cc57b93

2 files changed, 40 insertions(+), 6 deletions(-)

jump to
M bot.gobot.go

@@ -18,7 +18,6 @@

// HTTPClient is the type needed for the bot to perform HTTP requests. type HTTPClient interface { Do(req *http.Request) (*http.Response, error) - PostForm(url string, data url.Values) (*http.Response, error) } // BotAPI allows you to interact with the Telegram Bot API.

@@ -78,18 +77,18 @@ func (bot *BotAPI) SetAPIEndpoint(apiEndpoint string) {

bot.apiEndpoint = apiEndpoint } -func buildParams(in Params) (out url.Values) { +func buildParams(in Params) url.Values { if in == nil { return url.Values{} } - out = url.Values{} + out := url.Values{} for key, value := range in { out.Set(key, value) } - return + return out } // MakeRequest makes a request to a specific endpoint with our token.

@@ -102,7 +101,13 @@ method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)

values := buildParams(params) - resp, err := bot.Client.PostForm(method, values) + req, err := http.NewRequest("POST", method, strings.NewReader(values.Encode())) + if err != nil { + return &APIResponse{}, err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := bot.Client.Do(req) if err != nil { return nil, err }

@@ -670,3 +675,31 @@ err = json.Unmarshal(resp.Result, &messageID)

return messageID, err } + +// EscapeText takes an input text and escape Telegram markup symbols. +// In this way we can send a text without being afraid of having to escape the characters manually. +// Note that you don't have to include the formatting style in the input text, or it will be escaped too. +// If there is an error, an empty string will be returned. +// +// parseMode is the text formatting mode (ModeMarkdown, ModeMarkdownV2 or ModeHTML) +// text is the input string that will be escaped +func EscapeText(parseMode string, text string) string { + var replacer *strings.Replacer + + if parseMode == ModeHTML { + replacer = strings.NewReplacer("<", "&lt;", ">", "&gt;", "&", "&amp;") + } else if parseMode == ModeMarkdown { + replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[") + } else if parseMode == ModeMarkdownV2 { + replacer = strings.NewReplacer( + "_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", + "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", + "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", + "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!", + ) + } else { + return "" + } + + return replacer.Replace(text) +}
M bot_test.gobot_test.go

@@ -1002,10 +1002,11 @@ }

} // TODO: figure out why test is failing +// // func TestEditMessageMedia(t *testing.T) { // bot, _ := getBot(t) -// msg := NewPhoto(ChatID, FilePath("tests/image.jpg")) +// msg := NewPhoto(ChatID, "tests/image.jpg") // msg.Caption = "Test" // m, err := bot.Send(msg)