change bot endpoint

pull/271/head
Sergey Ryazanov 6 years ago
parent 67c024dc85
commit 07366fa0b9
  1. 55
      bot.go
  2. 2
      configs.go
  3. 1
      types.go

@ -6,7 +6,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -32,25 +31,40 @@ type BotAPI struct {
apiEndpoint string apiEndpoint string
} }
//const (
// // APIEndpoint is the endpoint for all API methods,
// // with formatting for Sprintf.
// // FileEndpoint is the endpoint for downloading a file from Telegram.
// FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
//)
// NewBotAPI creates a new BotAPI instance. // NewBotAPI creates a new BotAPI instance.
// //
// It requires a token, provided by @BotFather on Telegram. // It requires a token, provided by @BotFather on Telegram.
func NewBotAPI(token string, api string) (*BotAPI, error) { func NewBotAPI(token string) (*BotAPI, error) {
return NewBotAPIWithClient(token, api, &http.Client{}) return NewBotAPIWith(token, "https://api.telegram.org", &http.Client{})
}
// NewBotAPIWith creates a new BotAPI instance
// with custom Telegram Endpoint
//
// It requires a token, provided by @BotFather on Telegram.
func NewBotAPIWithEndpoint(token string, apiEndpoint string) (*BotAPI, error) {
return NewBotAPIWith(token, apiEndpoint, &http.Client{})
} }
// NewBotAPIWithClient creates a new BotAPI instance // NewBotAPIWith creates a new BotAPI instance
// and allows you to pass a http.Client. // with custom http.Client and Telegram Endpoint
// //
// It requires a token, provided by @BotFather on Telegram. // It requires a token, provided by @BotFather on Telegram.
func NewBotAPIWithClient(token string, api string, client *http.Client) (*BotAPI, error) { func NewBotAPIWith(token string, apiEndpoint string, client *http.Client) (*BotAPI, error) {
bot := &BotAPI{ bot := &BotAPI{
Token: token, Token: token,
Client: client, Client: client,
Buffer: 100, Buffer: 100,
shutdownChannel: make(chan interface{}), shutdownChannel: make(chan interface{}),
apiEndpoint: api, apiEndpoint: apiEndpoint,
} }
self, err := bot.GetMe() self, err := bot.GetMe()
@ -67,11 +81,24 @@ func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
b.apiEndpoint = apiEndpoint b.apiEndpoint = apiEndpoint
} }
// Link returns a full path to send Bot request.
//
// It requires the Bot method string.
func (bot *BotAPI) buildUrl(method string) string {
var b strings.Builder
b.WriteString(bot.apiEndpoint)
b.WriteString("/bot")
b.WriteString(bot.Token)
b.WriteString("/")
b.WriteString(method)
return b.String()
}
// MakeRequest makes a request to a specific endpoint with our token. // MakeRequest makes a request to a specific endpoint with our token.
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { func (bot *BotAPI) MakeRequest(method string, params url.Values) (APIResponse, error) {
method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
resp, err := bot.Client.PostForm(method, params) resp, err := bot.Client.PostForm(bot.buildUrl(method), params)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
@ -84,7 +111,7 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse,
} }
if bot.Debug { if bot.Debug {
log.Printf("%s resp: %s", endpoint, bytes) log.Printf("%s resp: %s", method, bytes)
} }
if !apiResp.Ok { if !apiResp.Ok {
@ -145,7 +172,7 @@ func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Messa
// //
// Note that if your FileReader has a size set to -1, it will read // Note that if your FileReader has a size set to -1, it will read
// the file into memory to calculate a size. // the file into memory to calculate a size.
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) { func (bot *BotAPI) UploadFile(method string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) {
ms := multipartstreamer.New() ms := multipartstreamer.New()
switch f := file.(type) { switch f := file.(type) {
@ -194,9 +221,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
return APIResponse{}, errors.New(ErrBadFileType) return APIResponse{}, errors.New(ErrBadFileType)
} }
method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint) req, err := http.NewRequest("POST", bot.buildUrl(method), nil)
req, err := http.NewRequest("POST", method, nil)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }

@ -9,8 +9,6 @@ import (
// Telegram constants // Telegram constants
const ( const (
// APIEndpoint is the endpoint for all API methods,
// with formatting for Sprintf.
// FileEndpoint is the endpoint for downloading a file from Telegram. // FileEndpoint is the endpoint for downloading a file from Telegram.
FileEndpoint = "https://api.telegram.org/file/bot%s/%s" FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
) )

@ -431,6 +431,7 @@ func (f *File) Link(token string) string {
return fmt.Sprintf(FileEndpoint, token, f.FilePath) return fmt.Sprintf(FileEndpoint, token, f.FilePath)
} }
//https://api.telegram.org/file/bot%s/%s"
// ReplyKeyboardMarkup allows the Bot to set a custom keyboard. // ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
type ReplyKeyboardMarkup struct { type ReplyKeyboardMarkup struct {
Keyboard [][]KeyboardButton `json:"keyboard"` Keyboard [][]KeyboardButton `json:"keyboard"`