|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/telegram/message" |
| 7 | + "github.com/gotd/td/telegram/message/styling" |
| 8 | + "github.com/gotd/td/tg" |
| 9 | +) |
| 10 | + |
| 11 | +// liveVideoDocument resolves the video component of a live photo into an MTProto |
| 12 | +// input document. An upload is finalized through messages.uploadMedia; a file_id |
| 13 | +// references an existing document. URLs are not supported. |
| 14 | +func (b *Bot) liveVideoDocument(ctx context.Context, file InputFile) (tg.InputDocumentClass, error) { |
| 15 | + switch f := file.(type) { |
| 16 | + case InputFileID: |
| 17 | + return inputDocumentFromFileID(string(f)) |
| 18 | + case *InputFileUpload: |
| 19 | + uploaded, err := b.uploadInputFile(ctx, f) |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + |
| 24 | + media, err := b.uploadMediaRef(ctx, &tg.InputMediaUploadedDocument{ |
| 25 | + File: uploaded, |
| 26 | + MimeType: mimeVideoMP4, |
| 27 | + Attributes: []tg.DocumentAttributeClass{&tg.DocumentAttributeVideo{}}, |
| 28 | + }) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + doc, ok := media.(*tg.InputMediaDocument) |
| 34 | + if !ok { |
| 35 | + return nil, &Error{Code: 500, Description: "Internal Server Error: unexpected live photo video media"} |
| 36 | + } |
| 37 | + |
| 38 | + return doc.ID, nil |
| 39 | + default: |
| 40 | + return nil, &Error{Code: 400, Description: "Bad Request: live photo video must be an uploaded file or file_id"} |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// SendLivePhoto sends a live photo (an Apple-style still image paired with a short |
| 45 | +// video). The still photo must be an uploaded file; livePhoto is its video, an |
| 46 | +// uploaded file or a file_id. |
| 47 | +func (b *Bot) SendLivePhoto( |
| 48 | + ctx context.Context, chat ChatID, photo, livePhoto InputFile, caption string, opts ...SendOption, |
| 49 | +) (*Message, error) { |
| 50 | + up, ok := photo.(*InputFileUpload) |
| 51 | + if !ok { |
| 52 | + return nil, &Error{Code: 400, Description: "Bad Request: live photo still must be an uploaded file"} |
| 53 | + } |
| 54 | + |
| 55 | + return b.sendResolvedMedia(ctx, chat, caption, func(ctx context.Context, c []styling.StyledTextOption) (message.MediaOption, error) { |
| 56 | + photoFile, err := b.uploadInputFile(ctx, up) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + video, err := b.liveVideoDocument(ctx, livePhoto) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + media := &tg.InputMediaUploadedPhoto{ |
| 67 | + File: photoFile, |
| 68 | + Video: video, |
| 69 | + LivePhoto: true, |
| 70 | + } |
| 71 | + |
| 72 | + return message.Media(media, c...), nil |
| 73 | + }, opts...) |
| 74 | +} |
0 commit comments