Make MediaGroupConfig Chattable and Fileable.

bot-api-6.1
Syfaro 2020-07-26 14:40:12 -05:00
parent c6bf64c67d
commit 8d14bd7a56
3 changed files with 52 additions and 48 deletions

46
bot.go
View File

@ -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
}

View File

@ -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).

View File

@ -348,4 +348,5 @@ var (
_ Fileable = (*UploadStickerConfig)(nil)
_ Fileable = (*NewStickerSetConfig)(nil)
_ Fileable = (*AddStickerConfig)(nil)
_ Fileable = (*MediaGroupConfig)(nil)
)