From 612584dbd3293113d88ed1212d8c8dd87a9b344a Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Mon, 27 Jul 2015 00:16:45 +0200 Subject: [PATCH 1/5] Ability to pass a client to the bot Changed every instance of a DefaultClient to a Client stored in the bot. --- bot.go | 12 ++++++++++-- methods.go | 7 +++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/bot.go b/bot.go index 03ca9f5..6c2bfb4 100644 --- a/bot.go +++ b/bot.go @@ -1,19 +1,27 @@ // Package tgbotapi has bindings for interacting with the Telegram Bot API. package tgbotapi +import "net/http" + // BotAPI has methods for interacting with all of Telegram's Bot API endpoints. type BotAPI struct { Token string `json:"token"` Debug bool `json:"debug"` Self User `json:"-"` Updates chan Update `json:"-"` + Client http.Client `json:"-"` } // NewBotAPI creates a new BotAPI instance. // Requires a token, provided by @BotFather on Telegram -func NewBotAPI(token string) (*BotAPI, error) { +func NewBotAPI(token string, client http.Client) (*BotAPI, error) { + if client == nil { + client = &http.Client{} + } + bot := &BotAPI{ - Token: token, + Token: token, + Client: client, } self, err := bot.GetMe() diff --git a/methods.go b/methods.go index dbd19d8..a3f6ccc 100644 --- a/methods.go +++ b/methods.go @@ -131,7 +131,7 @@ type WebhookConfig struct { // MakeRequest makes a request to a specific endpoint with our token. // All requests are POSTs because Telegram doesn't care, and it's easier. func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { - resp, err := http.PostForm("https://api.telegram.org/bot"+bot.Token+"/"+endpoint, params) + resp, err := bot.Client.PostForm("https://api.telegram.org/bot"+bot.Token+"/"+endpoint, params) if err != nil { return APIResponse{}, err } else { @@ -190,15 +190,14 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna w.Close() - req, err := http.NewRequest("POST", "https://api.telegram.org/bot"+bot.Token+"/"+endpoint, &b) + req, err := bot.Client.NewRequest("POST", "https://api.telegram.org/bot"+bot.Token+"/"+endpoint, &b) if err != nil { return APIResponse{}, err } req.Header.Set("Content-Type", w.FormDataContentType()) - client := &http.Client{} - res, err := client.Do(req) + res, err := bot.Client.Do(req) if err != nil { return APIResponse{}, err } From 8249dd6a932861ac1213dc4bcf4b6fc96104aa53 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Mon, 27 Jul 2015 00:22:10 +0200 Subject: [PATCH 2/5] Few bug fixes --- bot.go | 4 ++-- methods.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bot.go b/bot.go index 6c2bfb4..e0fc732 100644 --- a/bot.go +++ b/bot.go @@ -14,14 +14,14 @@ type BotAPI struct { // NewBotAPI creates a new BotAPI instance. // Requires a token, provided by @BotFather on Telegram -func NewBotAPI(token string, client http.Client) (*BotAPI, error) { +func NewBotAPI(token string, client *http.Client) (*BotAPI, error) { if client == nil { client = &http.Client{} } bot := &BotAPI{ Token: token, - Client: client, + Client: *client, } self, err := bot.GetMe() diff --git a/methods.go b/methods.go index a3f6ccc..a1f6e2e 100644 --- a/methods.go +++ b/methods.go @@ -190,7 +190,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna w.Close() - req, err := bot.Client.NewRequest("POST", "https://api.telegram.org/bot"+bot.Token+"/"+endpoint, &b) + req, err := http.NewRequest("POST", "https://api.telegram.org/bot"+bot.Token+"/"+endpoint, &b) if err != nil { return APIResponse{}, err } From fc8cb6e0398c8801c799ebe0a09d26e87ae6f6ce Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Tue, 28 Jul 2015 11:18:49 +0200 Subject: [PATCH 3/5] Client is now a pointer --- bot.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bot.go b/bot.go index e0fc732..3d8bf94 100644 --- a/bot.go +++ b/bot.go @@ -5,11 +5,11 @@ import "net/http" // BotAPI has methods for interacting with all of Telegram's Bot API endpoints. type BotAPI struct { - Token string `json:"token"` - Debug bool `json:"debug"` - Self User `json:"-"` - Updates chan Update `json:"-"` - Client http.Client `json:"-"` + Token string `json:"token"` + Debug bool `json:"debug"` + Self User `json:"-"` + Updates chan Update `json:"-"` + Client *http.Client `json:"-"` } // NewBotAPI creates a new BotAPI instance. @@ -21,7 +21,7 @@ func NewBotAPI(token string, client *http.Client) (*BotAPI, error) { bot := &BotAPI{ Token: token, - Client: *client, + Client: client, } self, err := bot.GetMe() From 2483f043976a025de9c97f53dfcaa4894c090b8b Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Tue, 28 Jul 2015 13:00:31 +0200 Subject: [PATCH 4/5] Separate NewBotAPI from NewBotAPIwithClient --- bot.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bot.go b/bot.go index 3d8bf94..74c867b 100644 --- a/bot.go +++ b/bot.go @@ -14,7 +14,13 @@ type BotAPI struct { // NewBotAPI creates a new BotAPI instance. // Requires a token, provided by @BotFather on Telegram -func NewBotAPI(token string, client *http.Client) (*BotAPI, error) { +func NewBotAPI(token string) (*BotAPI, error) { + return NewBotAPIwithClient(token, nil) +} + +// NewBotAPI creates a new BotAPI instance passing an http.Client. +// Requires a token, provided by @BotFather on Telegram +func NewBotAPIwithClient(token string, client *http.Client) (*BotAPI, error) { if client == nil { client = &http.Client{} } From 3101266b614ca587b4081c0d83a4832807e80390 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Tue, 28 Jul 2015 18:14:13 +0200 Subject: [PATCH 5/5] Fixed NewBotAPIWithClient --- bot.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/bot.go b/bot.go index 74c867b..89a456a 100644 --- a/bot.go +++ b/bot.go @@ -15,16 +15,12 @@ type BotAPI struct { // NewBotAPI creates a new BotAPI instance. // Requires a token, provided by @BotFather on Telegram func NewBotAPI(token string) (*BotAPI, error) { - return NewBotAPIwithClient(token, nil) + return NewBotAPIwithClient(token, &http.Client{}) } -// NewBotAPI creates a new BotAPI instance passing an http.Client. +// NewBotAPIWithClient creates a new BotAPI instance passing an http.Client. // Requires a token, provided by @BotFather on Telegram -func NewBotAPIwithClient(token string, client *http.Client) (*BotAPI, error) { - if client == nil { - client = &http.Client{} - } - +func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { bot := &BotAPI{ Token: token, Client: client,