Ability to pass a client to the bot
Changed every instance of a DefaultClient to a Client stored in the bot.bot-api-6.1
parent
60337023c5
commit
612584dbd3
10
bot.go
10
bot.go
|
@ -1,19 +1,27 @@
|
||||||
// Package tgbotapi has bindings for interacting with the Telegram Bot API.
|
// Package tgbotapi has bindings for interacting with the Telegram Bot API.
|
||||||
package tgbotapi
|
package tgbotapi
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
// BotAPI has methods for interacting with all of Telegram's Bot API endpoints.
|
// BotAPI has methods for interacting with all of Telegram's Bot API endpoints.
|
||||||
type BotAPI struct {
|
type BotAPI struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Debug bool `json:"debug"`
|
Debug bool `json:"debug"`
|
||||||
Self User `json:"-"`
|
Self User `json:"-"`
|
||||||
Updates chan Update `json:"-"`
|
Updates chan Update `json:"-"`
|
||||||
|
Client http.Client `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBotAPI creates a new BotAPI instance.
|
// NewBotAPI creates a new BotAPI instance.
|
||||||
// Requires a token, provided by @BotFather on Telegram
|
// 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{
|
bot := &BotAPI{
|
||||||
Token: token,
|
Token: token,
|
||||||
|
Client: client,
|
||||||
}
|
}
|
||||||
|
|
||||||
self, err := bot.GetMe()
|
self, err := bot.GetMe()
|
||||||
|
|
|
@ -131,7 +131,7 @@ type WebhookConfig struct {
|
||||||
// MakeRequest makes a request to a specific endpoint with our token.
|
// MakeRequest makes a request to a specific endpoint with our token.
|
||||||
// All requests are POSTs because Telegram doesn't care, and it's easier.
|
// All requests are POSTs because Telegram doesn't care, and it's easier.
|
||||||
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
|
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 {
|
if err != nil {
|
||||||
return APIResponse{}, err
|
return APIResponse{}, err
|
||||||
} else {
|
} else {
|
||||||
|
@ -190,15 +190,14 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
|
||||||
|
|
||||||
w.Close()
|
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 {
|
if err != nil {
|
||||||
return APIResponse{}, err
|
return APIResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
req.Header.Set("Content-Type", w.FormDataContentType())
|
||||||
|
|
||||||
client := &http.Client{}
|
res, err := bot.Client.Do(req)
|
||||||
res, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return APIResponse{}, err
|
return APIResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue