Handle InputMedia{Document,Audio} in media groups.

bot-api-6.1
Syfaro 2021-02-20 13:49:00 -05:00
parent f2cd95670d
commit b163052f82
4 changed files with 110 additions and 6 deletions

View File

@ -532,7 +532,7 @@ func TestSetWebhookWithoutCert(t *testing.T) {
bot.Request(RemoveWebhookConfig{}) bot.Request(RemoveWebhookConfig{})
} }
func TestSendWithMediaGroup(t *testing.T) { func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
bot, _ := getBot(t) bot, _ := getBot(t)
cfg := NewMediaGroup(ChatID, []interface{}{ cfg := NewMediaGroup(ChatID, []interface{}{
@ -555,6 +555,50 @@ func TestSendWithMediaGroup(t *testing.T) {
} }
} }
func TestSendWithMediaGroupDocument(t *testing.T) {
bot, _ := getBot(t)
cfg := NewMediaGroup(ChatID, []interface{}{
NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
NewInputMediaDocument("tests/image.jpg"),
})
messages, err := bot.SendMediaGroup(cfg)
if err != nil {
t.Error(err)
}
if messages == nil {
t.Error("No received messages")
}
if len(messages) != len(cfg.Media) {
t.Errorf("Different number of messages: %d", len(messages))
}
}
func TestSendWithMediaGroupAudio(t *testing.T) {
bot, _ := getBot(t)
cfg := NewMediaGroup(ChatID, []interface{}{
NewInputMediaAudio("tests/audio.mp3"),
NewInputMediaAudio("tests/audio.mp3"),
})
messages, err := bot.SendMediaGroup(cfg)
if err != nil {
t.Error(err)
}
if messages == nil {
t.Error("No received messages")
}
if len(messages) != len(cfg.Media) {
t.Errorf("Different number of messages: %d", len(messages))
}
}
func ExampleNewBotAPI() { func ExampleNewBotAPI() {
bot, err := NewBotAPI("MyAwesomeBotToken") bot, err := NewBotAPI("MyAwesomeBotToken")
if err != nil { if err != nil {

View File

@ -1805,6 +1805,30 @@ func prepareInputMediaParam(inputMedia interface{}, idx int) interface{} {
m.Thumb = fmt.Sprintf("attach://file-%d-thumb", idx) m.Thumb = fmt.Sprintf("attach://file-%d-thumb", idx)
} }
return m
case InputMediaAudio:
switch m.Media.(type) {
case string, FileBytes, FileReader:
m.Media = fmt.Sprintf("attach://file-%d", idx)
}
switch m.Thumb.(type) {
case string, FileBytes, FileReader:
m.Thumb = fmt.Sprintf("attach://file-%d-thumb", idx)
}
return m
case InputMediaDocument:
switch m.Media.(type) {
case string, FileBytes, FileReader:
m.Media = fmt.Sprintf("attach://file-%d", idx)
}
switch m.Thumb.(type) {
case string, FileBytes, FileReader:
m.Thumb = fmt.Sprintf("attach://file-%d-thumb", idx)
}
return m return m
} }
@ -1847,6 +1871,38 @@ func prepareInputMediaFile(inputMedia interface{}, idx int) []RequestFile {
File: f, File: f,
}) })
} }
case InputMediaDocument:
switch f := m.Media.(type) {
case string, FileBytes, FileReader:
files = append(files, RequestFile{
Name: fmt.Sprintf("file-%d", idx),
File: f,
})
}
switch f := m.Thumb.(type) {
case string, FileBytes, FileReader:
files = append(files, RequestFile{
Name: fmt.Sprintf("file-%d", idx),
File: f,
})
}
case InputMediaAudio:
switch f := m.Media.(type) {
case string, FileBytes, FileReader:
files = append(files, RequestFile{
Name: fmt.Sprintf("file-%d", idx),
File: f,
})
}
switch f := m.Thumb.(type) {
case string, FileBytes, FileReader:
files = append(files, RequestFile{
Name: fmt.Sprintf("file-%d", idx),
File: f,
})
}
} }
return files return files

View File

@ -201,7 +201,7 @@ func NewInputMediaAudio(media interface{}) InputMediaAudio {
} }
// NewInputMediaDocument creates a new InputMediaDocument. // NewInputMediaDocument creates a new InputMediaDocument.
func NewInputMediaDocument(media string) InputMediaDocument { func NewInputMediaDocument(media interface{}) InputMediaDocument {
return InputMediaDocument{ return InputMediaDocument{
BaseInputMedia: BaseInputMedia{ BaseInputMedia: BaseInputMedia{
Type: "document", Type: "document",

View File

@ -1112,10 +1112,11 @@ type BotCommand struct {
// BaseInputMedia is a base type for the InputMedia types. // BaseInputMedia is a base type for the InputMedia types.
type BaseInputMedia struct { type BaseInputMedia struct {
Type string `json:"type"` Type string `json:"type"`
Media interface{} `json:"media"` Media interface{} `json:"media"`
Caption string `json:"caption,omitempty"` Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"` CaptionEntities []MessageEntity `json:"caption_entities,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
} }
// InputMediaPhoto is a photo to send as part of a media group. // InputMediaPhoto is a photo to send as part of a media group.
@ -1144,6 +1145,7 @@ type InputMediaAnimation struct {
// InputMediaAudio is a audio to send as part of a media group. // InputMediaAudio is a audio to send as part of a media group.
type InputMediaAudio struct { type InputMediaAudio struct {
BaseInputMedia BaseInputMedia
Thumb interface{}
Duration int `json:"duration"` Duration int `json:"duration"`
Performer string `json:"performer"` Performer string `json:"performer"`
Title string `json:"title"` Title string `json:"title"`
@ -1152,6 +1154,8 @@ type InputMediaAudio struct {
// InputMediaDocument is a audio to send as part of a media group. // InputMediaDocument is a audio to send as part of a media group.
type InputMediaDocument struct { type InputMediaDocument struct {
BaseInputMedia BaseInputMedia
Thumb interface{}
DisableContentTypeDetection bool `json:"disable_content_type_detection,omitempty"`
} }
// Error is an error containing extra information returned by the Telegram API. // Error is an error containing extra information returned by the Telegram API.