diff --git a/bot.go b/bot.go index 35d1e25..09f3bec 100644 --- a/bot.go +++ b/bot.go @@ -27,6 +27,7 @@ type BotAPI struct { Self User `json:"-"` Client *http.Client `json:"-"` + ProxySettings ProxyCredentials `json:"-"` } // NewBotAPI creates a new BotAPI instance. @@ -57,6 +58,33 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { return bot, nil } +// NewBotAPIViaProxy creates a new BotAPI instance and allows you +// exchange data with APIEndpoint via proxy-server. +// +// It requires a token and proxy credentials. +func NewBotAPIViaProxy(token string, proxySettings ProxyCredentials) (*BotAPI, error) { + client := &http.Client{} + + if proxySettings.UseProxy { + var fixedURL *url.URL + var err error + + if proxySettings.Username != "" && proxySettings.Password != "" { + fixedURL, err = url.Parse(fmt.Sprintf("%s://%s:%s@%s:%s", proxySettings.Protocol, proxySettings.Username, proxySettings.Password, proxySettings.IP, proxySettings.Port)) + } else { + fixedURL, err = url.Parse(fmt.Sprintf("%s://%s:%s", proxySettings.Protocol, proxySettings.IP, proxySettings.Port)) + } + + if err == nil { + tr := &http.Transport{Proxy: http.ProxyURL(fixedURL)} + + client.Transport = tr + } + } + + return NewBotAPIWithClient(token, client) +} + // MakeRequest makes a request to a specific endpoint with our token. func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) diff --git a/bot_test.go b/bot_test.go index e7aa7ac..9bfb4c7 100644 --- a/bot_test.go +++ b/bot_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/mkutin/telegram-bot-api" ) const ( diff --git a/helpers.go b/helpers.go index f04a0a7..fe8c96c 100644 --- a/helpers.go +++ b/helpers.go @@ -2,6 +2,7 @@ package tgbotapi import ( "net/url" + "strings" ) // NewMessage creates a new Message. @@ -690,3 +691,32 @@ func NewSetChatPhotoShare(chatID int64, fileID string) SetChatPhotoConfig { }, } } + +// NewProxyCredentials create a new proxy credentials. +// You may use this to create new credentials for exchange data +// via proxy-server. +// +// useProxy tells you to use a proxy, protocol is one of the allowed protocols, +// username and password are credentials for authentication user on proxy-server, +// ip and port are parameters for connection to proxy-server. +func NewProxyCredentials(useProxy bool, protocol, username, password, ip, port string) ProxyCredentials { + switch strings.ToLower(protocol) { + case "socks5": + protocol = "socks5" + case "http": + protocol = "http" + case "https": + protocol = "https" + default: + useProxy = false + } + + return ProxyCredentials{ + UseProxy: useProxy, + Protocol: protocol, + Username: username, + Password: password, + IP: ip, + Port: port, + } +} diff --git a/types.go b/types.go index 0843ab9..32417cc 100644 --- a/types.go +++ b/types.go @@ -773,6 +773,17 @@ type PreCheckoutQuery struct { OrderInfo *OrderInfo `json:"order_info,omitempty"` } +// ProxyCredentials are credentials parameters for exchange data with +// APIEndpoint via proxy-server. +type ProxyCredentials struct { + UseProxy bool + Protocol string + Username string + Password string + IP string + Port string +} + // Error is an error containing extra information returned by the Telegram API. type Error struct { Message string