From 8d1bf90804dae97062252621be837b5fcc319e46 Mon Sep 17 00:00:00 2001 From: idcooldi Date: Thu, 30 Aug 2018 14:53:52 +0000 Subject: [PATCH] Added support proxy Added support proxy for Russian location --- bot.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/bot.go b/bot.go index 35d1e25..f683f91 100644 --- a/bot.go +++ b/bot.go @@ -15,7 +15,8 @@ import ( "strconv" "strings" "time" - + + "golang.org/x/net/proxy" "github.com/technoweenie/multipartstreamer" ) @@ -32,10 +33,31 @@ type BotAPI struct { // NewBotAPI creates a new BotAPI instance. // // It requires a token, provided by @BotFather on Telegram. -func NewBotAPI(token string) (*BotAPI, error) { +func NewBotAPI(token string, proxyparam ...interface{}) (*BotAPI, error) { + // create a socks5 dialer + if len(proxyparam) > 0 { + var auth *proxy.Auth + if proxyparam[0] == "socks5" { + if proxyparam[2] == nil { + auth = nil + } else { + auth = proxyparam[2].(*proxy.Auth) + } + dialer, err := proxy.SOCKS5("tcp", proxyparam[1].(string), auth, proxy.Direct) + if err != nil { + return nil, err + } + // setup a http client + httpTransport := &http.Transport{} + // set our socks5 as the dialer + httpTransport.Dial = dialer.Dial + return NewBotAPIWithClient(token, &http.Client{Transport: httpTransport}) + } + } return NewBotAPIWithClient(token, &http.Client{}) } + // NewBotAPIWithClient creates a new BotAPI instance // and allows you to pass a http.Client. //