From 66f5a84659fcd334b7dcaaec64c34758e9fc3690 Mon Sep 17 00:00:00 2001 From: Nikita Chisnikov Date: Fri, 6 Sep 2019 09:23:38 +0600 Subject: [PATCH] Add custom init func and files endpoint support --- bot.go | 37 ++++++++++++++++++++++++++++++++++--- types.go | 4 ++-- types_test.go | 4 ++-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/bot.go b/bot.go index a996790..e966f1f 100644 --- a/bot.go +++ b/bot.go @@ -29,7 +29,8 @@ type BotAPI struct { Client *http.Client `json:"-"` shutdownChannel chan interface{} - apiEndpoint string + apiEndpoint string + fileEndpoint string } // NewBotAPI creates a new BotAPI instance. @@ -50,7 +51,33 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { Buffer: 100, shutdownChannel: make(chan interface{}), - apiEndpoint: APIEndpoint, + apiEndpoint: APIEndpoint, + fileEndpoint: FileEndpoint, + } + + self, err := bot.GetMe() + if err != nil { + return nil, err + } + + bot.Self = self + + return bot, nil +} + +// NewBotAPICustom creates a new BotAPI instance +// and allows you to pass apiEndpoint, fileEndpoint and http.Client. +// +// It requires a token, provided by @BotFather on Telegram. +func NewBotAPICustom(token, apiEndpoint, fileEndpoint string, client *http.Client) (*BotAPI, error) { + bot := &BotAPI{ + Token: token, + Client: client, + Buffer: 100, + shutdownChannel: make(chan interface{}), + + apiEndpoint: apiEndpoint, + fileEndpoint: fileEndpoint, } self, err := bot.GetMe() @@ -67,6 +94,10 @@ func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) { b.apiEndpoint = apiEndpoint } +func (b *BotAPI) SetFileEndpoint(fileEndpoint string) { + b.fileEndpoint = fileEndpoint +} + // 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(bot.apiEndpoint, bot.Token, endpoint) @@ -242,7 +273,7 @@ func (bot *BotAPI) GetFileDirectURL(fileID string) (string, error) { return "", err } - return file.Link(bot.Token), nil + return file.Link(bot.Token, bot.fileEndpoint), nil } // GetMe fetches the currently authenticated bot. diff --git a/types.go b/types.go index 52cb36c..3539789 100644 --- a/types.go +++ b/types.go @@ -427,8 +427,8 @@ type File struct { // Link returns a full path to the download URL for a File. // // It requires the Bot Token to create the link. -func (f *File) Link(token string) string { - return fmt.Sprintf(FileEndpoint, token, f.FilePath) +func (f *File) Link(token, fileEndpoint string) string { + return fmt.Sprintf(fileEndpoint, token, f.FilePath) } // ReplyKeyboardMarkup allows the Bot to set a custom keyboard. diff --git a/types_test.go b/types_test.go index a5db1d4..08f0b04 100644 --- a/types_test.go +++ b/types_test.go @@ -4,7 +4,7 @@ import ( "testing" "time" - "github.com/go-telegram-bot-api/telegram-bot-api" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) func TestUserStringWith(t *testing.T) { @@ -274,7 +274,7 @@ func TestMessageEntityIsTextLink(t *testing.T) { func TestFileLink(t *testing.T) { file := tgbotapi.File{FilePath: "test/test.txt"} - if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" { + if file.Link("token", tgbotapi.FileEndpoint) != "https://api.telegram.org/file/bottoken/test/test.txt" { t.Fail() } }