Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom file endpoint #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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())
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.
Expand Down