|
|
@ -7,7 +7,6 @@ import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
|
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
@ -60,6 +59,10 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { |
|
|
|
|
|
|
|
|
|
|
|
// 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(endpoint string, params url.Values) (APIResponse, error) { |
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
|
|
|
|
log.Printf("Endpoint: %s, values: %v\n", endpoint, params) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) |
|
|
|
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) |
|
|
|
|
|
|
|
|
|
|
|
resp, err := bot.Client.PostForm(method, params) |
|
|
|
resp, err := bot.Client.PostForm(method, params) |
|
|
@ -68,45 +71,26 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, |
|
|
|
} |
|
|
|
} |
|
|
|
defer resp.Body.Close() |
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
var apiResp APIResponse |
|
|
|
bytes, err := ioutil.ReadAll(resp.Body) |
|
|
|
bytes, err := bot.decodeAPIResponse(resp.Body, &apiResp) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return apiResp, err |
|
|
|
return APIResponse{}, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
if bot.Debug { |
|
|
|
log.Printf("%s resp: %s", endpoint, bytes) |
|
|
|
log.Printf("Endpoint: %s, response: %s\n", endpoint, string(bytes)) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !apiResp.Ok { |
|
|
|
|
|
|
|
return apiResp, errors.New(apiResp.Description) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return apiResp, nil |
|
|
|
var apiResp APIResponse |
|
|
|
} |
|
|
|
err = json.Unmarshal(bytes, &apiResp) |
|
|
|
|
|
|
|
|
|
|
|
// decodeAPIResponse decode response and return slice of bytes if debug enabled.
|
|
|
|
|
|
|
|
// If debug disabled, just decode http.Response.Body stream to APIResponse struct
|
|
|
|
|
|
|
|
// for efficient memory usage
|
|
|
|
|
|
|
|
func (bot *BotAPI) decodeAPIResponse(responseBody io.Reader, resp *APIResponse) (_ []byte, err error) { |
|
|
|
|
|
|
|
if !bot.Debug { |
|
|
|
|
|
|
|
dec := json.NewDecoder(responseBody) |
|
|
|
|
|
|
|
err = dec.Decode(resp) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if debug, read reponse body
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(responseBody) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
return APIResponse{}, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, resp) |
|
|
|
if !apiResp.Ok { |
|
|
|
if err != nil { |
|
|
|
return apiResp, errors.New(apiResp.Description) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return data, nil |
|
|
|
return apiResp, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// UploadFile makes a request to the API with a file.
|
|
|
|
// UploadFile makes a request to the API with a file.
|
|
|
@ -166,6 +150,10 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna |
|
|
|
return APIResponse{}, errors.New(ErrBadFileType) |
|
|
|
return APIResponse{}, errors.New(ErrBadFileType) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
|
|
|
|
log.Printf("Endpoint: %s, fieldname: %s, params: %v, file: %T\n", endpoint, fieldname, params, file) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) |
|
|
|
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) |
|
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", method, nil) |
|
|
|
req, err := http.NewRequest("POST", method, nil) |
|
|
@ -187,7 +175,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
if bot.Debug { |
|
|
|
log.Println(string(bytes)) |
|
|
|
log.Printf("Endpoint: %s, response: %s\n", endpoint, string(bytes)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var apiResp APIResponse |
|
|
|
var apiResp APIResponse |
|
|
@ -231,8 +219,6 @@ func (bot *BotAPI) GetMe() (User, error) { |
|
|
|
var user User |
|
|
|
var user User |
|
|
|
json.Unmarshal(resp.Result, &user) |
|
|
|
json.Unmarshal(resp.Result, &user) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("getMe", nil, user) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return user, nil |
|
|
|
return user, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -286,16 +272,6 @@ func (bot *BotAPI) Send(c Chattable) (Message, error) { |
|
|
|
return message, err |
|
|
|
return message, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// debugLog checks if the bot is currently running in debug mode, and if
|
|
|
|
|
|
|
|
// so will display information about the request and response in the
|
|
|
|
|
|
|
|
// debug log.
|
|
|
|
|
|
|
|
func (bot *BotAPI) debugLog(context string, v url.Values, message interface{}) { |
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
|
|
|
|
log.Printf("%s req : %+v\n", context, v) |
|
|
|
|
|
|
|
log.Printf("%s resp: %+v\n", context, message) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetUserProfilePhotos gets a user's profile photos.
|
|
|
|
// GetUserProfilePhotos gets a user's profile photos.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// It requires UserID.
|
|
|
|
// It requires UserID.
|
|
|
@ -318,8 +294,6 @@ func (bot *BotAPI) GetUserProfilePhotos(config UserProfilePhotosConfig) (UserPro |
|
|
|
var profilePhotos UserProfilePhotos |
|
|
|
var profilePhotos UserProfilePhotos |
|
|
|
json.Unmarshal(resp.Result, &profilePhotos) |
|
|
|
json.Unmarshal(resp.Result, &profilePhotos) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("GetUserProfilePhoto", v, profilePhotos) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return profilePhotos, nil |
|
|
|
return profilePhotos, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -338,8 +312,6 @@ func (bot *BotAPI) GetFile(config FileConfig) (File, error) { |
|
|
|
var file File |
|
|
|
var file File |
|
|
|
json.Unmarshal(resp.Result, &file) |
|
|
|
json.Unmarshal(resp.Result, &file) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("GetFile", v, file) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return file, nil |
|
|
|
return file, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -370,8 +342,6 @@ func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) { |
|
|
|
var updates []Update |
|
|
|
var updates []Update |
|
|
|
json.Unmarshal(resp.Result, &updates) |
|
|
|
json.Unmarshal(resp.Result, &updates) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("getUpdates", v, updates) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return updates, nil |
|
|
|
return updates, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -450,8 +420,6 @@ func (bot *BotAPI) GetChat(config ChatConfig) (Chat, error) { |
|
|
|
var chat Chat |
|
|
|
var chat Chat |
|
|
|
err = json.Unmarshal(resp.Result, &chat) |
|
|
|
err = json.Unmarshal(resp.Result, &chat) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("getChat", v, chat) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return chat, err |
|
|
|
return chat, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -476,8 +444,6 @@ func (bot *BotAPI) GetChatAdministrators(config ChatConfig) ([]ChatMember, error |
|
|
|
var members []ChatMember |
|
|
|
var members []ChatMember |
|
|
|
err = json.Unmarshal(resp.Result, &members) |
|
|
|
err = json.Unmarshal(resp.Result, &members) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("getChatAdministrators", v, members) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return members, err |
|
|
|
return members, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -499,8 +465,6 @@ func (bot *BotAPI) GetChatMembersCount(config ChatConfig) (int, error) { |
|
|
|
var count int |
|
|
|
var count int |
|
|
|
err = json.Unmarshal(resp.Result, &count) |
|
|
|
err = json.Unmarshal(resp.Result, &count) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("getChatMembersCount", v, count) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return count, err |
|
|
|
return count, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -523,8 +487,6 @@ func (bot *BotAPI) GetChatMember(config ChatConfigWithUser) (ChatMember, error) |
|
|
|
var member ChatMember |
|
|
|
var member ChatMember |
|
|
|
err = json.Unmarshal(resp.Result, &member) |
|
|
|
err = json.Unmarshal(resp.Result, &member) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("getChatMember", v, member) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return member, err |
|
|
|
return member, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|