Skip to content

Commit

Permalink
telebot: a lot of refactor and clean-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Apr 26, 2020
1 parent e786ae7 commit 5c1986f
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 322 deletions.
34 changes: 6 additions & 28 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"strconv"
"time"

"github.com/pkg/errors"
)

// Rights is a list of privileges available to chat members.
Expand Down Expand Up @@ -126,7 +124,6 @@ func (b *Bot) Restrict(chat *Chat, member *ChatMember) error {
"user_id": member.User.Recipient(),
"until_date": strconv.FormatInt(until, 10),
}

embedRights(params, prv)

data, err := b.Raw("restrictChatMember", params)
Expand Down Expand Up @@ -155,7 +152,6 @@ func (b *Bot) Promote(chat *Chat, member *ChatMember) error {
"chat_id": chat.Recipient(),
"user_id": member.User.Recipient(),
}

embedRights(params, prv)

data, err := b.Raw("promoteChatMember", params)
Expand Down Expand Up @@ -183,20 +179,11 @@ func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) {
}

var resp struct {
Ok bool
Result []ChatMember
Description string `json:"description"`
Result []ChatMember
}

err = json.Unmarshal(data, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}

if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}

return resp.Result, nil
}

Expand All @@ -212,20 +199,11 @@ func (b *Bot) Len(chat *Chat) (int, error) {
}

var resp struct {
Ok bool
Result int
Description string `json:"description"`
}

err = json.Unmarshal(data, &resp)
if err != nil {
return 0, errors.Wrap(err, "bad response json")
Result int
}

if !resp.Ok {
return 0, errors.Errorf("api error: %s", resp.Description)
if err := json.Unmarshal(data, &resp); err != nil {
return 0, wrapError(err)
}

return resp.Result, nil
}

Expand Down
3 changes: 2 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (b *Bot) sendFiles(method string, files map[string]File, params map[string]

resp, err := b.client.Post(url, writer.FormDataContentType(), &buf)
if err != nil {
return nil, errors.Wrap(err, "http.Post failed")
return nil, wrapError(err)
}
resp.Close = true
defer resp.Body.Close()
Expand Down Expand Up @@ -181,6 +181,7 @@ func (b *Bot) getUpdates(offset int, timeout time.Duration) ([]Update, error) {
"offset": strconv.Itoa(offset),
"timeout": strconv.Itoa(int(timeout / time.Second)),
}

data, err := b.Raw("getUpdates", params)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 5c1986f

Please sign in to comment.