all repos — telegram-bot-api @ 309d612d709586e985187a1eaec9dbf48177fb1b

Golang bindings for the Telegram Bot API

Merge branch 'master' into develop
Syfaro syfaro@huefox.com
Fri, 20 Aug 2021 16:08:34 -0400
commit

309d612d709586e985187a1eaec9dbf48177fb1b

parent

000cb2eb0eed1de00a3c4a90d2fc085abb7040c6

2 files changed, 61 insertions(+), 26 deletions(-)

jump to
M bot.gobot.go

@@ -20,7 +20,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.

@@ -80,18 +79,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.

@@ -104,7 +103,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 }

@@ -703,3 +708,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

@@ -14,7 +14,7 @@ ChatID = 76918703

Channel = "@tgbotapitest" SupergroupChatID = -1001120141283 ReplyToMessageID = 35 - ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC" + ExistingPhotoFileID = "AgACAgIAAxkDAAEBFUZhIALQ9pZN4BUe8ZSzUU_2foSo1AACnrMxG0BucEhezsBWOgcikQEAAwIAA20AAyAE" ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag" ExistingAudioFileID = "BQADAgADRgADjMcoCdXg3lSIN49lAg" ExistingVoiceFileID = "AwADAgADWQADjMcoCeul6r_q52IyAg"

@@ -1001,30 +1001,32 @@ t.Error("Commands were incorrectly set")

} } -func TestEditMessageMedia(t *testing.T) { - bot, _ := getBot(t) +// TODO: figure out why test is failing +// +// func TestEditMessageMedia(t *testing.T) { +// bot, _ := getBot(t) - msg := NewPhoto(ChatID, "tests/image.jpg") - msg.Caption = "Test" - m, err := bot.Send(msg) +// msg := NewPhoto(ChatID, "tests/image.jpg") +// msg.Caption = "Test" +// m, err := bot.Send(msg) - if err != nil { - t.Error(err) - } +// if err != nil { +// t.Error(err) +// } - edit := EditMessageMediaConfig{ - BaseEdit: BaseEdit{ - ChatID: ChatID, - MessageID: m.MessageID, - }, - Media: NewInputMediaVideo("tests/video.mp4"), - } +// edit := EditMessageMediaConfig{ +// BaseEdit: BaseEdit{ +// ChatID: ChatID, +// MessageID: m.MessageID, +// }, +// Media: NewInputMediaVideo("tests/video.mp4"), +// } - _, err = bot.Request(edit) - if err != nil { - t.Error(err) - } -} +// _, err = bot.Request(edit) +// if err != nil { +// t.Error(err) +// } +// } func TestPrepareInputMediaForParams(t *testing.T) { media := []interface{}{