Skip to content

Commit 6aa0522

Browse files
committed
Merge branch 'develop' into files
2 parents 1a3364a + 309d612 commit 6aa0522

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

bot.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
// HTTPClient is the type needed for the bot to perform HTTP requests.
1919
type HTTPClient interface {
2020
Do(req *http.Request) (*http.Response, error)
21-
PostForm(url string, data url.Values) (*http.Response, error)
2221
}
2322

2423
// BotAPI allows you to interact with the Telegram Bot API.
@@ -78,18 +77,18 @@ func (bot *BotAPI) SetAPIEndpoint(apiEndpoint string) {
7877
bot.apiEndpoint = apiEndpoint
7978
}
8079

81-
func buildParams(in Params) (out url.Values) {
80+
func buildParams(in Params) url.Values {
8281
if in == nil {
8382
return url.Values{}
8483
}
8584

86-
out = url.Values{}
85+
out := url.Values{}
8786

8887
for key, value := range in {
8988
out.Set(key, value)
9089
}
9190

92-
return
91+
return out
9392
}
9493

9594
// MakeRequest makes a request to a specific endpoint with our token.
@@ -102,7 +101,13 @@ func (bot *BotAPI) MakeRequest(endpoint string, params Params) (*APIResponse, er
102101

103102
values := buildParams(params)
104103

105-
resp, err := bot.Client.PostForm(method, values)
104+
req, err := http.NewRequest("POST", method, strings.NewReader(values.Encode()))
105+
if err != nil {
106+
return &APIResponse{}, err
107+
}
108+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
109+
110+
resp, err := bot.Client.Do(req)
106111
if err != nil {
107112
return nil, err
108113
}
@@ -670,3 +675,31 @@ func (bot *BotAPI) CopyMessage(config CopyMessageConfig) (MessageID, error) {
670675

671676
return messageID, err
672677
}
678+
679+
// EscapeText takes an input text and escape Telegram markup symbols.
680+
// In this way we can send a text without being afraid of having to escape the characters manually.
681+
// Note that you don't have to include the formatting style in the input text, or it will be escaped too.
682+
// If there is an error, an empty string will be returned.
683+
//
684+
// parseMode is the text formatting mode (ModeMarkdown, ModeMarkdownV2 or ModeHTML)
685+
// text is the input string that will be escaped
686+
func EscapeText(parseMode string, text string) string {
687+
var replacer *strings.Replacer
688+
689+
if parseMode == ModeHTML {
690+
replacer = strings.NewReplacer("<", "&lt;", ">", "&gt;", "&", "&amp;")
691+
} else if parseMode == ModeMarkdown {
692+
replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[")
693+
} else if parseMode == ModeMarkdownV2 {
694+
replacer = strings.NewReplacer(
695+
"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(",
696+
"\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>",
697+
"#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|",
698+
"\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!",
699+
)
700+
} else {
701+
return ""
702+
}
703+
704+
return replacer.Replace(text)
705+
}

bot_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,11 @@ func TestCommands(t *testing.T) {
10021002
}
10031003

10041004
// TODO: figure out why test is failing
1005+
//
10051006
// func TestEditMessageMedia(t *testing.T) {
10061007
// bot, _ := getBot(t)
10071008

1008-
// msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
1009+
// msg := NewPhoto(ChatID, "tests/image.jpg")
10091010
// msg.Caption = "Test"
10101011
// m, err := bot.Send(msg)
10111012

0 commit comments

Comments
 (0)