Merge branch 'develop' into files

bot-api-6.1
Syfaro 2021-08-20 16:11:25 -04:00
commit 6aa05225a6
2 changed files with 40 additions and 6 deletions

43
bot.go
View File

@ -18,7 +18,6 @@ import (
// HTTPClient is the type needed for the bot to perform HTTP requests. // HTTPClient is the type needed for the bot to perform HTTP requests.
type HTTPClient interface { type HTTPClient interface {
Do(req *http.Request) (*http.Response, error) 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. // BotAPI allows you to interact with the Telegram Bot API.
@ -78,18 +77,18 @@ func (bot *BotAPI) SetAPIEndpoint(apiEndpoint string) {
bot.apiEndpoint = apiEndpoint bot.apiEndpoint = apiEndpoint
} }
func buildParams(in Params) (out url.Values) { func buildParams(in Params) url.Values {
if in == nil { if in == nil {
return url.Values{} return url.Values{}
} }
out = url.Values{} out := url.Values{}
for key, value := range in { for key, value := range in {
out.Set(key, value) out.Set(key, value)
} }
return return out
} }
// MakeRequest makes a request to a specific endpoint with our token. // 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
values := buildParams(params) 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 { if err != nil {
return nil, err return nil, err
} }
@ -670,3 +675,31 @@ func (bot *BotAPI) CopyMessage(config CopyMessageConfig) (MessageID, error) {
return messageID, err 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)
}

View File

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