From cb85dc91269b4abae714ba0023dade069e7f6380 Mon Sep 17 00:00:00 2001 From: Maksim Kutin Date: Wed, 11 Jul 2018 08:03:07 +0300 Subject: [PATCH] removed test & added ability to connect without credentials --- bot.go | 9 ++++++++- bot_test.go | 17 +---------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/bot.go b/bot.go index 15247ea..ddd1dcb 100644 --- a/bot.go +++ b/bot.go @@ -67,7 +67,14 @@ func NewBotAPIViaProxy(token string, proxySettings ProxyCredentials) (*BotAPI, e 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)) + 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)} diff --git a/bot_test.go b/bot_test.go index 7b4678a..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 ( @@ -23,12 +23,6 @@ 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) { @@ -43,15 +37,6 @@ 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("")