pull/266/merge
Nikita Chisnikov 6 years ago committed by GitHub
commit 48dfef60f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      bot.go
  2. 4
      types.go
  3. 4
      types_test.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.

@ -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.

@ -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()
}
}