added proxy credentials for easy way usefull exchange data with APIEndpoint via proxy

pull/186/head
Maksim Kutin 7 years ago
parent 4c16a90966
commit ddd5ca4cba
  1. 21
      bot.go
  2. 17
      bot_test.go
  3. 30
      helpers.go
  4. 11
      types.go

@ -28,6 +28,7 @@ type BotAPI struct {
Self User `json:"-"`
Client *http.Client `json:"-"`
ProxySettings ProxyCredentials `json:"-"`
}
// NewBotAPI creates a new BotAPI instance.
@ -58,6 +59,26 @@ 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 {
fixedURL, err := url.Parse(fmt.Sprintf("%s://%s:%s@%s:%s", proxySettings.Protocol, proxySettings.Username, proxySettings.Password, 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 (
@ -23,6 +23,12 @@ const (
ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg"
ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
UseProxy = true
ProxyProtocol = "SOCKS5"
ProxyUsername = "YGko8h"
ProxyPassword = "VofoA7"
ProxyIP = "185.179.113.223"
ProxyPort = "8000"
)
func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
@ -37,6 +43,15 @@ func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
return bot, err
}
func TestNewBotAPIViaProxy(t *testing.T) {
_, err := tgbotapi.NewBotAPIViaProxy(TestToken, tgbotapi.NewProxyCredentials(UseProxy, ProxyProtocol, ProxyUsername, ProxyPassword, ProxyIP, ProxyPort))
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestNewBotAPI_notoken(t *testing.T) {
_, err := tgbotapi.NewBotAPI("")

@ -3,6 +3,7 @@ package tgbotapi
import (
"log"
"net/url"
"strings"
)
// NewMessage creates a new Message.
@ -691,3 +692,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