From 98004e726b4057a73e5a85f757b5e67053ff1831 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Sat, 6 Jan 2018 16:59:23 +0500 Subject: [PATCH] :sparkles: Added SendPhoto method --- send_photo.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 send_photo.go diff --git a/send_photo.go b/send_photo.go new file mode 100644 index 0000000..c77e87a --- /dev/null +++ b/send_photo.go @@ -0,0 +1,75 @@ +package telegram + +import ( + "strconv" + + json "github.com/pquerna/ffjson/ffjson" + http "github.com/valyala/fasthttp" +) + +type SendPhotoParameters struct { + // Unique identifier for the target chat + ChatID int64 `json:"chat_id"` + + // Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers + // (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or + // upload a new photo using multipart/form-data. + Photo InputFile `json:"photo"` + + // Photo caption (may also be used when resending photos by file_id), 0-200 characters + Caption string `json:"caption,omitempty"` + + // Disables link previews for links in this message + DisableWebPagePreview bool `json:"disable_web_page_preview,omitempty"` + + // Sends the message silently. Users will receive a notification with no sound. + DisableNotification bool `json:"disable_notification,omitempty"` + + // If the message is a reply, ID of the original message + ReplyToMessageID int `json:"reply_to_message_id,omitempty"` + + // Additional interface options. A JSON-serialized object for an inline keyboard, custom reply + // keyboard, instructions to remove reply keyboard or to force a reply from the user. + ReplyMarkup interface{} `json:"reply_markup,omitempty"` +} + +func NewPhoto(chatID int64, photo interface{}) *SendPhotoParameters { + return &SendPhotoParameters{ + ChatID: chatID, + Photo: photo, + } +} + +// SendPhoto send photos. On success, the sent Message is returned. +func (bot *Bot) SendPhoto(params *SendPhotoParameters) (*Message, error) { + args := http.AcquireArgs() + defer http.ReleaseArgs(args) + args.Add("chat_id", strconv.FormatInt(params.ChatID, 10)) + + if params.Caption != "" { + args.Add("caption", params.Caption) + } + + if params.ReplyMarkup != nil { + dst, err := json.Marshal(params.ReplyMarkup) + if err != nil { + return nil, err + } + args.Add("reply_markup", string(dst)) + } + + if params.ReplyToMessageID != 0 { + args.Add("reply_to_message_id", strconv.Itoa(params.ReplyToMessageID)) + } + + args.Add("disable_notification", strconv.FormatBool(params.DisableNotification)) + + resp, err := bot.upload(params.Photo, "photo", "", "sendPhoto", args) + if err != nil { + return nil, err + } + + var data Message + err = json.Unmarshal(*resp.Result, &data) + return &data, err +}