1
0
Fork 0

🎨 Format and fixes other code

This commit is contained in:
Maxim Lebedev 2019-07-26 15:09:49 +05:00
parent 2c6c60c30a
commit d7159f9599
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
17 changed files with 283 additions and 259 deletions

5
add.go
View File

@ -25,7 +25,7 @@ type AddStickerToSetParameters struct {
// AddStickerToSet add a new sticker to a set created by the bot. Returns True
// on success.
func (b *Bot) AddStickerToSet(params *AddStickerToSetParameters) (ok bool, err error) {
func (b *Bot) AddStickerToSet(params *AddStickerToSetParameters) (bool, error) {
args := http.AcquireArgs()
defer http.ReleaseArgs(args)
args.SetUint("user_id", params.UserID)
@ -50,6 +50,7 @@ func (b *Bot) AddStickerToSet(params *AddStickerToSetParameters) (ok bool, err e
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

View File

@ -145,19 +145,20 @@ func NewAnswerInlineQuery(inlineQueryID string, results ...interface{}) *AnswerI
// option to work, you must first create a game for your bot via @Botfather and
// accept the terms. Otherwise, you may use links like t.me/your_bot?start=XXXX
// that open your bot with a parameter.
func (bot *Bot) AnswerCallbackQuery(params *AnswerCallbackQueryParameters) (ok bool, err error) {
func (bot *Bot) AnswerCallbackQuery(params *AnswerCallbackQueryParameters) (bool, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodAnswerCallbackQuery)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// AnswerPreCheckoutQuery respond to such pre-checkout queries.
@ -169,19 +170,20 @@ func (bot *Bot) AnswerCallbackQuery(params *AnswerCallbackQueryParameters) (ok b
//
// Note: The Bot API must receive an answer within 10 seconds after the
// pre-checkout query was sent.
func (bot *Bot) AnswerPreCheckoutQuery(params *AnswerShippingQueryParameters) (ok bool, err error) {
func (bot *Bot) AnswerPreCheckoutQuery(params *AnswerShippingQueryParameters) (bool, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodAnswerPreCheckoutQuery)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// AnswerShippingQuery reply to shipping queries.
@ -189,35 +191,37 @@ func (bot *Bot) AnswerPreCheckoutQuery(params *AnswerShippingQueryParameters) (o
// 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 bot. On success, True is returned.
func (bot *Bot) AnswerShippingQuery(params *AnswerShippingQueryParameters) (ok bool, err error) {
func (bot *Bot) AnswerShippingQuery(params *AnswerShippingQueryParameters) (bool, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodAnswerShippingQuery)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// AnswerInlineQuery send answers to an inline query. On success, True is returned.
//
// No more than 50 results per query are allowed.
func (bot *Bot) AnswerInlineQuery(params *AnswerInlineQueryParameters) (ok bool, err error) {
func (bot *Bot) AnswerInlineQuery(params *AnswerInlineQueryParameters) (bool, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodAnswerInlineQuery)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

View File

@ -35,50 +35,53 @@ type (
//
// Note: In regular groups (non-supergroups), this method will only work if the
// 'All Members Are Admins' setting is off in the target group.
func (bot *Bot) DeleteChatPhoto(chatID int64) (ok bool, err error) {
func (bot *Bot) DeleteChatPhoto(chatID int64) (bool, error) {
dst, err := parser.Marshal(&DeleteChatPhotoParameters{ChatID: chatID})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodDeleteChatPhoto)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// 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 (bot *Bot) DeleteChatStickerSet(chatID int64) (ok bool, err error) {
func (bot *Bot) DeleteChatStickerSet(chatID int64) (bool, error) {
dst, err := parser.Marshal(&DeleteChatStickerSetParameters{ChatID: chatID})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodDeleteChatStickerSet)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// DeleteWebhook remove webhook integration if you decide to switch back to
// getUpdates. Returns True on success. Requires no parameters.
func (bot *Bot) DeleteWebhook() (ok bool, err error) {
func (bot *Bot) DeleteWebhook() (bool, error) {
resp, err := bot.request(nil, MethodDeleteWebhook)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// DeleteMessage delete a message, including service messages, with the following
@ -88,37 +91,39 @@ func (bot *Bot) DeleteWebhook() (ok bool, err error) {
// bot is an administrator of a group, 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.
func (bot *Bot) DeleteMessage(chatID int64, messageID int) (ok bool, err error) {
func (bot *Bot) DeleteMessage(chatID int64, messageID int) (bool, error) {
dst, err := parser.Marshal(&DeleteMessageParameters{
ChatID: chatID,
MessageID: messageID,
})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodDeleteMessage)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// DeleteStickerFromSet delete a sticker from a set created by the bot. Returns
// True on success.
func (bot *Bot) DeleteStickerFromSet(sticker string) (ok bool, err error) {
func (bot *Bot) DeleteStickerFromSet(sticker string) (bool, error) {
dst, err := parser.Marshal(&DeleteStickerFromSetParameters{Sticker: sticker})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodDeleteStickerFromSet)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

63
edit.go
View File

@ -141,58 +141,58 @@ func NewMessageText(text string) *EditMessageTextParameters {
// 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 (bot *Bot) EditMessageLiveLocation(params *EditMessageLiveLocationParameters) (msg *Message, err error) {
func (bot *Bot) EditMessageLiveLocation(params *EditMessageLiveLocationParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodEditMessageLiveLocation)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// EditMessageText edit text and game messages sent by the bot or via the bot
// (for inline bots). On success, if edited message is sent by the bot, the
// edited Message is returned, otherwise True is returned.
func (bot *Bot) EditMessageText(params *EditMessageTextParameters) (msg *Message, err error) {
func (bot *Bot) EditMessageText(params *EditMessageTextParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodEditMessageText)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// EditMessageCaption edit captions of messages sent by the bot or via the bot
// (for inline bots). On success, if edited message is sent by the bot, the
// edited Message is returned, otherwise True is returned.
func (bot *Bot) EditMessageCaption(params *EditMessageCaptionParameters) (msg *Message, err error) {
func (bot *Bot) EditMessageCaption(params *EditMessageCaptionParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodEditMessageCaption)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// EditMessageMedia edit audio, document, photo, or video messages. If a message
@ -201,38 +201,37 @@ func (bot *Bot) EditMessageCaption(params *EditMessageCaptionParameters) (msg *M
// edited, new file can't be uploaded. Use previously uploaded file via its
// file_id or specify a URL. On success, if the edited message was sent by the
// bot, the edited Message is returned, otherwise True is returned.
func (b *Bot) EditMessageMedia(emmp *EditMessageMediaParameters) (msg *Message, err error) {
var src []byte
src, err = parser.Marshal(emmp)
func (b *Bot) EditMessageMedia(emmp *EditMessageMediaParameters) (*Message, error) {
src, err := parser.Marshal(emmp)
if err != nil {
return
return nil, err
}
resp, err := b.request(src, MethodEditMessageMedia)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// EditMessageReplyMarkup edit only the reply markup of messages sent by the bot
// or via the bot (for inline bots). On success, if edited message is sent by the
// bot, the edited Message is returned, otherwise True is returned.
func (bot *Bot) EditMessageReplyMarkup(params *EditMessageReplyMarkupParameters) (msg *Message, err error) {
func (bot *Bot) EditMessageReplyMarkup(params *EditMessageReplyMarkupParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodEditMessageReplyMarkup)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}

View File

@ -9,17 +9,18 @@ type ExportChatInviteLinkParameters struct {
// 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.
func (bot *Bot) ExportChatInviteLink(chatID int64) (inviteLink string, err error) {
func (bot *Bot) ExportChatInviteLink(chatID int64) (string, error) {
dst, err := parser.Marshal(&ExportChatInviteLinkParameters{ChatID: chatID})
if err != nil {
return
return "", err
}
resp, err := bot.request(dst, MethodExportChatInviteLink)
if err != nil {
return
return "", err
}
var inviteLink string
err = parser.Unmarshal(resp.Result, &inviteLink)
return
return inviteLink, err
}

View File

@ -28,18 +28,18 @@ func NewForwardMessage(from, to int64, messageID int) *ForwardMessageParameters
}
// ForwardMessage forward messages of any kind. On success, the sent Message is returned.
func (bot *Bot) ForwardMessage(params *ForwardMessageParameters) (msg *Message, err error) {
func (bot *Bot) ForwardMessage(params *ForwardMessageParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodForwardMessage)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}

119
get.go
View File

@ -110,77 +110,79 @@ func NewGameHighScores(userID int) *GetGameHighScoresParameters {
// 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.
func (bot *Bot) GetChat(chatID int64) (chat *Chat, err error) {
func (bot *Bot) GetChat(chatID int64) (*Chat, error) {
dst, err := parser.Marshal(&GetChatParameters{ChatID: chatID})
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodGetChat)
if err != nil {
return
return nil, err
}
chat = new(Chat)
err = parser.Unmarshal(resp.Result, chat)
return
var chat Chat
err = parser.Unmarshal(resp.Result, &chat)
return &chat, err
}
// GetChatAdministrators get a list of administrators in a chat. On success,
// returns an Array of ChatMember objects that contains information about all
// chat administrators except other bots. If the chat is a group or a supergroup
// and no administrators were appointed, only the creator will be returned.
func (bot *Bot) GetChatAdministrators(chatID int64) (members []ChatMember, err error) {
func (bot *Bot) GetChatAdministrators(chatID int64) ([]ChatMember, error) {
dst, err := parser.Marshal(&GetChatAdministratorsParameters{ChatID: chatID})
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodGetChatAdministrators)
if err != nil {
return
return nil, err
}
err = parser.Unmarshal(resp.Result, &members)
return
var chatMembers []ChatMember
err = parser.Unmarshal(resp.Result, &chatMembers)
return chatMembers, err
}
// GetChatMember get information about a member of a chat. Returns a ChatMember
// object on success.
func (bot *Bot) GetChatMember(chatID int64, userID int) (member *ChatMember, err error) {
func (bot *Bot) GetChatMember(chatID int64, userID int) (*ChatMember, error) {
dst, err := parser.Marshal(&GetChatMemberParameters{
ChatID: chatID,
UserID: userID,
})
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodGetChatMember)
if err != nil {
return
return nil, err
}
member = new(ChatMember)
err = parser.Unmarshal(resp.Result, member)
return
var chatMember ChatMember
err = parser.Unmarshal(resp.Result, &chatMember)
return &chatMember, err
}
// GetChatMembersCount get the number of members in a chat. Returns Int on
// success.
func (bot *Bot) GetChatMembersCount(chatID int64) (count int, err error) {
func (bot *Bot) GetChatMembersCount(chatID int64) (int, error) {
dst, err := parser.Marshal(&GetChatMembersCountParameters{ChatID: chatID})
if err != nil {
return
return 0, err
}
resp, err := bot.request(dst, MethodGetChatMembersCount)
if err != nil {
return
return 0, err
}
var count int
err = parser.Unmarshal(resp.Result, &count)
return
return count, err
}
// GetFile get basic info about a file and prepare it for downloading. For the
@ -194,118 +196,119 @@ func (bot *Bot) GetChatMembersCount(chatID int64) (count int, err error) {
// Note: This function may not preserve the original file name and MIME type. You
// should save the file's MIME type and name (if available) when the File object
// is received.
func (bot *Bot) GetFile(fileID string) (file *File, err error) {
func (bot *Bot) GetFile(fileID string) (*File, error) {
dst, err := parser.Marshal(&GetFileParameters{FileID: fileID})
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodGetFile)
if err != nil {
return
return nil, err
}
file = new(File)
err = parser.Unmarshal(resp.Result, file)
return
var file File
err = parser.Unmarshal(resp.Result, &file)
return &file, err
}
// GetMe testing your bot's auth token. Requires no parameters. Returns basic
// information about the bot in form of a User object.
func (bot *Bot) GetMe() (me *User, err error) {
func (bot *Bot) GetMe() (*User, error) {
resp, err := bot.request(nil, MethodGetMe)
if err != nil {
return
return nil, err
}
me = new(User)
err = parser.Unmarshal(resp.Result, me)
return
var me User
err = parser.Unmarshal(resp.Result, &me)
return &me, err
}
// GetUpdates receive incoming updates using long polling. An Array of Update objects is returned.
func (bot *Bot) GetUpdates(params *GetUpdatesParameters) (updates []Update, err error) {
func (bot *Bot) GetUpdates(params *GetUpdatesParameters) ([]Update, error) {
if params == nil {
params = &GetUpdatesParameters{Limit: 100}
}
src, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(src, MethodGetUpdates)
if err != nil {
return
return nil, err
}
updates = make([]Update, params.Limit)
updates := make([]Update, params.Limit)
err = parser.Unmarshal(resp.Result, &updates)
return
return updates, err
}
// GetUserProfilePhotos get a list of profile pictures for a user. Returns a UserProfilePhotos object.
func (bot *Bot) GetUserProfilePhotos(params *GetUserProfilePhotosParameters) (photos *UserProfilePhotos, err error) {
func (bot *Bot) GetUserProfilePhotos(params *GetUserProfilePhotosParameters) (*UserProfilePhotos, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodGetUserProfilePhotos)
if err != nil {
return
return nil, err
}
photos = new(UserProfilePhotos)
err = parser.Unmarshal(resp.Result, photos)
return
var photos UserProfilePhotos
err = parser.Unmarshal(resp.Result, &photos)
return &photos, err
}
// 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.
func (bot *Bot) GetWebhookInfo() (info *WebhookInfo, err error) {
func (bot *Bot) GetWebhookInfo() (*WebhookInfo, error) {
resp, err := bot.request(nil, MethodGetWebhookInfo)
if err != nil {
return
return nil, err
}
info = new(WebhookInfo)
err = parser.Unmarshal(resp.Result, info)
return
var info WebhookInfo
err = parser.Unmarshal(resp.Result, &info)
return &info, err
}
// GetGameHighScores get data for high score tables. Will return the score of the
// specified user and several of his neighbors in a game. On success, returns an
// Array of GameHighScore objects.
func (bot *Bot) GetGameHighScores(params *GetGameHighScoresParameters) (scores []GameHighScore, err error) {
func (bot *Bot) GetGameHighScores(params *GetGameHighScoresParameters) ([]GameHighScore, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodGetGameHighScores)
if err != nil {
return
return nil, err
}
var scores []GameHighScore
err = parser.Unmarshal(resp.Result, &scores)
return
return scores, err
}
// GetStickerSet get a sticker set. On success, a StickerSet object is returned.
func (bot *Bot) GetStickerSet(name string) (set *StickerSet, err error) {
func (bot *Bot) GetStickerSet(name string) (*StickerSet, error) {
dst, err := parser.Marshal(&GetStickerSetParameters{Name: name})
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodGetStickerSet)
if err != nil {
return
return nil, err
}
set = new(StickerSet)
err = parser.Unmarshal(resp.Result, set)
return
var set StickerSet
err = parser.Unmarshal(resp.Result, &set)
return &set, err
}

View File

@ -22,17 +22,18 @@ type KickChatMemberParameters struct {
// 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 (bot *Bot) KickChatMember(params *KickChatMemberParameters) (ok bool, err error) {
func (bot *Bot) KickChatMember(params *KickChatMemberParameters) (bool, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodKickChatMember)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

View File

@ -7,17 +7,18 @@ type LeaveChatParameters struct {
}
// LeaveChat leave a group, supergroup or channel. Returns True on success.
func (bot *Bot) LeaveChat(chatID int64) (ok bool, err error) {
func (bot *Bot) LeaveChat(chatID int64) (bool, error) {
dst, err := parser.Marshal(&LeaveChatParameters{ChatID: chatID})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodLeaveChat)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

9
pin.go
View File

@ -17,17 +17,18 @@ type PinChatMessageParameters struct {
// PinChatMessage pin a message in 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 (bot *Bot) PinChatMessage(params *PinChatMessageParameters) (ok bool, err error) {
func (bot *Bot) PinChatMessage(params *PinChatMessageParameters) (bool, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodPinChatMessage)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

83
send.go
View File

@ -561,20 +561,20 @@ func (bot *Bot) SendChatAction(chatID int64, action string) (bool, error) {
}
// SendContact send phone contacts. On success, the sent Message is returned.
func (bot *Bot) SendContact(params *SendContactParameters) (msg *Message, err error) {
func (bot *Bot) SendContact(params *SendContactParameters) (*Message, error) {
dst, err := parser.Marshal(*params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSendContact)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// SendDocument send general files. On success, the sent Message is returned. Bots can currently send
@ -613,71 +613,72 @@ func (bot *Bot) SendDocument(params *SendDocumentParameters) (*Message, error) {
}
// SendInvoice send invoices. On success, the sent Message is returned.
func (bot *Bot) SendInvoice(params *SendInvoiceParameters) (msg *Message, err error) {
func (bot *Bot) SendInvoice(params *SendInvoiceParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSendInvoice)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// SendLocation send point on the map. On success, the sent Message is returned.
func (bot *Bot) SendLocation(params *SendLocationParameters) (msg *Message, err error) {
func (bot *Bot) SendLocation(params *SendLocationParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSendLocation)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// SendMediaGroup send a group of photos or videos as an album. On success, an array of the sent
// Messages is returned.
func (bot *Bot) SendMediaGroup(params *SendMediaGroupParameters) (album []Message, err error) {
func (bot *Bot) SendMediaGroup(params *SendMediaGroupParameters) ([]Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSendMediaGroup)
if err != nil {
return
return nil, err
}
err = parser.Unmarshal(resp.Result, &album)
return
var group []Message
err = parser.Unmarshal(resp.Result, &group)
return group, err
}
// SendMessage send text messages. On success, the sent Message is returned.
func (bot *Bot) SendMessage(params *SendMessageParameters) (msg *Message, err error) {
func (bot *Bot) SendMessage(params *SendMessageParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSendMessage)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// SendPhoto send photos. On success, the sent Message is returned.
@ -733,37 +734,37 @@ func (b *Bot) SendPoll(params SendPollConfig) (*Message, error) {
}
// SendVenue send information about a venue. On success, the sent Message is returned.
func (bot *Bot) SendVenue(params *SendVenueParameters) (msg *Message, err error) {
func (bot *Bot) SendVenue(params *SendVenueParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSendVenue)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// SendGame send a game. On success, the sent Message is returned.
func (bot *Bot) SendGame(params *SendGameParameters) (msg *Message, err error) {
func (bot *Bot) SendGame(params *SendGameParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSendGame)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// SendSticker send .webp stickers. On success, the sent Message is returned.

57
set.go
View File

@ -140,22 +140,23 @@ func NewGameScore(userID, score int) *SetGameScoreParameters {
// SetChatDescription change the description of 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 (bot *Bot) SetChatDescription(chatID int64, description string) (ok bool, err error) {
func (bot *Bot) SetChatDescription(chatID int64, description string) (bool, error) {
dst, err := parser.Marshal(&SetChatDescriptionParameters{
ChatID: chatID,
Description: description,
})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodSetChatDescription)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// SetChatPhoto set a new profile photo for the chat. Photos can't be changed for private chats. The
@ -183,22 +184,23 @@ func (bot *Bot) SetChatPhoto(chatID int64, chatPhoto interface{}) (bool, error)
// 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 (bot *Bot) SetChatStickerSet(chatID int64, stickerSetName string) (ok bool, err error) {
func (bot *Bot) SetChatStickerSet(chatID int64, stickerSetName string) (bool, error) {
dst, err := parser.Marshal(&SetChatStickerSetParameters{
ChatID: chatID,
StickerSetName: stickerSetName,
})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodSetChatStickerSet)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// SetChatTitle change the title of a chat. Titles can't be changed for private
@ -207,22 +209,23 @@ func (bot *Bot) SetChatStickerSet(chatID int64, stickerSetName string) (ok bool,
//
// Note: In regular groups (non-supergroups), this method will only work if the
// 'All Members Are Admins' setting is off in the target group.
func (bot *Bot) SetChatTitle(chatID int64, title string) (ok bool, err error) {
func (bot *Bot) SetChatTitle(chatID int64, title string) (bool, error) {
dst, err := parser.Marshal(&SetChatTitleParameters{
ChatID: chatID,
Title: title,
})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodSetChatTitle)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// SetPassportDataErrors informs a user that some of the Telegram Passport
@ -235,22 +238,23 @@ func (bot *Bot) SetChatTitle(chatID int64, title string) (ok bool, err error) {
// 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(userId int, errors []PassportElementError) (ok bool, err error) {
func (b *Bot) SetPassportDataErrors(userId int, errors []PassportElementError) (bool, error) {
dst, err := parser.Marshal(&SetPassportDataErrorsParameters{
UserID: userId,
Errors: errors,
})
if err != nil {
return
return false, err
}
resp, err := b.request(dst, MethodSetPassportDataErrors)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}
// SetWebhook specify a url and receive incoming updates via an outgoing webhook.
@ -301,38 +305,39 @@ func (bot *Bot) SetWebhook(params *SetWebhookParameters) (bool, error) {
// message was sent by the bot, returns the edited Message, otherwise returns
// True. Returns an error, if the new score is not greater than the user's
// current score in the chat and force is False.
func (bot *Bot) SetGameScore(params *SetGameScoreParameters) (msg *Message, err error) {
func (bot *Bot) SetGameScore(params *SetGameScoreParameters) (*Message, error) {
dst, err := parser.Marshal(params)
if err != nil {
return
return nil, err
}
resp, err := bot.request(dst, MethodSetGameScore)
if err != nil {
return
return nil, err
}
msg = new(Message)
err = parser.Unmarshal(resp.Result, msg)
return
var msg Message
err = parser.Unmarshal(resp.Result, &msg)
return &msg, err
}
// 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) (ok bool, err error) {
func (b *Bot) SetStickerPositionInSet(sticker string, position int) (bool, error) {
dst, err := parser.Marshal(&SetStickerPositionInSetParameters{
Sticker: sticker,
Position: position,
})
if err != nil {
return
return false, err
}
resp, err := b.request(dst, MethodSetStickerPositionInSet)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

View File

@ -26,22 +26,22 @@ var (
parser = json.ConfigFastest
)
func (bot *Bot) request(dst []byte, method string) (*Response, error) {
if bot.Client == nil {
bot.SetClient(&defaultClient)
func (b *Bot) request(dst []byte, method string) (*Response, error) {
if b.Client == nil {
b.SetClient(&defaultClient)
}
requestURI := http.AcquireURI()
requestURI.SetScheme("https")
requestURI.SetHost("api.telegram.org")
requestURI.SetPath(path.Join("bot"+bot.AccessToken, method))
requestURI.SetPath(path.Join("bot"+b.AccessToken, method))
req := http.AcquireRequest()
defer http.ReleaseRequest(req)
req.Header.SetContentType("application/json; charset=utf-8")
req.Header.SetMethod("POST")
req.Header.SetMethod(http.MethodPost)
if dst == nil {
req.Header.SetMethod("GET")
req.Header.SetMethod(http.MethodGet)
}
req.Header.SetRequestURI(requestURI.String())
req.Header.SetUserAgent(path.Join("telegram", Version))
@ -51,7 +51,7 @@ func (bot *Bot) request(dst []byte, method string) (*Response, error) {
resp := http.AcquireResponse()
defer http.ReleaseResponse(resp)
if err := bot.Client.Do(req, resp); err != nil {
if err := b.Client.Do(req, resp); err != nil {
return nil, err
}

View File

@ -12,21 +12,22 @@ type UnbanChatMemberParameters struct {
// 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 (bot *Bot) UnbanChatMember(chatID int64, userID int) (ok bool, err error) {
func (bot *Bot) UnbanChatMember(chatID int64, userID int) (bool, error) {
params := UnbanChatMemberParameters{
ChatID: chatID,
UserID: userID,
}
dst, err := parser.Marshal(&params)
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodUnbanChatMember)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

View File

@ -9,17 +9,18 @@ type UnpinChatMessageParameters struct {
// UnpinChatMessage unpin a message in a supergroup chat. 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 (bot *Bot) UnpinChatMessage(chatID int64) (ok bool, err error) {
func (bot *Bot) UnpinChatMessage(chatID int64) (bool, error) {
dst, err := parser.Marshal(&UnpinChatMessageParameters{ChatID: chatID})
if err != nil {
return
return false, err
}
resp, err := bot.request(dst, MethodUnpinChatMessage)
if err != nil {
return
return false, err
}
var ok bool
err = parser.Unmarshal(resp.Result, &ok)
return
return ok, err
}

View File

@ -113,18 +113,16 @@ func (b *Bot) Upload(method, key, name string, file InputFile, args *http.Args)
func createFileField(w *multipart.Writer, file interface{}, key, val string) (err error) {
switch src := file.(type) {
case string: // Send FileID of file on disk path
err = createFileFieldString(w, key, src)
return createFileFieldString(w, key, src)
case *http.URI: // Send by URL
err = w.WriteField(key, src.String())
return w.WriteField(key, src.String())
case []byte: // Upload new
err = createFileFieldRaw(w, key, val, bytes.NewReader(src))
return createFileFieldRaw(w, key, val, bytes.NewReader(src))
case io.Reader: // Upload new
err = createFileFieldRaw(w, key, val, src)
return createFileFieldRaw(w, key, val, src)
default:
err = ErrBadFileType
return ErrBadFileType
}
return
}
func createFileFieldString(w *multipart.Writer, key, src string) (err error) {

View File

@ -47,9 +47,31 @@ const (
var ErrNotEqual = errors.New("credentials hash and credentials data hash is not equal")
// New creates a new default Bot structure based on the input access token.
func New(accessToken string) (*Bot, error) {
var err error
b := new(Bot)
b.SetClient(&defaultClient)
b.AccessToken = accessToken
b.User, err = b.GetMe()
return b, err
}
// SetClient allow set custom fasthttp.Client (for proxy traffic, for example).
func (b *Bot) SetClient(newClient *http.Client) {
if b == nil {
b = new(Bot)
}
b.Client = newClient
}
// NewForceReply calls the response interface to the message.
func NewForceReply() *ForceReply {
return &ForceReply{ForceReply: true}
return &ForceReply{
ForceReply: true,
}
}
// NewInlineMentionURL creates a url.URL for the mention user without username.
@ -162,26 +184,6 @@ func (a *Audio) File() *File {
}
}
// SetClient allow set custom fasthttp.Client (for proxy traffic, for example).
func (b *Bot) SetClient(newClient *http.Client) {
if b == nil {
b = new(Bot)
}
b.Client = newClient
}
// New creates a new default Bot structure based on the input access token.
func New(accessToken string) (*Bot, error) {
var err error
b := new(Bot)
b.SetClient(&defaultClient)
b.AccessToken = accessToken
b.User, err = b.GetMe()
return b, err
}
// IsMessageFromMe checks that the input message is a message from the current
// bot.
func (b *Bot) IsMessageFromMe(m *Message) bool {