From e2bf12bcda95d3ee40993046c78c1c5b51a4d5b4 Mon Sep 17 00:00:00 2001 From: Vinicius Tinti Date: Wed, 14 Nov 2018 08:26:39 -0200 Subject: [PATCH] Add BotAPIConfig object BotAPIConfig allows the user to configure custom http.Client, http.Server and http.ServerMux in a single object. Future configurations may be added to this. This deprecates NewBotAPIWithClient() in favor of NewBotAPIWithConfig(). Signed-off-by: Vinicius Tinti --- bot.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/bot.go b/bot.go index 7766346..bf62cef 100644 --- a/bot.go +++ b/bot.go @@ -18,14 +18,23 @@ import ( "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. type BotAPI struct { Token string `json:"token"` Debug bool `json:"debug"` Buffer int `json:"buffer"` - Self User `json:"-"` - Client *http.Client `json:"-"` + Self User `json:"-"` + Config *BotAPIConfig `json:"-"` shutdownChannel chan interface{} } @@ -33,18 +42,19 @@ type BotAPI struct { // // It requires a token, provided by @BotFather on Telegram. 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 -// and allows you to pass a http.Client. +// and allows you to pass BotAPIConfig. // // 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{ Token: token, - Client: client, Buffer: 100, + Config: config, shutdownChannel: make(chan interface{}), } @@ -82,7 +92,7 @@ func (bot *BotAPI) MakeRequest(endpoint string, params Params) (APIResponse, err values := buildParams(params) - resp, err := bot.Client.PostForm(method, values) + resp, err := bot.Config.Client.PostForm(method, values) if err != nil { return APIResponse{}, err } @@ -208,7 +218,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params Params, fieldname string, ms.SetupRequest(req) - res, err := bot.Client.Do(req) + res, err := bot.Config.Client.Do(req) if err != nil { return APIResponse{}, err } @@ -422,7 +432,12 @@ func (bot *BotAPI) StopReceivingUpdates() { func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel { 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) var update Update