|
|
@ -7,6 +7,7 @@ import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"io" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
@ -67,31 +68,49 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, |
|
|
|
} |
|
|
|
} |
|
|
|
defer resp.Body.Close() |
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusForbidden { |
|
|
|
var apiResp APIResponse |
|
|
|
return APIResponse{}, errors.New(ErrAPIForbidden) |
|
|
|
bytes, err := bot.decodeAPIResponse(resp.Body, &apiResp) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return apiResp, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK { |
|
|
|
if bot.Debug { |
|
|
|
return APIResponse{}, errors.New(http.StatusText(resp.StatusCode)) |
|
|
|
log.Printf("%s resp: %s", endpoint, bytes) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(resp.Body) |
|
|
|
if !apiResp.Ok { |
|
|
|
if err != nil { |
|
|
|
parameters := ResponseParameters{} |
|
|
|
return APIResponse{}, err |
|
|
|
if apiResp.Parameters != nil { |
|
|
|
|
|
|
|
parameters = *apiResp.Parameters |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return apiResp, Error{apiResp.Description, parameters} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
return apiResp, nil |
|
|
|
log.Println(endpoint, string(bytes)) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var apiResp APIResponse |
|
|
|
// if debug, read reponse body
|
|
|
|
json.Unmarshal(bytes, &apiResp) |
|
|
|
data, err := ioutil.ReadAll(responseBody) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !apiResp.Ok { |
|
|
|
err = json.Unmarshal(data, resp) |
|
|
|
return apiResp, errors.New(apiResp.Description) |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return apiResp, nil |
|
|
|
return data, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// makeMessageRequest makes a request to a method that returns a Message.
|
|
|
|
// makeMessageRequest makes a request to a method that returns a Message.
|
|
|
@ -712,24 +731,28 @@ func (bot *BotAPI) RestrictChatMember(config RestrictChatMemberConfig) (APIRespo |
|
|
|
} |
|
|
|
} |
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
|
|
|
|
|
|
|
if &config.CanSendMessages != nil { |
|
|
|
if config.CanSendMessages != nil { |
|
|
|
v.Add("can_send_messages", strconv.FormatBool(*config.CanSendMessages)) |
|
|
|
v.Add("can_send_messages", strconv.FormatBool(*config.CanSendMessages)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanSendMediaMessages != nil { |
|
|
|
if config.CanSendMediaMessages != nil { |
|
|
|
v.Add("can_send_media_messages", strconv.FormatBool(*config.CanSendMediaMessages)) |
|
|
|
v.Add("can_send_media_messages", strconv.FormatBool(*config.CanSendMediaMessages)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanSendOtherMessages != nil { |
|
|
|
if config.CanSendOtherMessages != nil { |
|
|
|
v.Add("can_send_other_messages", strconv.FormatBool(*config.CanSendOtherMessages)) |
|
|
|
v.Add("can_send_other_messages", strconv.FormatBool(*config.CanSendOtherMessages)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanAddWebPagePreviews != nil { |
|
|
|
if config.CanAddWebPagePreviews != nil { |
|
|
|
v.Add("can_add_web_page_previews", strconv.FormatBool(*config.CanAddWebPagePreviews)) |
|
|
|
v.Add("can_add_web_page_previews", strconv.FormatBool(*config.CanAddWebPagePreviews)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if config.UntilDate != 0 { |
|
|
|
|
|
|
|
v.Add("until_date", strconv.FormatInt(config.UntilDate, 10)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog("restrictChatMember", v, nil) |
|
|
|
bot.debugLog("restrictChatMember", v, nil) |
|
|
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("restrictChatMember", v) |
|
|
|
return bot.MakeRequest("restrictChatMember", v) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PromoteChatMember add admin rights to user
|
|
|
|
func (bot *BotAPI) PromoteChatMember(config PromoteChatMemberConfig) (APIResponse, error) { |
|
|
|
func (bot *BotAPI) PromoteChatMember(config PromoteChatMemberConfig) (APIResponse, error) { |
|
|
|
v := url.Values{} |
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
|
@ -742,28 +765,28 @@ func (bot *BotAPI) PromoteChatMember(config PromoteChatMemberConfig) (APIRespons |
|
|
|
} |
|
|
|
} |
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
|
|
|
|
|
|
|
if &config.CanChangeInfo != nil { |
|
|
|
if config.CanChangeInfo != nil { |
|
|
|
v.Add("can_change_info", strconv.FormatBool(*config.CanChangeInfo)) |
|
|
|
v.Add("can_change_info", strconv.FormatBool(*config.CanChangeInfo)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanPostMessages != nil { |
|
|
|
if config.CanPostMessages != nil { |
|
|
|
v.Add("can_post_messages", strconv.FormatBool(*config.CanPostMessages)) |
|
|
|
v.Add("can_post_messages", strconv.FormatBool(*config.CanPostMessages)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanEditMessages != nil { |
|
|
|
if config.CanEditMessages != nil { |
|
|
|
v.Add("can_edit_messages", strconv.FormatBool(*config.CanEditMessages)) |
|
|
|
v.Add("can_edit_messages", strconv.FormatBool(*config.CanEditMessages)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanDeleteMessages != nil { |
|
|
|
if config.CanDeleteMessages != nil { |
|
|
|
v.Add("can_delete_messages", strconv.FormatBool(*config.CanDeleteMessages)) |
|
|
|
v.Add("can_delete_messages", strconv.FormatBool(*config.CanDeleteMessages)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanInviteUsers != nil { |
|
|
|
if config.CanInviteUsers != nil { |
|
|
|
v.Add("can_invite_users", strconv.FormatBool(*config.CanInviteUsers)) |
|
|
|
v.Add("can_invite_users", strconv.FormatBool(*config.CanInviteUsers)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanRestrictMembers != nil { |
|
|
|
if config.CanRestrictMembers != nil { |
|
|
|
v.Add("can_restrict_members", strconv.FormatBool(*config.CanRestrictMembers)) |
|
|
|
v.Add("can_restrict_members", strconv.FormatBool(*config.CanRestrictMembers)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanPinMessages != nil { |
|
|
|
if config.CanPinMessages != nil { |
|
|
|
v.Add("can_pin_messages", strconv.FormatBool(*config.CanPinMessages)) |
|
|
|
v.Add("can_pin_messages", strconv.FormatBool(*config.CanPinMessages)) |
|
|
|
} |
|
|
|
} |
|
|
|
if &config.CanPromoteMembers != nil { |
|
|
|
if config.CanPromoteMembers != nil { |
|
|
|
v.Add("can_promote_members", strconv.FormatBool(*config.CanPromoteMembers)) |
|
|
|
v.Add("can_promote_members", strconv.FormatBool(*config.CanPromoteMembers)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -846,9 +869,84 @@ func (bot *BotAPI) GetInviteLink(config ChatConfig) (string, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("exportChatInviteLink", v) |
|
|
|
resp, err := bot.MakeRequest("exportChatInviteLink", v) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var inviteLink string |
|
|
|
var inviteLink string |
|
|
|
err = json.Unmarshal(resp.Result, &inviteLink) |
|
|
|
err = json.Unmarshal(resp.Result, &inviteLink) |
|
|
|
|
|
|
|
|
|
|
|
return inviteLink, err |
|
|
|
return inviteLink, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PinChatMessage pin message in supergroup
|
|
|
|
|
|
|
|
func (bot *BotAPI) PinChatMessage(config PinChatMessageConfig) (APIResponse, error) { |
|
|
|
|
|
|
|
v, err := config.values() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return APIResponse{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UnpinChatMessage unpin message in supergroup
|
|
|
|
|
|
|
|
func (bot *BotAPI) UnpinChatMessage(config UnpinChatMessageConfig) (APIResponse, error) { |
|
|
|
|
|
|
|
v, err := config.values() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return APIResponse{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetChatTitle change title of chat.
|
|
|
|
|
|
|
|
func (bot *BotAPI) SetChatTitle(config SetChatTitleConfig) (APIResponse, error) { |
|
|
|
|
|
|
|
v, err := config.values() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return APIResponse{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetChatDescription change description of chat.
|
|
|
|
|
|
|
|
func (bot *BotAPI) SetChatDescription(config SetChatDescriptionConfig) (APIResponse, error) { |
|
|
|
|
|
|
|
v, err := config.values() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return APIResponse{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetChatPhoto change photo of chat.
|
|
|
|
|
|
|
|
func (bot *BotAPI) SetChatPhoto(config SetChatPhotoConfig) (APIResponse, error) { |
|
|
|
|
|
|
|
params, err := config.params() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return APIResponse{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file := config.getFile() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bot.UploadFile(config.method(), params, config.name(), file) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteChatPhoto delete photo of chat.
|
|
|
|
|
|
|
|
func (bot *BotAPI) DeleteChatPhoto(config DeleteChatPhotoConfig) (APIResponse, error) { |
|
|
|
|
|
|
|
v, err := config.values() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return APIResponse{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
|
|
|
} |
|
|
|