pull/206/merge
Vinicius Tinti 6 years ago committed by GitHub
commit fc262827d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      bot.go

@ -18,6 +18,15 @@ import (
"github.com/technoweenie/multipartstreamer" "github.com/technoweenie/multipartstreamer"
) )
// BotAPI configuration object to allow custom setups.
//
// BotAPIConfig allows to set http.Client and http.Server
type BotAPIConfig struct {
Client *http.Client
Server *http.Server
ServeMux *http.ServeMux
}
// BotAPI allows you to interact with the Telegram Bot API. // BotAPI allows you to interact with the Telegram Bot API.
type BotAPI struct { type BotAPI struct {
Token string `json:"token"` Token string `json:"token"`
@ -25,7 +34,7 @@ type BotAPI struct {
Buffer int `json:"buffer"` Buffer int `json:"buffer"`
Self User `json:"-"` Self User `json:"-"`
Client *http.Client `json:"-"` Config *BotAPIConfig `json:"-"`
shutdownChannel chan interface{} shutdownChannel chan interface{}
} }
@ -33,18 +42,19 @@ type BotAPI struct {
// //
// It requires a token, provided by @BotFather on Telegram. // It requires a token, provided by @BotFather on Telegram.
func NewBotAPI(token string) (*BotAPI, error) { func NewBotAPI(token string) (*BotAPI, error) {
return NewBotAPIWithClient(token, &http.Client{}) return NewBotAPIWithConfig(token, &BotAPIConfig{http.DefaultClient,
nil, http.DefaultServeMux})
} }
// NewBotAPIWithClient creates a new BotAPI instance // NewBotAPIWithClient creates a new BotAPI instance
// and allows you to pass a http.Client. // and allows you to pass BotAPIConfig.
// //
// It requires a token, provided by @BotFather on Telegram. // It requires a token, provided by @BotFather on Telegram.
func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { func NewBotAPIWithConfig(token string, config *BotAPIConfig) (*BotAPI, error) {
bot := &BotAPI{ bot := &BotAPI{
Token: token, Token: token,
Client: client,
Buffer: 100, Buffer: 100,
Config: config,
shutdownChannel: make(chan interface{}), shutdownChannel: make(chan interface{}),
} }
@ -82,7 +92,7 @@ func (bot *BotAPI) MakeRequest(endpoint string, params Params) (APIResponse, err
values := buildParams(params) values := buildParams(params)
resp, err := bot.Client.PostForm(method, values) resp, err := bot.Config.Client.PostForm(method, values)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
@ -208,7 +218,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params Params, fieldname string,
ms.SetupRequest(req) ms.SetupRequest(req)
res, err := bot.Client.Do(req) res, err := bot.Config.Client.Do(req)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
@ -437,7 +447,12 @@ func (bot *BotAPI) StopReceivingUpdates() {
func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel { func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
ch := make(chan Update, bot.Buffer) ch := make(chan Update, bot.Buffer)
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { handleFunc := http.DefaultServeMux.HandleFunc
if mux := bot.Config.ServeMux; mux != nil {
handleFunc = mux.HandleFunc
}
handleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body) bytes, _ := ioutil.ReadAll(r.Body)
var update Update var update Update