diff --git a/.travis.yml b/.travis.yml index 8408fb7..b54e2c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ language: go go: - - 1.4 - - tip \ No newline at end of file + - tip diff --git a/README.md b/README.md index d9a6873..59279c0 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ import ( func main() { bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") + //bot, err := tgbotapi.NewBotAPI("my:token", "socks5", "192.168.1.1:1080", nil) //SOCKS5 Proxy if err != nil { log.Panic(err) } diff --git a/bot.go b/bot.go index 8fb6200..3ee7b13 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. @@ -31,9 +32,36 @@ type BotAPI struct { } // NewBotAPI creates a new BotAPI instance. +// 'proxyparams' only support socks5 protocol. +// +// For example: tgbotapi.NewBotAPI("my:token", "socks5", "192.168.1.1:1080", +// *proxy.Auth format see here: https://github.com/golang/net/blob/master/proxy/socks5.go#L17) // // 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{}) }