1
0
Fork 0

♻️ Moved duplicated methods parts into single util

This commit is contained in:
Maxim Lebedev 2020-06-06 02:57:29 +05:00
parent 30ab992414
commit 2ac03cb7b9
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
9 changed files with 247 additions and 499 deletions

View File

@ -109,13 +109,8 @@ func (b Bot) SendGame(p SendGame) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -136,13 +131,8 @@ func (b Bot) SetGameScore(p SetGameScore) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -156,13 +146,8 @@ func (b Bot) GetGameHighScores(p GetGameHighScores) ([]*GameHighScore, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := make([]*GameHighScore, 0) 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 return nil, err
} }

View File

@ -798,23 +798,17 @@ func NewAnswerInline(inlineQueryID string, results ...InlineQueryResult) AnswerI
// AnswerInlineQuery send answers to an inline query. On success, True is returned. // AnswerInlineQuery send answers to an inline query. On success, True is returned.
// //
// No more than 50 results per query are allowed. // 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) src, err := b.Do(MethodAnswerInlineQuery, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var ok bool return
if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
return false, err
}
return ok, nil
} }
func NewReplyKeyboardRemove(selective bool) ReplyKeyboardRemove { func NewReplyKeyboardRemove(selective bool) ReplyKeyboardRemove {

View File

@ -720,13 +720,8 @@ func (b Bot) GetMe() (*User, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(User) 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 return nil, err
} }
@ -747,13 +742,8 @@ func (b Bot) SendMessage(p SendMessage) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -775,13 +765,8 @@ func (b Bot) ForwardMessage(p ForwardMessage) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -824,13 +809,8 @@ func (b Bot) SendPhoto(p SendPhoto) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -885,13 +865,8 @@ func (b Bot) SendAudio(p SendAudio) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -933,13 +908,8 @@ func (b Bot) SendDocument(p SendDocument) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -993,13 +963,8 @@ func (b Bot) SendVideo(p SendVideo) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -1052,13 +1017,8 @@ func (b Bot) SendAnimation(p SendAnimation) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -1101,13 +1061,8 @@ func (b Bot) SendVoice(p SendVoice) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -1157,13 +1112,8 @@ func (b Bot) SendVideoNote(p SendVideoNote) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -1208,13 +1158,8 @@ func (b Bot) SendMediaGroup(p SendMediaGroup) ([]*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := make([]*Message, 0) 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 return nil, err
} }
@ -1236,13 +1181,8 @@ func (b Bot) SendLocation(p SendLocation) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 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. // 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) src, err := b.Do(MethodEditMessageLiveLocation, p)
if err != nil { if err != nil {
return nil, false, err return nil, err
}
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, false, err
} }
result := new(Message) result := new(Message)
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { if err = parseResponseError(b.marshler, src, result); err != nil {
return nil, resp.Ok, err 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. // 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) src, err := b.Do(MethodStopMessageLiveLocation, p)
if err != nil { if err != nil {
return nil, false, err return nil, err
}
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, false, err
} }
result := new(Message) result := new(Message)
if err = b.marshler.Unmarshal(resp.Result, result); err != nil { if err = parseResponseError(b.marshler, src, result); err != nil {
return nil, resp.Ok, err return nil, err
} }
return result, resp.Ok, nil return result, nil
} }
func NewVenue(chatID int64, latitude, longitude float32, title, address string) SendVenue { 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 return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -1341,13 +1266,8 @@ func (b Bot) SendContact(p SendContact) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -1369,13 +1289,8 @@ func (b Bot) SendPoll(p SendPoll) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -1391,13 +1306,8 @@ func (b Bot) SendDice(p SendDice) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 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. // 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. // 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}) src, err := b.Do(MethodSendChatAction, SendChatAction{ChatID: cid, Action: action})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
// GetUserProfilePhotos get a list of profile pictures for a user. Returns a UserProfilePhotos object. // 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 return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(UserProfilePhotos) 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 return nil, err
} }
@ -1455,13 +1359,8 @@ func (b Bot) GetFile(fid string) (*File, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(File) 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 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. // 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. // 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) src, err := b.Do(MethodKickChatMember, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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}) src, err := b.Do(MethodUnbanChatMember, UnbanChatMember{ChatID: cid, UserID: uid})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
func NewRestrict(chatID int64, userID int, permissions ChatPermissions) RestrictChatMember { 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. // 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) src, err := b.Do(MethodRestrictChatMember, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
func NewPromote(chatID int64, userID int) PromoteChatMember { 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. // 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) src, err := b.Do(MethodPromoteChatMember, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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) src, err := b.Do(MethodSetChatAdministratorCustomTitle, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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) src, err := b.Do(MethodSetChatPermissions, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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. // 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 := make(map[string]string)
params["chat_id"] = strconv.FormatInt(cid, 10) params["chat_id"] = strconv.FormatInt(cid, 10)
var err error
if params["photo"], err = b.marshler.MarshalToString(photo); err != nil { if params["photo"], err = b.marshler.MarshalToString(photo); err != nil {
return false, err return
} }
files := make([]*InputFile, 0) 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...) src, err := b.Upload(MethodSetChatPhoto, params, files...)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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}) src, err := b.Do(MethodDeleteChatPhoto, DeleteChatPhoto{ChatID: cid})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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}) src, err := b.Do(MethodSetChatTitle, SetChatTitle{ChatID: cid, Title: title})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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}) src, err := b.Do(MethodSetChatDescription, SetChatDescription{ChatID: cid, Description: txt})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
func NewPin(chatID int64, messageID int) PinChatMessage { 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. // 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) src, err := b.Do(MethodPinChatMessage, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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}) src, err := b.Do(MethodUnpinChatMessage, UnpinChatMessage{ChatID: cid})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
// LeaveChat leave a group, supergroup or channel. Returns True on success. // 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}) src, err := b.Do(MethodLeaveChat, LeaveChat{ChatID: cid})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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 return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Chat) 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 return nil, err
} }
@ -1819,13 +1699,8 @@ func (b Bot) GetChatAdministrators(cid int64) ([]*ChatMember, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := make([]*ChatMember, 0) 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 return nil, err
} }
@ -1859,13 +1734,8 @@ func (b Bot) GetChatMember(cid int64, uid int) (*ChatMember, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(ChatMember) 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 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. // 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}) src, err := b.Do(MethodSetChatStickerSet, SetChatStickerSet{ChatID: cid, StickerSetName: name})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
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. // 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}) src, err := b.Do(MethodDeleteChatStickerSet, DeleteChatStickerSet{ChatID: cid})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
func NewAnswerCallback(callbackQueryID string) AnswerCallbackQuery { 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. // 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) src, err := b.Do(MethodAnswerCallbackQuery, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
// SetMyCommands change the list of the bot's commands. Returns True on success. // 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) src, err := b.Do(MethodSetMyCommands, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }
// GetMyCommands get the current list of the bot's commands. Requires no parameters. Returns Array of BotCommand on // 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 return nil, err
} }
resp := new(Response) result := make([]*BotCommand, 0)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = parseResponseError(b.marshler, src, &result); err != nil {
return nil, err
}
var result []*BotCommand
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return nil, err return nil, err
} }

