From 67c024dc85c4987ea1806c98b8160cab15b1c808 Mon Sep 17 00:00:00 2001 From: Sergey Ryazanov Date: Tue, 17 Sep 2019 18:24:10 +0300 Subject: [PATCH 1/3] update bot struct --- bot.go | 8 ++++---- configs.go | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/bot.go b/bot.go index a996790..423a212 100644 --- a/bot.go +++ b/bot.go @@ -35,22 +35,22 @@ type BotAPI struct { // NewBotAPI creates a new BotAPI instance. // // It requires a token, provided by @BotFather on Telegram. -func NewBotAPI(token string) (*BotAPI, error) { - return NewBotAPIWithClient(token, &http.Client{}) +func NewBotAPI(token string, api string) (*BotAPI, error) { + return NewBotAPIWithClient(token, api, &http.Client{}) } // NewBotAPIWithClient creates a new BotAPI instance // and allows you to pass a http.Client. // // It requires a token, provided by @BotFather on Telegram. -func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { +func NewBotAPIWithClient(token string, api string, client *http.Client) (*BotAPI, error) { bot := &BotAPI{ Token: token, Client: client, Buffer: 100, shutdownChannel: make(chan interface{}), - apiEndpoint: APIEndpoint, + apiEndpoint: api, } self, err := bot.GetMe() diff --git a/configs.go b/configs.go index 181d4e4..d5b2706 100644 --- a/configs.go +++ b/configs.go @@ -11,7 +11,6 @@ import ( const ( // APIEndpoint is the endpoint for all API methods, // with formatting for Sprintf. - APIEndpoint = "https://api.telegram.org/bot%s/%s" // FileEndpoint is the endpoint for downloading a file from Telegram. FileEndpoint = "https://api.telegram.org/file/bot%s/%s" ) From 07366fa0b9303dfc2d3124d040c7c056f42f9f21 Mon Sep 17 00:00:00 2001 From: Sergey Ryazanov Date: Thu, 26 Sep 2019 17:47:44 +0300 Subject: [PATCH 2/3] change bot endpoint --- bot.go | 55 +++++++++++++++++++++++++++++++++++++++--------------- configs.go | 2 -- types.go | 1 + 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/bot.go b/bot.go index 423a212..225ec85 100644 --- a/bot.go +++ b/bot.go @@ -6,7 +6,6 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io" "io/ioutil" "net/http" @@ -32,25 +31,40 @@ type BotAPI struct { apiEndpoint string } +//const ( +// // APIEndpoint is the endpoint for all API methods, +// // with formatting for Sprintf. +// // FileEndpoint is the endpoint for downloading a file from Telegram. +// FileEndpoint = "https://api.telegram.org/file/bot%s/%s" +//) + // NewBotAPI creates a new BotAPI instance. // // It requires a token, provided by @BotFather on Telegram. -func NewBotAPI(token string, api string) (*BotAPI, error) { - return NewBotAPIWithClient(token, api, &http.Client{}) +func NewBotAPI(token string) (*BotAPI, error) { + return NewBotAPIWith(token, "https://api.telegram.org", &http.Client{}) +} + +// NewBotAPIWith creates a new BotAPI instance +// with custom Telegram Endpoint +// +// It requires a token, provided by @BotFather on Telegram. +func NewBotAPIWithEndpoint(token string, apiEndpoint string) (*BotAPI, error) { + return NewBotAPIWith(token, apiEndpoint, &http.Client{}) } -// NewBotAPIWithClient creates a new BotAPI instance -// and allows you to pass a http.Client. +// NewBotAPIWith creates a new BotAPI instance +// with custom http.Client and Telegram Endpoint // // It requires a token, provided by @BotFather on Telegram. -func NewBotAPIWithClient(token string, api string, client *http.Client) (*BotAPI, error) { +func NewBotAPIWith(token string, apiEndpoint string, client *http.Client) (*BotAPI, error) { bot := &BotAPI{ Token: token, Client: client, Buffer: 100, shutdownChannel: make(chan interface{}), - apiEndpoint: api, + apiEndpoint: apiEndpoint, } self, err := bot.GetMe() @@ -67,11 +81,24 @@ func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) { b.apiEndpoint = apiEndpoint } + +// Link returns a full path to send Bot request. +// +// It requires the Bot method string. +func (bot *BotAPI) buildUrl(method string) string { + var b strings.Builder + b.WriteString(bot.apiEndpoint) + b.WriteString("/bot") + b.WriteString(bot.Token) + b.WriteString("/") + b.WriteString(method) + return b.String() +} + // MakeRequest makes a request to a specific endpoint with our token. -func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { - method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint) +func (bot *BotAPI) MakeRequest(method string, params url.Values) (APIResponse, error) { - resp, err := bot.Client.PostForm(method, params) + resp, err := bot.Client.PostForm(bot.buildUrl(method), params) if err != nil { return APIResponse{}, err } @@ -84,7 +111,7 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, } if bot.Debug { - log.Printf("%s resp: %s", endpoint, bytes) + log.Printf("%s resp: %s", method, bytes) } if !apiResp.Ok { @@ -145,7 +172,7 @@ func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Messa // // Note that if your FileReader has a size set to -1, it will read // the file into memory to calculate a size. -func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) { +func (bot *BotAPI) UploadFile(method string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) { ms := multipartstreamer.New() switch f := file.(type) { @@ -194,9 +221,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna return APIResponse{}, errors.New(ErrBadFileType) } - method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint) - - req, err := http.NewRequest("POST", method, nil) + req, err := http.NewRequest("POST", bot.buildUrl(method), nil) if err != nil { return APIResponse{}, err } diff --git a/configs.go b/configs.go index d5b2706..fff045d 100644 --- a/configs.go +++ b/configs.go @@ -9,8 +9,6 @@ import ( // Telegram constants const ( - // APIEndpoint is the endpoint for all API methods, - // with formatting for Sprintf. // FileEndpoint is the endpoint for downloading a file from Telegram. FileEndpoint = "https://api.telegram.org/file/bot%s/%s" ) diff --git a/types.go b/types.go index 52cb36c..75f22b5 100644 --- a/types.go +++ b/types.go @@ -431,6 +431,7 @@ func (f *File) Link(token string) string { return fmt.Sprintf(FileEndpoint, token, f.FilePath) } +//https://api.telegram.org/file/bot%s/%s" // ReplyKeyboardMarkup allows the Bot to set a custom keyboard. type ReplyKeyboardMarkup struct { Keyboard [][]KeyboardButton `json:"keyboard"` From e68add58f0149cd9e18286a397636c38a1429c40 Mon Sep 17 00:00:00 2001 From: Sergey Ryazanov Date: Fri, 27 Sep 2019 13:46:02 +0300 Subject: [PATCH 3/3] cleanUp for merge --- bot.go | 9 +-------- types.go | 1 - 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/bot.go b/bot.go index 225ec85..9ef22b5 100644 --- a/bot.go +++ b/bot.go @@ -31,13 +31,6 @@ type BotAPI struct { apiEndpoint string } -//const ( -// // APIEndpoint is the endpoint for all API methods, -// // with formatting for Sprintf. -// // FileEndpoint is the endpoint for downloading a file from Telegram. -// FileEndpoint = "https://api.telegram.org/file/bot%s/%s" -//) - // NewBotAPI creates a new BotAPI instance. // // It requires a token, provided by @BotFather on Telegram. @@ -82,7 +75,7 @@ func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) { } -// Link returns a full path to send Bot request. +// buildUrl returns a full path to send Bot request. // // It requires the Bot method string. func (bot *BotAPI) buildUrl(method string) string { diff --git a/types.go b/types.go index 75f22b5..52cb36c 100644 --- a/types.go +++ b/types.go @@ -431,7 +431,6 @@ func (f *File) Link(token string) string { return fmt.Sprintf(FileEndpoint, token, f.FilePath) } -//https://api.telegram.org/file/bot%s/%s" // ReplyKeyboardMarkup allows the Bot to set a custom keyboard. type ReplyKeyboardMarkup struct { Keyboard [][]KeyboardButton `json:"keyboard"`