From 85900d7f97542d1b2f32ac424a6cf18c28ee5ca8 Mon Sep 17 00:00:00 2001 From: Alex Borisov <79996669747@ya.ru> Date: Sat, 30 Nov 2024 18:34:48 +0300 Subject: [PATCH 1/2] Some changes in file requests. Now it should work like this: ```golang bot, err := tgbotapi.NewBotAPIWithClient("YOUR_TOKEN", "https://your-custom-api.example.com/bot%s/%s", "https://your-custom-file-api.example.com/file/bot%s/%s", &http.Client{}) file, err := bot.GetFile(tgbotapi.FileConfig{FileID: "fileID"}, "https://your-custom-file-api.example.com/file/bot%s/%s") ``` --- bot.go | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/bot.go b/bot.go index 25e5328d..43310236 100644 --- a/bot.go +++ b/bot.go @@ -23,14 +23,13 @@ type HTTPClient interface { // BotAPI allows you to interact with the Telegram Bot API. type BotAPI struct { - Token string `json:"token"` - Debug bool `json:"debug"` - Buffer int `json:"buffer"` - - Self User `json:"-"` - Client HTTPClient `json:"-"` - - apiEndpoint string + Token string `json:"token"` + Debug bool `json:"debug"` + Buffer int `json:"buffer"` + APIEndpoint string `json:"-"` + FileEndpoint string `json:"-"` + Self User `json:"-"` + Client HTTPClient `json:"-"` stoppers []context.CancelFunc mu sync.RWMutex @@ -40,7 +39,7 @@ type BotAPI struct { // // It requires a token, provided by @BotFather on Telegram. func NewBotAPI(token string) (*BotAPI, error) { - return NewBotAPIWithClient(token, APIEndpoint, &http.Client{}) + return NewBotAPIWithClient(token, APIEndpoint, FileEndpoint, &http.Client{}) } // NewBotAPIWithAPIEndpoint creates a new BotAPI instance @@ -52,16 +51,16 @@ func NewBotAPIWithAPIEndpoint(token, apiEndpoint string) (*BotAPI, error) { } // NewBotAPIWithClient creates a new BotAPI instance -// and allows you to pass a http.Client. +// and allows you to pass a http.Client and endpoints. // -// It requires a token, provided by @BotFather on Telegram and API endpoint. -func NewBotAPIWithClient(token, apiEndpoint string, client HTTPClient) (*BotAPI, error) { +// It requires a token, provided by @BotFather on Telegram. +func NewBotAPIWithClient(token, apiEndpoint, fileEndpoint string, client HTTPClient) (*BotAPI, error) { bot := &BotAPI{ - Token: token, - Client: client, - Buffer: 100, - - apiEndpoint: apiEndpoint, + Token: token, + Client: client, + Buffer: 100, + APIEndpoint: apiEndpoint, + FileEndpoint: fileEndpoint, } self, err := bot.GetMe() @@ -273,17 +272,25 @@ func (bot *BotAPI) UploadFilesWithContext(ctx context.Context, endpoint string, return &apiResp, nil } -// GetFileDirectURL returns direct URL to file +// GetFile returns a File which can download a file from Telegram. // -// It requires the FileID. -func (bot *BotAPI) GetFileDirectURL(fileID string) (string, error) { - file, err := bot.GetFile(FileConfig{fileID}) +// Requires FileID. Optionally takes a fileEndpoint. If fileEndpoint is empty, +// the default FileEndpoint is used. +func (bot *BotAPI) GetFile(config FileConfig, fileEndpoint ...string) (File, error) { + endpointToUse := bot.FileEndpoint + if len(fileEndpoint) > 0 && fileEndpoint[0] != "" { + endpointToUse = fileEndpoint[0] + } + resp, err := bot.MakeRequest(fmt.Sprintf(endpointToUse, bot.Token, "getFile"), config.params()) //Изменено: используем MakeRequest с новым endpoint if err != nil { - return "", err + return File{}, err } - return file.Link(bot.Token), nil + var file File + err = json.Unmarshal(resp.Result, &file) + + return file, err } // GetMe fetches the currently authenticated bot. From 4cafefd6a1f4699ad638a33b1889b405e80fb1b8 Mon Sep 17 00:00:00 2001 From: Alex Borisov <113920954+soaska@users.noreply.github.com> Date: Mon, 2 Dec 2024 14:37:34 +0300 Subject: [PATCH 2/2] Update bot.go Co-authored-by: Valeriy Selitskiy <239034+iamwavecut@users.noreply.github.com> --- bot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.go b/bot.go index 43310236..e4291b58 100644 --- a/bot.go +++ b/bot.go @@ -282,7 +282,7 @@ func (bot *BotAPI) GetFile(config FileConfig, fileEndpoint ...string) (File, err endpointToUse = fileEndpoint[0] } - resp, err := bot.MakeRequest(fmt.Sprintf(endpointToUse, bot.Token, "getFile"), config.params()) //Изменено: используем MakeRequest с новым endpoint + resp, err := bot.MakeRequest(fmt.Sprintf(endpointToUse, bot.Token, "getFile"), config.params()) if err != nil { return File{}, err }