|
| 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 | +) |
| 9 | + |
| 10 | +// typedMedia resolves an InputFile into a typed document media option. For local |
| 11 | +// uploads it applies the typed attribute builder (video/audio/...); for file_id |
| 12 | +// and URL inputs it falls back to a plain document, since the stored document |
| 13 | +// already carries its type. |
| 14 | +func (b *Bot) typedMedia( |
| 15 | + ctx context.Context, |
| 16 | + file InputFile, |
| 17 | + caption []styling.StyledTextOption, |
| 18 | + typed func(*message.UploadedDocumentBuilder) message.MediaOption, |
| 19 | +) (message.MediaOption, error) { |
| 20 | + up, ok := file.(*InputFileUpload) |
| 21 | + if !ok { |
| 22 | + return b.documentMedia(ctx, file, caption) |
| 23 | + } |
| 24 | + f, err := b.uploadInputFile(ctx, up) |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + doc := message.UploadedDocument(f, caption...) |
| 29 | + if up.Name != "" { |
| 30 | + doc = doc.Filename(up.Name) |
| 31 | + } |
| 32 | + return typed(doc), nil |
| 33 | +} |
| 34 | + |
| 35 | +// SendVideo sends a video from a file_id, URL or local upload. |
| 36 | +func (b *Bot) SendVideo(ctx context.Context, chat ChatID, video InputFile, caption string, opts ...SendOption) (*Message, error) { |
| 37 | + return b.sendResolvedMedia(ctx, chat, caption, func(ctx context.Context, c []styling.StyledTextOption) (message.MediaOption, error) { |
| 38 | + return b.typedMedia(ctx, video, c, func(d *message.UploadedDocumentBuilder) message.MediaOption { return d.Video() }) |
| 39 | + }, opts...) |
| 40 | +} |
| 41 | + |
| 42 | +// SendAnimation sends an animation (GIF or silent video) from a file_id, URL or |
| 43 | +// local upload. |
| 44 | +func (b *Bot) SendAnimation(ctx context.Context, chat ChatID, animation InputFile, caption string, opts ...SendOption) (*Message, error) { |
| 45 | + return b.sendResolvedMedia(ctx, chat, caption, func(ctx context.Context, c []styling.StyledTextOption) (message.MediaOption, error) { |
| 46 | + return b.typedMedia(ctx, animation, c, func(d *message.UploadedDocumentBuilder) message.MediaOption { return d.GIF() }) |
| 47 | + }, opts...) |
| 48 | +} |
| 49 | + |
| 50 | +// SendAudio sends an audio file (music) from a file_id, URL or local upload. |
| 51 | +func (b *Bot) SendAudio(ctx context.Context, chat ChatID, audio InputFile, caption string, opts ...SendOption) (*Message, error) { |
| 52 | + return b.sendResolvedMedia(ctx, chat, caption, func(ctx context.Context, c []styling.StyledTextOption) (message.MediaOption, error) { |
| 53 | + return b.typedMedia(ctx, audio, c, func(d *message.UploadedDocumentBuilder) message.MediaOption { return d.Audio() }) |
| 54 | + }, opts...) |
| 55 | +} |
| 56 | + |
| 57 | +// SendVoice sends a voice note from a file_id, URL or local upload. |
| 58 | +func (b *Bot) SendVoice(ctx context.Context, chat ChatID, voice InputFile, caption string, opts ...SendOption) (*Message, error) { |
| 59 | + return b.sendResolvedMedia(ctx, chat, caption, func(ctx context.Context, c []styling.StyledTextOption) (message.MediaOption, error) { |
| 60 | + return b.typedMedia(ctx, voice, c, func(d *message.UploadedDocumentBuilder) message.MediaOption { return d.Voice() }) |
| 61 | + }, opts...) |
| 62 | +} |
| 63 | + |
| 64 | +// SendVideoNote sends a rounded square video message from a file_id or local |
| 65 | +// upload. |
| 66 | +func (b *Bot) SendVideoNote(ctx context.Context, chat ChatID, videoNote InputFile, opts ...SendOption) (*Message, error) { |
| 67 | + return b.sendResolvedMedia(ctx, chat, "", func(ctx context.Context, c []styling.StyledTextOption) (message.MediaOption, error) { |
| 68 | + return b.typedMedia(ctx, videoNote, c, func(d *message.UploadedDocumentBuilder) message.MediaOption { return d.RoundVideo() }) |
| 69 | + }, opts...) |
| 70 | +} |
| 71 | + |
| 72 | +// SendSticker sends a sticker from a file_id, URL or local upload. |
| 73 | +func (b *Bot) SendSticker(ctx context.Context, chat ChatID, sticker InputFile, opts ...SendOption) (*Message, error) { |
| 74 | + return b.sendResolvedMedia(ctx, chat, "", func(ctx context.Context, c []styling.StyledTextOption) (message.MediaOption, error) { |
| 75 | + return b.typedMedia(ctx, sticker, c, func(d *message.UploadedDocumentBuilder) message.MediaOption { return d.UploadedSticker() }) |
| 76 | + }, opts...) |
| 77 | +} |
0 commit comments