From 2ac03cb7b97eba3d7b81ea852679009bce5717eb Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Sat, 6 Jun 2020 02:57:29 +0500 Subject: [PATCH] :recycle: Moved duplicated methods parts into single util --- games.go | 21 +-- inline.go | 16 +- methods.go | 423 +++++++++++++++---------------------------- passport.go | 16 +- payments.go | 35 +--- stickers.go | 114 ++++-------- updates.go | 46 ++--- updating_messages.go | 48 ++--- utils.go | 27 +++ 9 files changed, 247 insertions(+), 499 deletions(-) create mode 100644 utils.go diff --git a/games.go b/games.go index 8cf6eaf..dbb1689 100644 --- a/games.go +++ b/games.go @@ -109,13 +109,8 @@ func (b Bot) SendGame(p SendGame) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -136,13 +131,8 @@ func (b Bot) SetGameScore(p SetGameScore) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -156,13 +146,8 @@ func (b Bot) GetGameHighScores(p GetGameHighScores) ([]*GameHighScore, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := make([]*GameHighScore, 0) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, &result); err != nil { return nil, err } diff --git a/inline.go b/inline.go index 01b3838..1ff0d42 100644 --- a/inline.go +++ b/inline.go @@ -798,23 +798,17 @@ func NewAnswerInline(inlineQueryID string, results ...InlineQueryResult) AnswerI // AnswerInlineQuery send answers to an inline query. On success, True is returned. // // No more than 50 results per query are allowed. -func (b Bot) AnswerInlineQuery(p AnswerInlineQuery) (bool, error) { +func (b Bot) AnswerInlineQuery(p AnswerInlineQuery) (ok bool, err error) { src, err := b.Do(MethodAnswerInlineQuery, p) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var ok bool - if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { - return false, err - } - - return ok, nil + return } func NewReplyKeyboardRemove(selective bool) ReplyKeyboardRemove { diff --git a/methods.go b/methods.go index 0fe1344..3f647b1 100644 --- a/methods.go +++ b/methods.go @@ -720,13 +720,8 @@ func (b Bot) GetMe() (*User, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(User) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -747,13 +742,8 @@ func (b Bot) SendMessage(p SendMessage) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -775,13 +765,8 @@ func (b Bot) ForwardMessage(p ForwardMessage) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -824,13 +809,8 @@ func (b Bot) SendPhoto(p SendPhoto) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -885,13 +865,8 @@ func (b Bot) SendAudio(p SendAudio) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -933,13 +908,8 @@ func (b Bot) SendDocument(p SendDocument) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -993,13 +963,8 @@ func (b Bot) SendVideo(p SendVideo) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1052,13 +1017,8 @@ func (b Bot) SendAnimation(p SendAnimation) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1101,13 +1061,8 @@ func (b Bot) SendVoice(p SendVoice) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1157,13 +1112,8 @@ func (b Bot) SendVideoNote(p SendVideoNote) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1208,13 +1158,8 @@ func (b Bot) SendMediaGroup(p SendMediaGroup) ([]*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := make([]*Message, 0) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, &result); err != nil { return nil, err } @@ -1236,13 +1181,8 @@ func (b Bot) SendLocation(p SendLocation) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1257,43 +1197,33 @@ func NewLiveLocation(latitude, longitude float32) EditMessageLiveLocation { } // EditMessageLiveLocation edit live location messages. A location can be edited until its live_period expires or editing is explicitly disabled by a call to stopMessageLiveLocation. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned. -func (b Bot) EditMessageLiveLocation(p EditMessageLiveLocation) (*Message, bool, error) { +func (b Bot) EditMessageLiveLocation(p EditMessageLiveLocation) (*Message, error) { src, err := b.Do(MethodEditMessageLiveLocation, p) if err != nil { - return nil, false, err - } - - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, false, err + return nil, err } result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return nil, resp.Ok, err + if err = parseResponseError(b.marshler, src, result); err != nil { + return nil, err } - return result, resp.Ok, nil + return result, nil } // StopMessageLiveLocation stop updating a live location message before live_period expires. On success, if the message was sent by the bot, the sent Message is returned, otherwise True is returned. -func (b Bot) StopMessageLiveLocation(p StopMessageLiveLocation) (*Message, bool, error) { +func (b Bot) StopMessageLiveLocation(p StopMessageLiveLocation) (*Message, error) { src, err := b.Do(MethodStopMessageLiveLocation, p) if err != nil { - return nil, false, err - } - - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, false, err + return nil, err } result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { - return nil, resp.Ok, err + if err = parseResponseError(b.marshler, src, result); err != nil { + return nil, err } - return result, resp.Ok, nil + return result, nil } func NewVenue(chatID int64, latitude, longitude float32, title, address string) SendVenue { @@ -1313,13 +1243,8 @@ func (b Bot) SendVenue(p SendVenue) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1341,13 +1266,8 @@ func (b Bot) SendContact(p SendContact) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1369,13 +1289,8 @@ func (b Bot) SendPoll(p SendPoll) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1391,13 +1306,8 @@ func (b Bot) SendDice(p SendDice) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1407,23 +1317,22 @@ func (b Bot) SendDice(p SendDice) (*Message, error) { // SendChatAction tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). Returns True on success. // // We only recommend using this method when a response from the bot will take a noticeable amount of time to arrive. -func (b Bot) SendChatAction(cid int64, action string) (bool, error) { +func (b Bot) SendChatAction(cid int64, action string) (ok bool, err error) { src, err := b.Do(MethodSendChatAction, SendChatAction{ChatID: cid, Action: action}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // GetUserProfilePhotos get a list of profile pictures for a user. Returns a UserProfilePhotos object. @@ -1433,13 +1342,8 @@ func (b Bot) GetUserProfilePhotos(p GetUserProfilePhotos) (*UserProfilePhotos, e return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(UserProfilePhotos) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1455,13 +1359,8 @@ func (b Bot) GetFile(fid string) (*File, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(File) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1478,43 +1377,41 @@ func NewKick(chatID int64, userID int) KickChatMember { // KickChatMember kick a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. // // Note: In regular groups (non-supergroups), this method will only work if the 'All Members Are Admins' setting is off in the target group. Otherwise members may only be removed by the group's creator or by the member that added them. -func (b Bot) KickChatMember(p KickChatMember) (bool, error) { +func (b Bot) KickChatMember(p KickChatMember) (ok bool, err error) { src, err := b.Do(MethodKickChatMember, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // UnbanChatMember unban a previously kicked user in a supergroup or channel. The user will not return to the group or channel automatically, but will be able to join via link, etc. The bot must be an administrator for this to work. Returns True on success. -func (b Bot) UnbanChatMember(cid int64, uid int) (bool, error) { +func (b Bot) UnbanChatMember(cid int64, uid int) (ok bool, err error) { src, err := b.Do(MethodUnbanChatMember, UnbanChatMember{ChatID: cid, UserID: uid}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } func NewRestrict(chatID int64, userID int, permissions ChatPermissions) RestrictChatMember { @@ -1526,23 +1423,22 @@ func NewRestrict(chatID int64, userID int, permissions ChatPermissions) Restrict } // restrict a user in a supergroup. The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. Pass True for all permissions to lift restrictions from a user. Returns True on success. -func (b Bot) RestrictChatMember(p RestrictChatMember) (bool, error) { +func (b Bot) RestrictChatMember(p RestrictChatMember) (ok bool, err error) { src, err := b.Do(MethodRestrictChatMember, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } func NewPromote(chatID int64, userID int) PromoteChatMember { @@ -1553,63 +1449,60 @@ func NewPromote(chatID int64, userID int) PromoteChatMember { } // PromoteChatMember promote or demote a user in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Pass False for all boolean to demote a user. Returns True on success. -func (b Bot) PromoteChatMember(p PromoteChatMember) (bool, error) { +func (b Bot) PromoteChatMember(p PromoteChatMember) (ok bool, err error) { src, err := b.Do(MethodPromoteChatMember, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // SetChatAdministratorCustomTitle method to set a custom title for an administrator in a supergroup promoted by the b. Returns True on success. -func (b Bot) SetChatAdministratorCustomTitle(p SetChatAdministratorCustomTitle) (bool, error) { +func (b Bot) SetChatAdministratorCustomTitle(p SetChatAdministratorCustomTitle) (ok bool, err error) { src, err := b.Do(MethodSetChatAdministratorCustomTitle, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // SetChatPermissions set default chat permissions for all members. The bot must be an administrator in the group or a supergroup for this to work and must have the can_restrict_members admin rights. Returns True on success. -func (b Bot) SetChatPermissions(p SetChatPermissions) (bool, error) { +func (b Bot) SetChatPermissions(p SetChatPermissions) (ok bool, err error) { src, err := b.Do(MethodSetChatPermissions, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // ExportChatInviteLink export an invite link to a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns exported invite link as String on success. @@ -1633,13 +1526,12 @@ func (b Bot) ExportChatInviteLink(cid int64) (string, error) { } // SetChatPhoto set a new profile photo for the chat. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. -func (b Bot) SetChatPhoto(cid int64, photo *InputFile) (bool, error) { +func (b Bot) SetChatPhoto(cid int64, photo *InputFile) (ok bool, err error) { params := make(map[string]string) params["chat_id"] = strconv.FormatInt(cid, 10) - var err error if params["photo"], err = b.marshler.MarshalToString(photo); err != nil { - return false, err + return } files := make([]*InputFile, 0) @@ -1649,80 +1541,76 @@ func (b Bot) SetChatPhoto(cid int64, photo *InputFile) (bool, error) { src, err := b.Upload(MethodSetChatPhoto, params, files...) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // DeleteChatPhoto delete a chat photo. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. -func (b Bot) DeleteChatPhoto(cid int64) (bool, error) { +func (b Bot) DeleteChatPhoto(cid int64) (ok bool, err error) { src, err := b.Do(MethodDeleteChatPhoto, DeleteChatPhoto{ChatID: cid}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // SetChatTitle change the title of a chat. Titles can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. -func (b Bot) SetChatTitle(cid int64, title string) (bool, error) { +func (b Bot) SetChatTitle(cid int64, title string) (ok bool, err error) { src, err := b.Do(MethodSetChatTitle, SetChatTitle{ChatID: cid, Title: title}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // SetChatDescription change the description of a group, a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. -func (b Bot) SetChatDescription(cid int64, txt string) (bool, error) { +func (b Bot) SetChatDescription(cid int64, txt string) (ok bool, err error) { src, err := b.Do(MethodSetChatDescription, SetChatDescription{ChatID: cid, Description: txt}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } func NewPin(chatID int64, messageID int) PinChatMessage { @@ -1733,63 +1621,60 @@ func NewPin(chatID int64, messageID int) PinChatMessage { } // PinChatMessage pin a message in a group, a supergroup, or a channel. The bot must be an administrator in the chat for this to work and must have the ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right in the channel. Returns True on success. -func (b Bot) PinChatMessage(p PinChatMessage) (bool, error) { +func (b Bot) PinChatMessage(p PinChatMessage) (ok bool, err error) { src, err := b.Do(MethodPinChatMessage, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // UnpinChatMessage unpin a message in a group, a supergroup, or a channel. The bot must be an administrator in the chat for this to work and must have the ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right in the channel. Returns True on success. -func (b Bot) UnpinChatMessage(cid int64) (bool, error) { +func (b Bot) UnpinChatMessage(cid int64) (ok bool, err error) { src, err := b.Do(MethodUnpinChatMessage, UnpinChatMessage{ChatID: cid}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // LeaveChat leave a group, supergroup or channel. Returns True on success. -func (b Bot) LeaveChat(cid int64) (bool, error) { +func (b Bot) LeaveChat(cid int64) (ok bool, err error) { src, err := b.Do(MethodLeaveChat, LeaveChat{ChatID: cid}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // GetChat get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). Returns a Chat object on success. @@ -1799,13 +1684,8 @@ func (b Bot) GetChat(cid int64) (*Chat, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Chat) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1819,13 +1699,8 @@ func (b Bot) GetChatAdministrators(cid int64) ([]*ChatMember, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := make([]*ChatMember, 0) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, &result); err != nil { return nil, err } @@ -1859,13 +1734,8 @@ func (b Bot) GetChatMember(cid int64, uid int) (*ChatMember, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(ChatMember) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -1873,43 +1743,41 @@ func (b Bot) GetChatMember(cid int64, uid int) (*ChatMember, error) { } // SetChatStickerSet set a new group sticker set for a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in getChat requests to check if the bot can use this method. Returns True on success. -func (b Bot) SetChatStickerSet(cid int64, name string) (bool, error) { +func (b Bot) SetChatStickerSet(cid int64, name string) (ok bool, err error) { src, err := b.Do(MethodSetChatStickerSet, SetChatStickerSet{ChatID: cid, StickerSetName: name}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // DeleteChatStickerSet delete a group sticker set from a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in getChat requests to check if the bot can use this method. Returns True on success. -func (b Bot) DeleteChatStickerSet(cid int64) (bool, error) { +func (b Bot) DeleteChatStickerSet(cid int64) (ok bool, err error) { src, err := b.Do(MethodDeleteChatStickerSet, DeleteChatStickerSet{ChatID: cid}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } func NewAnswerCallback(callbackQueryID string) AnswerCallbackQuery { @@ -1917,43 +1785,41 @@ func NewAnswerCallback(callbackQueryID string) AnswerCallbackQuery { } // AnswerCallbackQuery send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned. -func (b Bot) AnswerCallbackQuery(p AnswerCallbackQuery) (bool, error) { +func (b Bot) AnswerCallbackQuery(p AnswerCallbackQuery) (ok bool, err error) { src, err := b.Do(MethodAnswerCallbackQuery, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // SetMyCommands change the list of the bot's commands. Returns True on success. -func (b Bot) SetMyCommands(p SetMyCommands) (bool, error) { +func (b Bot) SetMyCommands(p SetMyCommands) (ok bool, err error) { src, err := b.Do(MethodSetMyCommands, p) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } // GetMyCommands get the current list of the bot's commands. Requires no parameters. Returns Array of BotCommand on @@ -1964,13 +1830,8 @@ func (b Bot) GetMyCommands() ([]*BotCommand, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - - var result []*BotCommand - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + result := make([]*BotCommand, 0) + if err = parseResponseError(b.marshler, src, &result); err != nil { return nil, err } diff --git a/passport.go b/passport.go index 32181b9..43e8a09 100644 --- a/passport.go +++ b/passport.go @@ -530,25 +530,19 @@ var ErrNotEqual = errors.New("credentials hash and credentials data hash is not // SetPassportDataErrors informs a user that some of the Telegram Passport elements they provided contains errors. The user will not be able to re-submit their Passport to you until the errors are fixed (the contents of the field for which you returned the error must change). Returns True on success. // // Use this if the data submitted by the user doesn't satisfy the standards your service requires for any reason. For example, if a birthday date seems invalid, a submitted document is blurry, a scan shows evidence of tampering, etc. Supply some details in the error message to make sure the user knows how to correct the issues. -func (b Bot) SetPassportDataErrors(uid int, errors ...PassportElementError) (bool, error) { +func (b Bot) SetPassportDataErrors(uid int, errors ...PassportElementError) (ok bool, err error) { src, err := b.Do(MethodSetPassportDataErrors, SetPassportDataErrors{ UserID: uid, Errors: errors, }) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } /* TODO(toby3d) diff --git a/payments.go b/payments.go index 8b22fc0..fb7b55d 100644 --- a/payments.go +++ b/payments.go @@ -255,13 +255,8 @@ func (b Bot) SendInvoice(p SendInvoice) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -278,23 +273,17 @@ func NewAnswerShipping(shippingQueryID string, ok bool) AnswerShippingQuery { // AnswerShippingQuery reply to shipping queries. // // If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API will send an Update with a shipping_query field to the b. On success, True is returned. -func (b Bot) AnswerShippingQuery(p AnswerShippingQuery) (bool, error) { +func (b Bot) AnswerShippingQuery(p AnswerShippingQuery) (ok bool, err error) { src, err := b.Do(MethodAnswerShippingQuery, p) if err != nil { return false, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } func NewAnswerPreCheckout(preCheckoutQueryID string, ok bool) AnswerPreCheckoutQuery { @@ -309,21 +298,15 @@ func NewAnswerPreCheckout(preCheckoutQueryID string, ok bool) AnswerPreCheckoutQ // Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field pre_checkout_query. Use this method to respond to such pre-checkout queries. On success, True is returned. // // Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent. -func (b Bot) AnswerPreCheckoutQuery(p AnswerShippingQuery) (bool, error) { +func (b Bot) AnswerPreCheckoutQuery(p AnswerShippingQuery) (ok bool, err error) { src, err := b.Do(MethodAnswerPreCheckoutQuery, p) if err != nil { return false, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } diff --git a/stickers.go b/stickers.go index de00861..3ef5a4a 100644 --- a/stickers.go +++ b/stickers.go @@ -203,13 +203,8 @@ func (b Bot) SendSticker(p SendSticker) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -223,13 +218,8 @@ func (b Bot) GetStickerSet(name string) (*StickerSet, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(StickerSet) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -251,13 +241,8 @@ func (b Bot) UploadStickerFile(uid int, sticker *InputFile) (*File, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(File) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -275,7 +260,7 @@ func NewStickerSet(userID int, name, title string, pngSticker *InputFile, emojis } // CreateNewStickerSet create new sticker set owned by a user. The bot will be able to edit the created sticker set. Returns True on success. -func (b *Bot) CreateNewStickerSet(p CreateNewStickerSet) (bool, error) { +func (b *Bot) CreateNewStickerSet(p CreateNewStickerSet) (ok bool, err error) { params := make(map[string]string) params["user_id"] = strconv.Itoa(p.UserID) params["name"] = p.Name @@ -283,13 +268,12 @@ func (b *Bot) CreateNewStickerSet(p CreateNewStickerSet) (bool, error) { params["emojis"] = p.Emojis params["contains_masks"] = strconv.FormatBool(p.ContainsMasks) - var err error if params["png_sticker"], err = b.marshler.MarshalToString(p.PNGSticker); err != nil { - return false, err + return } if params["mask_position"], err = b.marshler.MarshalToString(p.MaskPosition); err != nil { - return false, err + return } files := make([]*InputFile, 0) @@ -299,36 +283,29 @@ func (b *Bot) CreateNewStickerSet(p CreateNewStickerSet) (bool, error) { src, err := b.Upload(MethodCreateNewStickerSet, params, files...) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } // AddStickerToSet add a new sticker to a set created by the b. Returns True on success. -func (b *Bot) AddStickerToSet(p AddStickerToSet) (bool, error) { +func (b *Bot) AddStickerToSet(p AddStickerToSet) (ok bool, err error) { params := make(map[string]string) params["user_id"] = strconv.Itoa(p.UserID) params["name"] = p.Name params["emojis"] = p.Emojis - var err error if params["png_sticker"], err = b.marshler.MarshalToString(p.PNGSticker); err != nil { - return false, err + return } if params["mask_position"], err = b.marshler.MarshalToString(p.MaskPosition); err != nil { - return false, err + return } files := make([]*InputFile, 0) @@ -338,75 +315,56 @@ func (b *Bot) AddStickerToSet(p AddStickerToSet) (bool, error) { src, err := b.Upload(MethodAddStickerToSet, params, files...) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } // SetStickerPositionInSet move a sticker in a set created by the bot to a specific position. Returns True on success. -func (b *Bot) SetStickerPositionInSet(sticker string, position int) (bool, error) { +func (b *Bot) SetStickerPositionInSet(sticker string, position int) (ok bool, err error) { src, err := b.marshler.Marshal(&SetStickerPositionInSet{ Sticker: sticker, Position: position, }) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } // DeleteStickerFromSet delete a sticker from a set created by the b. Returns True on success. -func (b *Bot) DeleteStickerFromSet(sticker string) (bool, error) { +func (b *Bot) DeleteStickerFromSet(sticker string) (ok bool, err error) { src, err := b.Do(MethodDeleteStickerFromSet, DeleteStickerFromSet{Sticker: sticker}) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } // SetStickerSetThumb set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets // only. Returns True on success. -func (b *Bot) SetStickerSetThumb(p SetStickerSetThumb) (bool, error) { +func (b *Bot) SetStickerSetThumb(p SetStickerSetThumb) (ok bool, err error) { params := make(map[string]string) params["name"] = p.Name params["user_id"] = strconv.Itoa(p.UserID) - var err error if params["thumb"], err = b.marshler.MarshalToString(p.Thumb); err != nil { - return false, err + return } files := make([]*InputFile, 0) @@ -416,20 +374,14 @@ func (b *Bot) SetStickerSetThumb(p SetStickerSetThumb) (bool, error) { src, err := b.Upload(MethodSetStickerSetThumb, params, files...) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } // InSet checks that the current sticker in the stickers set. diff --git a/updates.go b/updates.go index c6cc209..20a28eb 100644 --- a/updates.go +++ b/updates.go @@ -120,13 +120,8 @@ func (b Bot) GetUpdates(p *GetUpdates) ([]*Update, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := make([]*Update, 0) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, &result); err != nil { return nil, err } @@ -136,7 +131,7 @@ func (b Bot) GetUpdates(p *GetUpdates) ([]*Update, error) { // SetWebhook specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns true. // // If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g. https://www.example.com/. Since nobody else knows your bot‘s token, you can be pretty sure it’s us. -func (b Bot) SetWebhook(p SetWebhook) (bool, error) { +func (b Bot) SetWebhook(p SetWebhook) (ok bool, err error) { if p.Certificate.IsAttachment() { _, err := b.Upload(MethodSetWebhook, map[string]string{ "allowed_updates": strings.Join(p.AllowedUpdates, ","), @@ -149,40 +144,28 @@ func (b Bot) SetWebhook(p SetWebhook) (bool, error) { src, err := b.Do(MethodSetWebhook, p) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } // DeleteWebhook remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters. -func (b Bot) DeleteWebhook() (bool, error) { +func (b Bot) DeleteWebhook() (ok bool, err error) { src, err := b.Do(MethodDeleteWebhook, nil) if err != nil { - return false, err + return ok, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + if err = parseResponseError(b.marshler, src, &ok); err != nil { + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err - } - - return result, nil + return } // GetWebhookInfo get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty. @@ -192,13 +175,8 @@ func (b Bot) GetWebhookInfo() (*WebhookInfo, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(WebhookInfo) - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } diff --git a/updating_messages.go b/updating_messages.go index ec053e1..72ae0d9 100644 --- a/updating_messages.go +++ b/updating_messages.go @@ -111,13 +111,8 @@ func (b Bot) EditMessageText(p *EditMessageText) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -131,13 +126,8 @@ func (b Bot) EditMessageCaption(p *EditMessageCaption) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -155,13 +145,8 @@ func (b Bot) EditMessageMedia(p EditMessageMedia) (*Message, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -175,13 +160,8 @@ func (b Bot) EditMessageReplyMarkup(p EditMessageReplyMarkup) (*Message, error) return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Message) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -199,13 +179,8 @@ func (b Bot) StopPoll(p StopPoll) (*Poll, error) { return nil, err } - resp := new(Response) - if err = b.marshler.Unmarshal(src, resp); err != nil { - return nil, err - } - result := new(Poll) - if err = b.marshler.Unmarshal(resp.Result, result); err != nil { + if err = parseResponseError(b.marshler, src, result); err != nil { return nil, err } @@ -222,21 +197,20 @@ func (b Bot) StopPoll(p StopPoll) (*Poll, error) { // - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there. // // Returns True on success. -func (b Bot) DeleteMessage(cid int64, mid int) (bool, error) { +func (b Bot) DeleteMessage(cid int64, mid int) (ok bool, err error) { src, err := b.Do(MethodDeleteMessage, DeleteMessage{ChatID: cid, MessageID: mid}) if err != nil { - return false, err + return ok, err } resp := new(Response) if err = b.marshler.Unmarshal(src, resp); err != nil { - return false, err + return } - var result bool - if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { - return false, err + if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil { + return } - return result, nil + return } diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..b35995c --- /dev/null +++ b/utils.go @@ -0,0 +1,27 @@ +package telegram + +import ( + jsoniter "github.com/json-iterator/go" + "golang.org/x/xerrors" +) + +// parseResponseError unmarshal src bytes into dst or return response Error. +func parseResponseError(marshler jsoniter.API, src []byte, dst interface{}) (err error) { + resp := new(Response) + if err = marshler.Unmarshal(src, resp); err != nil { + return + } + + if resp.Ok { + return marshler.Unmarshal(resp.Result, dst) + } + + respErr := new(Error) + respErr.Code = resp.ErrorCode + respErr.Description = resp.Description + respErr.frame = xerrors.Caller(1) + + copy(respErr.Parameters, resp.Parameters) + + return respErr +}