pull/271/merge
Sergey Ryazanov 6 years ago committed by GitHub
commit 803cdfb871
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 46
      bot.go
  2. 3
      configs.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"
@ -36,21 +35,29 @@ type BotAPI struct {
// //
// It requires a token, provided by @BotFather on Telegram. // It requires a token, provided by @BotFather on Telegram.
func NewBotAPI(token string) (*BotAPI, error) { func NewBotAPI(token string) (*BotAPI, error) {
return NewBotAPIWithClient(token, &http.Client{}) return NewBotAPIWith(token, "https://api.telegram.org", &http.Client{})
} }
// NewBotAPIWithClient creates a new BotAPI instance // NewBotAPIWith creates a new BotAPI instance
// and allows you to pass a http.Client. // with custom Telegram Endpoint
// //
// It requires a token, provided by @BotFather on Telegram. // It requires a token, provided by @BotFather on Telegram.
func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { func NewBotAPIWithEndpoint(token string, apiEndpoint string) (*BotAPI, error) {
return NewBotAPIWith(token, apiEndpoint, &http.Client{})
}
// NewBotAPIWith creates a new BotAPI instance
// with custom http.Client and Telegram Endpoint
//
// It requires a token, provided by @BotFather on Telegram.
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: APIEndpoint, apiEndpoint: apiEndpoint,
} }
self, err := bot.GetMe() self, err := bot.GetMe()
@ -67,11 +74,24 @@ func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
b.apiEndpoint = apiEndpoint b.apiEndpoint = apiEndpoint
} }
// buildUrl 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 +104,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 +165,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 +214,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,9 +9,6 @@ import (
// Telegram constants // Telegram constants
const ( const (
// APIEndpoint is the endpoint for all API methods,
// with formatting for Sprintf.
APIEndpoint = "https://api.telegram.org/bot%s/%s"
// 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"
) )