diff --git a/bot.go b/bot.go index 03ca9f5..89a456a 100644 --- a/bot.go +++ b/bot.go @@ -1,19 +1,29 @@ // 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:"-"` + 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) { + return NewBotAPIwithClient(token, &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) { bot := &BotAPI{ - Token: token, + Token: token, + Client: client, } self, err := bot.GetMe() diff --git a/methods.go b/methods.go index dbd19d8..a1f6e2e 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 { @@ -197,8 +197,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna 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 }