Skip to content

Commit

Permalink
Merge pull request go-telegram-bot-api#238 from go-telegram-bot-api/a…
Browse files Browse the repository at this point in the history
…dd-api-endpoint-setter

Allow to change API endpoint for bot
  • Loading branch information
zhuharev authored May 24, 2019
2 parents 87e7035 + 8d34583 commit cea05bf
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ type BotAPI struct {
Debug bool `json:"debug"`
Buffer int `json:"buffer"`

Self User `json:"-"`
Client *http.Client `json:"-"`
Self User `json:"-"`
Client *http.Client `json:"-"`
shutdownChannel chan interface{}

apiEndpoint string
}

// NewBotAPI creates a new BotAPI instance.
Expand All @@ -43,10 +45,12 @@ func NewBotAPI(token string) (*BotAPI, error) {
// It requires a token, provided by @BotFather on Telegram.
func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
bot := &BotAPI{
Token: token,
Client: client,
Buffer: 100,
Token: token,
Client: client,
Buffer: 100,
shutdownChannel: make(chan interface{}),

apiEndpoint: APIEndpoint,
}

self, err := bot.GetMe()
Expand All @@ -59,9 +63,13 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
return bot, nil
}

func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
b.apiEndpoint = apiEndpoint
}

// 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(APIEndpoint, bot.Token, endpoint)
method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)

resp, err := bot.Client.PostForm(method, params)
if err != nil {
Expand Down Expand Up @@ -186,7 +194,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
return APIResponse{}, errors.New(ErrBadFileType)
}

method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint)
method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)

req, err := http.NewRequest("POST", method, nil)
if err != nil {
Expand Down Expand Up @@ -490,7 +498,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
return
default:
}

updates, err := bot.GetUpdates(config)
if err != nil {
log.Println(err)
Expand Down

0 comments on commit cea05bf

Please sign in to comment.