all repos — telegram-bot-api @ 8d14bd7a5608c3a9fb7459c37893dc977d968f4f

Golang bindings for the Telegram Bot API

Make MediaGroupConfig Chattable and Fileable.
Syfaro syfaro@huefox.com
Sun, 26 Jul 2020 14:40:12 -0500
commit

8d14bd7a5608c3a9fb7459c37893dc977d968f4f

parent

c6bf64c67d2d1002b7fbec45608fb914d176616c

3 files changed, 52 insertions(+), 48 deletions(-)

jump to
M bot.gobot.go

@@ -377,51 +377,7 @@ }

// 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 }
M configs.goconfigs.go

@@ -1,6 +1,7 @@

package tgbotapi import ( + "fmt" "io" "net/url" "strconv"

@@ -1694,9 +1695,6 @@

// 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,7 +1715,56 @@ params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)

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.
M types_test.gotypes_test.go

@@ -348,4 +348,5 @@ _ Fileable = (*SetChatPhotoConfig)(nil)

_ Fileable = (*UploadStickerConfig)(nil) _ Fileable = (*NewStickerSetConfig)(nil) _ Fileable = (*AddStickerConfig)(nil) + _ Fileable = (*MediaGroupConfig)(nil) )