pull/173/merge
Komarov Alexander 7 years ago committed by GitHub
commit bd5bc5a8bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .travis.yml
  2. 1
      README.md
  3. 30
      bot.go

@ -1,5 +1,4 @@
language: go
go:
- 1.4
- tip
- tip

@ -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)
}

@ -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", <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.
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{})
}