add option to pass custom http.Client to tb.NewBot() in case of proxying and other needs

pull/132/head
sigurniv 6 years ago
parent 2a7e9045d0
commit 5f938b2a7a

@ -26,7 +26,7 @@ func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
return []byte{}, wrapSystem(err)
}
resp, err := http.Post(url, "application/json", &buf)
resp, err := b.client.Post(url, "application/json", &buf)
if err != nil {
return []byte{}, errors.Wrap(err, "http.Post failed")
}
@ -85,7 +85,7 @@ func (b *Bot) sendFiles(
req.Header.Add("Content-Type", writer.FormDataContentType())
resp, err := http.DefaultClient.Do(req)
resp, err := b.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "http.Post failed")
}

@ -20,6 +20,11 @@ func NewBot(pref Settings) (*Bot, error) {
pref.Updates = 100
}
client := pref.Client
if client == nil {
client = http.DefaultClient
}
bot := &Bot{
Token: pref.Token,
Updates: make(chan Update, pref.Updates),
@ -28,6 +33,7 @@ func NewBot(pref Settings) (*Bot, error) {
handlers: make(map[string]interface{}),
stop: make(chan struct{}),
reporter: pref.Reporter,
client:client,
}
user, err := bot.getMe()
@ -49,6 +55,7 @@ type Bot struct {
handlers map[string]interface{}
reporter func(error)
stop chan struct{}
client *http.Client
}
// Settings represents a utility struct for passing certain
@ -66,6 +73,9 @@ type Settings struct {
// Reporter is a callback function that will get called
// on any panics recovered from endpoint handlers.
Reporter func(error)
// HTTP Client used to make requests to telegram api
Client *http.Client
}
// Update object represents an incoming update.

@ -26,8 +26,21 @@ func TestBot(t *testing.T) {
}
}
func TestRecipient(_ *testing.T) {
bot := Bot{}
func TestRecipient(t *testing.T) {
token := os.Getenv("TELEBOT_SECRET")
if token == "" {
fmt.Println("ERROR: " +
"In order to test telebot functionality, you need to set up " +
"TELEBOT_SECRET environmental variable, which represents an API " +
"key to a Telegram bot.\n")
t.Fatal("Could't find TELEBOT_SECRET, aborting.")
}
bot, err := NewBot(Settings{Token: token})
if err != nil {
t.Fatal("couldn't create bot:", err)
}
bot.Send(&User{}, "")
bot.Send(&Chat{}, "")
}

Loading…
Cancel
Save