diff --git a/bot.go b/bot.go index 46aa066..0c6f58e 100644 --- a/bot.go +++ b/bot.go @@ -725,6 +725,131 @@ func (b *Bot) EditCaption(originalMsg Editable, caption string) (*Message, error return extractMsgResponse(respJSON) } +// EditMedia used to edit already sent media with known recepient and message id. +// +// Use cases: +// +// bot.EditMedia(msg, &tb.Photo{File: tb.FromDisk("chicken.jpg")}); +// bot.EditMedia(msg, &tb.Video{File: tb.FromURL("http://video.mp4")}); +// +func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...interface{}) (*Message, error) { + var mediaRepr string; + var jsonRepr []byte; + var thumb *Photo; + + file := make(map[string]File); + + f := inputMedia.MediaFile(); + + if f.InCloud() { + mediaRepr = f.FileID; + } else if f.FileURL != "" { + mediaRepr = f.FileURL; + } else if f.OnDisk() || f.FileReader != nil { + s := f.FileLocal; + if (f.FileReader != nil) { + s = "0"; + } + mediaRepr = "attach://" + s; + file[s] = *f; + } else { + return nil, errors.Errorf( + "telebot: can't edit media, it doesn't exist anywhere"); + } + + type FileJson struct { + // All types. + Type string `json:"type"` + Caption string `json:"caption"` + Media string `json:"media"` + + // Video. + Width int `json:"width,omitempty"` + Height int `json:"height,omitempty"` + SupportsStreaming bool `json:"supports_streaming,omitempty"` + + // Video and audio. + Duration int `json:"duration,omitempty"` + + // Document. + FileName string `json:"file_name"` + + // Document, video and audio. + Thumbnail string `json:"thumb,omitempty"` + MIME string `json:"mime_type,omitempty"` + + // Audio. + Title string `json:"title,omitempty"` + Performer string `json:"performer,omitempty"` + } + + resultMedia := &FileJson {Media: mediaRepr}; + + switch y := inputMedia.(type) { + case *Photo: + resultMedia.Type = "photo"; + resultMedia.Caption = y.Caption; + case *Video: + resultMedia.Type = "video"; + resultMedia.Caption = y.Caption; + resultMedia.Width = y.Width; + resultMedia.Height = y.Height; + resultMedia.Duration = y.Duration; + resultMedia.SupportsStreaming = y.SupportsStreaming; + resultMedia.MIME = y.MIME; + thumb = y.Thumbnail; + if thumb != nil { + resultMedia.Thumbnail = "attach://thumb"; + } + case *Document: + resultMedia.Type = "document"; + resultMedia.Caption = y.Caption; + resultMedia.FileName = y.FileName; + resultMedia.MIME = y.MIME; + thumb = y.Thumbnail; + if thumb != nil { + resultMedia.Thumbnail = "attach://thumb"; + } + case *Audio: + resultMedia.Type = "audio"; + resultMedia.Caption = y.Caption; + resultMedia.Duration = y.Duration; + resultMedia.MIME = y.MIME; + resultMedia.Title = y.Title; + resultMedia.Performer = y.Performer; + default: + return nil, errors.Errorf("telebot: inputMedia entry is not valid"); + } + + messageID, chatID := message.MessageSig(); + + jsonRepr, _ = json.Marshal(resultMedia); + params := map[string]string{}; + params["media"] = string(jsonRepr); + + // If inline message. + if chatID == 0 { + params["inline_message_id"] = messageID; + } else { + params["chat_id"] = strconv.FormatInt(chatID, 10); + params["message_id"] = messageID; + } + + if thumb != nil { + file["thumb"] = *thumb.MediaFile(); + } + + sendOpts := extractOptions(options); + embedSendOptions(params, sendOpts); + + respJSON, err := b.sendFiles("editMessageMedia", file, params); + if err != nil { + return nil, err; + } + + return extractMsgResponse(respJSON); +} + // Delete removes the message, including service messages, // with the following limitations: // diff --git a/media.go b/media.go index 07c0267..e7069b2 100644 --- a/media.go +++ b/media.go @@ -85,6 +85,11 @@ type Audio struct { MIME string `json:"mime_type,omitempty"` } +// MediaFile returns &Audio.File +func (a *Audio) MediaFile() *File { + return &a.File +} + // Document object represents a general file (as opposed to Photo or Audio). // Telegram users can send files of any type of up to 1.5 GB in size. type Document struct { @@ -99,6 +104,11 @@ type Document struct { MIME string `json:"mime_type"` } +// MediaFile returns &Document.File +func (d *Document) MediaFile() *File { + return &d.File +} + // Video object represents a video file. type Video struct { File