View File

@ -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. // 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. // 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{ src, err := b.Do(MethodSetPassportDataErrors, SetPassportDataErrors{
UserID: uid, Errors: errors, UserID: uid, Errors: errors,
}) })
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
/* TODO(toby3d) /* TODO(toby3d)

View File

@ -255,13 +255,8 @@ func (b Bot) SendInvoice(p SendInvoice) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -278,23 +273,17 @@ func NewAnswerShipping(shippingQueryID string, ok bool) AnswerShippingQuery {
// AnswerShippingQuery reply to shipping queries. // 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. // 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) src, err := b.Do(MethodAnswerShippingQuery, p)
if err != nil { if err != nil {
return false, err return false, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
func NewAnswerPreCheckout(preCheckoutQueryID string, ok bool) AnswerPreCheckoutQuery { 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. // 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. // 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) src, err := b.Do(MethodAnswerPreCheckoutQuery, p)
if err != nil { if err != nil {
return false, err return false, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }

View File

@ -203,13 +203,8 @@ func (b Bot) SendSticker(p SendSticker) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -223,13 +218,8 @@ func (b Bot) GetStickerSet(name string) (*StickerSet, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(StickerSet) 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 return nil, err
} }
@ -251,13 +241,8 @@ func (b Bot) UploadStickerFile(uid int, sticker *InputFile) (*File, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(File) 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 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. // 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 := make(map[string]string)
params["user_id"] = strconv.Itoa(p.UserID) params["user_id"] = strconv.Itoa(p.UserID)
params["name"] = p.Name params["name"] = p.Name
@ -283,13 +268,12 @@ func (b *Bot) CreateNewStickerSet(p CreateNewStickerSet) (bool, error) {
params["emojis"] = p.Emojis params["emojis"] = p.Emojis
params["contains_masks"] = strconv.FormatBool(p.ContainsMasks) params["contains_masks"] = strconv.FormatBool(p.ContainsMasks)
var err error
if params["png_sticker"], err = b.marshler.MarshalToString(p.PNGSticker); err != nil { 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 { if params["mask_position"], err = b.marshler.MarshalToString(p.MaskPosition); err != nil {
return false, err return
} }
files := make([]*InputFile, 0) files := make([]*InputFile, 0)
@ -299,36 +283,29 @@ func (b *Bot) CreateNewStickerSet(p CreateNewStickerSet) (bool, error) {
src, err := b.Upload(MethodCreateNewStickerSet, params, files...) src, err := b.Upload(MethodCreateNewStickerSet, params, files...)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
// AddStickerToSet add a new sticker to a set created by the b. Returns True on success. // 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 := make(map[string]string)
params["user_id"] = strconv.Itoa(p.UserID) params["user_id"] = strconv.Itoa(p.UserID)
params["name"] = p.Name params["name"] = p.Name
params["emojis"] = p.Emojis params["emojis"] = p.Emojis
var err error
if params["png_sticker"], err = b.marshler.MarshalToString(p.PNGSticker); err != nil { 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 { if params["mask_position"], err = b.marshler.MarshalToString(p.MaskPosition); err != nil {
return false, err return
} }
files := make([]*InputFile, 0) files := make([]*InputFile, 0)
@ -338,75 +315,56 @@ func (b *Bot) AddStickerToSet(p AddStickerToSet) (bool, error) {
src, err := b.Upload(MethodAddStickerToSet, params, files...) src, err := b.Upload(MethodAddStickerToSet, params, files...)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
// SetStickerPositionInSet move a sticker in a set created by the bot to a specific position. Returns True on success. // 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{ src, err := b.marshler.Marshal(&SetStickerPositionInSet{
Sticker: sticker, Sticker: sticker,
Position: position, Position: position,
}) })
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
// DeleteStickerFromSet delete a sticker from a set created by the b. Returns True on success. // 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}) src, err := b.Do(MethodDeleteStickerFromSet, DeleteStickerFromSet{Sticker: sticker})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
// SetStickerSetThumb set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets // SetStickerSetThumb set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets
// only. Returns True on success. // 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 := make(map[string]string)
params["name"] = p.Name params["name"] = p.Name
params["user_id"] = strconv.Itoa(p.UserID) params["user_id"] = strconv.Itoa(p.UserID)
var err error
if params["thumb"], err = b.marshler.MarshalToString(p.Thumb); err != nil { if params["thumb"], err = b.marshler.MarshalToString(p.Thumb); err != nil {
return false, err return
} }
files := make([]*InputFile, 0) files := make([]*InputFile, 0)
@ -416,20 +374,14 @@ func (b *Bot) SetStickerSetThumb(p SetStickerSetThumb) (bool, error) {
src, err := b.Upload(MethodSetStickerSetThumb, params, files...) src, err := b.Upload(MethodSetStickerSetThumb, params, files...)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
// InSet checks that the current sticker in the stickers set. // InSet checks that the current sticker in the stickers set.

View File

@ -120,13 +120,8 @@ func (b Bot) GetUpdates(p *GetUpdates) ([]*Update, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := make([]*Update, 0) 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 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. // 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/<token>. Since nobody else knows your bots token, you can be pretty sure its us. // 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/<token>. Since nobody else knows your bots token, you can be pretty sure its us.
func (b Bot) SetWebhook(p SetWebhook) (bool, error) { func (b Bot) SetWebhook(p SetWebhook) (ok bool, err error) {
if p.Certificate.IsAttachment() { if p.Certificate.IsAttachment() {
_, err := b.Upload(MethodSetWebhook, map[string]string{ _, err := b.Upload(MethodSetWebhook, map[string]string{
"allowed_updates": strings.Join(p.AllowedUpdates, ","), "allowed_updates": strings.Join(p.AllowedUpdates, ","),
@ -149,40 +144,28 @@ func (b Bot) SetWebhook(p SetWebhook) (bool, error) {
src, err := b.Do(MethodSetWebhook, p) src, err := b.Do(MethodSetWebhook, p)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
// DeleteWebhook remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters. // 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) src, err := b.Do(MethodDeleteWebhook, nil)
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) if err = parseResponseError(b.marshler, src, &ok); err != nil {
if err = b.marshler.Unmarshal(src, resp); err != nil { return
return false, err
} }
var result bool return
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil {
return false, err
}
return result, nil
} }
// 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. // 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 return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(WebhookInfo) 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 return nil, err
} }

View File

@ -111,13 +111,8 @@ func (b Bot) EditMessageText(p *EditMessageText) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -131,13 +126,8 @@ func (b Bot) EditMessageCaption(p *EditMessageCaption) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -155,13 +145,8 @@ func (b Bot) EditMessageMedia(p EditMessageMedia) (*Message, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -175,13 +160,8 @@ func (b Bot) EditMessageReplyMarkup(p EditMessageReplyMarkup) (*Message, error)
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Message) 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 return nil, err
} }
@ -199,13 +179,8 @@ func (b Bot) StopPoll(p StopPoll) (*Poll, error) {
return nil, err return nil, err
} }
resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil {
return nil, err
}
result := new(Poll) 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 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. // - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there.
// //
// Returns True on success. // 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}) src, err := b.Do(MethodDeleteMessage, DeleteMessage{ChatID: cid, MessageID: mid})
if err != nil { if err != nil {
return false, err return ok, err
} }
resp := new(Response) resp := new(Response)
if err = b.marshler.Unmarshal(src, resp); err != nil { if err = b.marshler.Unmarshal(src, resp); err != nil {
return false, err return
} }
var result bool if err = b.marshler.Unmarshal(resp.Result, &ok); err != nil {
if err = b.marshler.Unmarshal(resp.Result, &result); err != nil { return
return false, err
} }
return result, nil return
} }

27
utils.go Normal file
View File

@ -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
}