@@ -18,7 +18,6 @@ import (
1818// HTTPClient is the type needed for the bot to perform HTTP requests.
1919type 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 ("<" , "<" , ">" , ">" , "&" , "&" )
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+ }
0 commit comments