From 8d14bd7a5608c3a9fb7459c37893dc977d968f4f Mon Sep 17 00:00:00 2001 From: Syfaro Date: Sun, 26 Jul 2020 14:40:12 -0500 Subject: [PATCH] Make MediaGroupConfig Chattable and Fileable. --- bot.go | 46 +------------------------------------------- configs.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++--- types_test.go | 1 + 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/bot.go b/bot.go index 4bd2fba..d44ae54 100644 --- a/bot.go +++ b/bot.go @@ -377,51 +377,7 @@ func (bot *BotAPI) Send(c Chattable) (Message, error) { // SendMediaGroup sends a media group and returns the resulting messages. func (bot *BotAPI) SendMediaGroup(config MediaGroupConfig) ([]Message, error) { - filesToUpload := []RequestFile{} - - newMedia := []interface{}{} - - for idx, media := range config.Media { - switch m := media.(type) { - case InputMediaPhoto: - switch f := m.Media.(type) { - case string, FileBytes, FileReader: - m.Media = fmt.Sprintf("attach://file-%d", idx) - newMedia = append(newMedia, m) - - filesToUpload = append(filesToUpload, RequestFile{ - Name: fmt.Sprintf("file-%d", idx), - File: f, - }) - default: - newMedia = append(newMedia, m) - } - case InputMediaVideo: - switch f := m.Media.(type) { - case string, FileBytes, FileReader: - m.Media = fmt.Sprintf("attach://file-%d", idx) - newMedia = append(newMedia, m) - - filesToUpload = append(filesToUpload, RequestFile{ - Name: fmt.Sprintf("file-%d", idx), - File: f, - }) - default: - newMedia = append(newMedia, m) - } - default: - return nil, errors.New(ErrBadFileType) - } - } - - params, err := config.params() - if err != nil { - return nil, err - } - - params.AddInterface("media", newMedia) - - resp, err := bot.UploadFiles(config.method(), params, filesToUpload) + resp, err := bot.Request(config) if err != nil { return nil, err } diff --git a/configs.go b/configs.go index 495f9ac..36e5ad7 100644 --- a/configs.go +++ b/configs.go @@ -1,6 +1,7 @@ package tgbotapi import ( + "fmt" "io" "net/url" "strconv" @@ -1694,9 +1695,6 @@ func (config DeleteChatStickerSetConfig) params() (Params, error) { // MediaGroupConfig allows you to send a group of media. // // Media consist of InputMedia items (InputMediaPhoto, InputMediaVideo). -// -// Due to additional processing required, this config is not Chattable or -// Fileable. It must be uploaded with SendMediaGroup. type MediaGroupConfig struct { ChatID int64 ChannelUsername string @@ -1717,9 +1715,58 @@ func (config MediaGroupConfig) params() (Params, error) { params.AddBool("disable_notification", config.DisableNotification) params.AddNonZero("reply_to_message_id", config.ReplyToMessageID) + newMedia := make([]interface{}, len(config.Media)) + copy(newMedia, config.Media) + + for idx, media := range config.Media { + switch m := media.(type) { + case InputMediaPhoto: + switch m.Media.(type) { + case string, FileBytes, FileReader: + m.Media = fmt.Sprintf("attach://file-%d", idx) + newMedia[idx] = m + } + case InputMediaVideo: + switch m.Media.(type) { + case string, FileBytes, FileReader: + m.Media = fmt.Sprintf("attach://file-%d", idx) + newMedia[idx] = m + } + } + } + + params.AddInterface("media", newMedia) + return params, nil } +func (config MediaGroupConfig) files() []RequestFile { + files := []RequestFile{} + + for idx, media := range config.Media { + switch m := media.(type) { + case InputMediaPhoto: + switch f := m.Media.(type) { + case string, FileBytes, FileReader: + files = append(files, RequestFile{ + Name: fmt.Sprintf("file-%d", idx), + File: f, + }) + } + case InputMediaVideo: + switch f := m.Media.(type) { + case string, FileBytes, FileReader: + files = append(files, RequestFile{ + Name: fmt.Sprintf("file-%d", idx), + File: f, + }) + } + } + } + + return files +} + // DiceConfig allows you to send a random dice roll to Telegram. // // Emoji may be one of the following: 🎲 (1-6), 🎯 (1-6), 🏀 (1-5). diff --git a/types_test.go b/types_test.go index 46ec2d1..3b6c5be 100644 --- a/types_test.go +++ b/types_test.go @@ -348,4 +348,5 @@ var ( _ Fileable = (*UploadStickerConfig)(nil) _ Fileable = (*NewStickerSetConfig)(nil) _ Fileable = (*AddStickerConfig)(nil) + _ Fileable = (*MediaGroupConfig)(nil) )