Added support proxy-socks5 protocol. It actual for the Russian Internet users because of the lock the Telegram

pull/173/head
ignusius 7 years ago
parent 212b154115
commit 2b45ce08b5
  1. 30
      bot.go

@ -18,6 +18,7 @@ import (
"time" "time"
"github.com/technoweenie/multipartstreamer" "github.com/technoweenie/multipartstreamer"
"golang.org/x/net/proxy"
) )
// BotAPI allows you to interact with the Telegram Bot API. // BotAPI allows you to interact with the Telegram Bot API.
@ -31,9 +32,36 @@ type BotAPI struct {
} }
// NewBotAPI creates a new BotAPI instance. // NewBotAPI creates a new BotAPI instance.
// 'proxyparams' only support socks5 protocol.
//
// For example: tgbotapi.NewBotAPI("my:token", "socks5", "192.168.1.1:1080", <nil, *proxy.Auth>
// *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. // 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{}) return NewBotAPIWithClient(token, &http.Client{})
} }