@@ -20,7 +20,6 @@ import (
2020// HTTPClient is the type needed for the bot to perform HTTP requests.
2121type HTTPClient interface {
2222 Do (req * http.Request ) (* http.Response , error )
23- PostForm (url string , data url.Values ) (* http.Response , error )
2423}
2524
2625// BotAPI allows you to interact with the Telegram Bot API.
@@ -80,18 +79,18 @@ func (bot *BotAPI) SetAPIEndpoint(apiEndpoint string) {
8079 bot .apiEndpoint = apiEndpoint
8180}
8281
83- func buildParams (in Params ) ( out url.Values ) {
82+ func buildParams (in Params ) url.Values {
8483 if in == nil {
8584 return url.Values {}
8685 }
8786
88- out = url.Values {}
87+ out : = url.Values {}
8988
9089 for key , value := range in {
9190 out .Set (key , value )
9291 }
9392
94- return
93+ return out
9594}
9695
9796// MakeRequest makes a request to a specific endpoint with our token.
@@ -104,7 +103,13 @@ func (bot *BotAPI) MakeRequest(endpoint string, params Params) (*APIResponse, er
104103
105104 values := buildParams (params )
106105
107- resp , err := bot .Client .PostForm (method , values )
106+ req , err := http .NewRequest ("POST" , method , strings .NewReader (values .Encode ()))
107+ if err != nil {
108+ return & APIResponse {}, err
109+ }
110+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
111+
112+ resp , err := bot .Client .Do (req )
108113 if err != nil {
109114 return nil , err
110115 }
@@ -703,3 +708,31 @@ func (bot *BotAPI) CopyMessage(config CopyMessageConfig) (MessageID, error) {
703708
704709 return messageID , err
705710}
711+
712+ // EscapeText takes an input text and escape Telegram markup symbols.
713+ // In this way we can send a text without being afraid of having to escape the characters manually.
714+ // Note that you don't have to include the formatting style in the input text, or it will be escaped too.
715+ // If there is an error, an empty string will be returned.
716+ //
717+ // parseMode is the text formatting mode (ModeMarkdown, ModeMarkdownV2 or ModeHTML)
718+ // text is the input string that will be escaped
719+ func EscapeText (parseMode string , text string ) string {
720+ var replacer * strings.Replacer
721+
722+ if parseMode == ModeHTML {
723+ replacer = strings .NewReplacer ("<" , "<" , ">" , ">" , "&" , "&" )
724+ } else if parseMode == ModeMarkdown {
725+ replacer = strings .NewReplacer ("_" , "\\ _" , "*" , "\\ *" , "`" , "\\ `" , "[" , "\\ [" )
726+ } else if parseMode == ModeMarkdownV2 {
727+ replacer = strings .NewReplacer (
728+ "_" , "\\ _" , "*" , "\\ *" , "[" , "\\ [" , "]" , "\\ ]" , "(" ,
729+ "\\ (" , ")" , "\\ )" , "~" , "\\ ~" , "`" , "\\ `" , ">" , "\\ >" ,
730+ "#" , "\\ #" , "+" , "\\ +" , "-" , "\\ -" , "=" , "\\ =" , "|" ,
731+ "\\ |" , "{" , "\\ {" , "}" , "\\ }" , "." , "\\ ." , "!" , "\\ !" ,
732+ )
733+ } else {
734+ return ""
735+ }
736+
737+ return replacer .Replace (text )
738+ }
0 commit comments