Handle InputMedia{Document,Audio} in media groups.
parent
f2cd95670d
commit
b163052f82
46
bot_test.go
46
bot_test.go
|
@ -532,7 +532,7 @@ func TestSetWebhookWithoutCert(t *testing.T) {
|
|||
bot.Request(RemoveWebhookConfig{})
|
||||
}
|
||||
|
||||
func TestSendWithMediaGroup(t *testing.T) {
|
||||
func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
|
||||
bot, _ := getBot(t)
|
||||
|
||||
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() {
|
||||
bot, err := NewBotAPI("MyAwesomeBotToken")
|
||||
if err != nil {
|
||||
|
|
56
configs.go
56
configs.go
|
@ -1805,6 +1805,30 @@ func prepareInputMediaParam(inputMedia interface{}, idx int) interface{} {
|
|||
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
|
||||
}
|
||||
|
||||
|
@ -1847,6 +1871,38 @@ func prepareInputMediaFile(inputMedia interface{}, idx int) []RequestFile {
|
|||
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
|
||||
|
|
|
@ -201,7 +201,7 @@ func NewInputMediaAudio(media interface{}) InputMediaAudio {
|
|||
}
|
||||
|
||||
// NewInputMediaDocument creates a new InputMediaDocument.
|
||||
func NewInputMediaDocument(media string) InputMediaDocument {
|
||||
func NewInputMediaDocument(media interface{}) InputMediaDocument {
|
||||
return InputMediaDocument{
|
||||
BaseInputMedia: BaseInputMedia{
|
||||
Type: "document",
|
||||
|
|
4
types.go
4
types.go
|
@ -1115,6 +1115,7 @@ type BaseInputMedia struct {
|
|||
Type string `json:"type"`
|
||||
Media interface{} `json:"media"`
|
||||
Caption string `json:"caption,omitempty"`
|
||||
CaptionEntities []MessageEntity `json:"caption_entities,omitempty"`
|
||||
ParseMode string `json:"parse_mode,omitempty"`
|
||||
}
|
||||
|
||||
|
@ -1144,6 +1145,7 @@ type InputMediaAnimation struct {
|
|||
// InputMediaAudio is a audio to send as part of a media group.
|
||||
type InputMediaAudio struct {
|
||||
BaseInputMedia
|
||||
Thumb interface{}
|
||||
Duration int `json:"duration"`
|
||||
Performer string `json:"performer"`
|
||||
Title string `json:"title"`
|
||||
|
@ -1152,6 +1154,8 @@ type InputMediaAudio struct {
|
|||
// InputMediaDocument is a audio to send as part of a media group.
|
||||
type InputMediaDocument struct {
|
||||
BaseInputMedia
|
||||
Thumb interface{}
|
||||
DisableContentTypeDetection bool `json:"disable_content_type_detection,omitempty"`
|
||||
}
|
||||
|
||||
// Error is an error containing extra information returned by the Telegram API.
|
||||
|
|
Loading…
Reference in New Issue