From 9013abc73d0f2a7e944deb84ccfce9c1ef736289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D0=B0=D1=81=D0=B8=D1=85=D0=BE=D0=B2=20=D0=98=D1=80?= =?UTF-8?q?=D0=B5=D0=BA=20=D0=9D=D1=83=D1=80=D0=B3=D0=B0=D1=8F=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Wed, 18 Apr 2018 10:32:48 +0300 Subject: [PATCH] added support for socks5 --- bot.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/bot.go b/bot.go index 8fb6200..da02a10 100644 --- a/bot.go +++ b/bot.go @@ -18,6 +18,7 @@ import ( "time" "github.com/technoweenie/multipartstreamer" + "golang.org/x/net/proxy" ) // BotAPI allows you to interact with the Telegram Bot API. @@ -34,7 +35,24 @@ type BotAPI struct { // // It requires a token, provided by @BotFather on Telegram. func NewBotAPI(token string) (*BotAPI, error) { - return NewBotAPIWithClient(token, &http.Client{}) + socks5 := os.Getenv("SOCKS5_PROXY") + client := &http.Client{} + if len(socks5) > 0 { + tgProxyURL, err := url.Parse(socks5) + if err != nil { + log.Printf("Failed to parse proxy URL:%s\n", err) + return nil, err + } + tgDialer, err := proxy.FromURL(tgProxyURL, proxy.Direct) + if err != nil { + log.Printf("Failed to obtain proxy dialer: %s\n", err) + } + tgTransport := &http.Transport{ + Dial: tgDialer.Dial, + } + client.Transport = tgTransport + } + return NewBotAPIWithClient(token, client) } // NewBotAPIWithClient creates a new BotAPI instance