Make MediaGroupConfig Chattable and Fileable.
parent
c6bf64c67d
commit
8d14bd7a56
46
bot.go
46
bot.go
|
@ -377,51 +377,7 @@ func (bot *BotAPI) Send(c Chattable) (Message, error) {
|
||||||
|
|
||||||
// SendMediaGroup sends a media group and returns the resulting messages.
|
// SendMediaGroup sends a media group and returns the resulting messages.
|
||||||
func (bot *BotAPI) SendMediaGroup(config MediaGroupConfig) ([]Message, error) {
|
func (bot *BotAPI) SendMediaGroup(config MediaGroupConfig) ([]Message, error) {
|
||||||
filesToUpload := []RequestFile{}
|
resp, err := bot.Request(config)
|
||||||
|
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
53
configs.go
53
configs.go
|
@ -1,6 +1,7 @@
|
||||||
package tgbotapi
|
package tgbotapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -1694,9 +1695,6 @@ func (config DeleteChatStickerSetConfig) params() (Params, error) {
|
||||||
// MediaGroupConfig allows you to send a group of media.
|
// MediaGroupConfig allows you to send a group of media.
|
||||||
//
|
//
|
||||||
// Media consist of InputMedia items (InputMediaPhoto, InputMediaVideo).
|
// 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 {
|
type MediaGroupConfig struct {
|
||||||
ChatID int64
|
ChatID int64
|
||||||
ChannelUsername string
|
ChannelUsername string
|
||||||
|
@ -1717,9 +1715,58 @@ func (config MediaGroupConfig) params() (Params, error) {
|
||||||
params.AddBool("disable_notification", config.DisableNotification)
|
params.AddBool("disable_notification", config.DisableNotification)
|
||||||
params.AddNonZero("reply_to_message_id", config.ReplyToMessageID)
|
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
|
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.
|
// DiceConfig allows you to send a random dice roll to Telegram.
|
||||||
//
|
//
|
||||||
// Emoji may be one of the following: 🎲 (1-6), 🎯 (1-6), 🏀 (1-5).
|
// Emoji may be one of the following: 🎲 (1-6), 🎯 (1-6), 🏀 (1-5).
|
||||||
|
|
|
@ -348,4 +348,5 @@ var (
|
||||||
_ Fileable = (*UploadStickerConfig)(nil)
|
_ Fileable = (*UploadStickerConfig)(nil)
|
||||||
_ Fileable = (*NewStickerSetConfig)(nil)
|
_ Fileable = (*NewStickerSetConfig)(nil)
|
||||||
_ Fileable = (*AddStickerConfig)(nil)
|
_ Fileable = (*AddStickerConfig)(nil)
|
||||||
|
_ Fileable = (*MediaGroupConfig)(nil)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue