pull/186/merge
Maksim Kutin 7 years ago committed by GitHub
commit a741167957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      bot.go
  2. 2
      bot_test.go
  3. 30
      helpers.go
  4. 11
      types.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)

@ -8,7 +8,7 @@ import (
"testing"
"time"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/mkutin/telegram-bot-api"
)
const (

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

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