all repos — telegram-bot-api @ b163052f82f70d259ba53759bd3470286201b479

Golang bindings for the Telegram Bot API

Handle InputMedia{Document,Audio} in media groups.
Syfaro syfaro@huefox.com
Sat, 20 Feb 2021 13:49:00 -0500
commit

b163052f82f70d259ba53759bd3470286201b479

parent

f2cd95670dcea2b0416090fcffab9e4940cce341

4 files changed, 110 insertions(+), 6 deletions(-)

jump to
M bot_test.gobot_test.go

@@ -532,13 +532,57 @@

bot.Request(RemoveWebhookConfig{}) } -func TestSendWithMediaGroup(t *testing.T) { +func TestSendWithMediaGroupPhotoVideo(t *testing.T) { bot, _ := getBot(t) cfg := NewMediaGroup(ChatID, []interface{}{ NewInputMediaPhoto(FileURL("https://i.imgur.com/unQLJIb.jpg")), NewInputMediaPhoto("tests/image.jpg"), NewInputMediaVideo("tests/video.mp4"), + }) + + 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 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)
M configs.goconfigs.go

@@ -1806,6 +1806,30 @@ 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 nil

@@ -1844,6 +1868,38 @@ switch f := m.Thumb.(type) {

case string, FileBytes, FileReader: files = append(files, RequestFile{ Name: fmt.Sprintf("file-%d-thumb", idx), + 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, }) }
M helpers.gohelpers.go

@@ -201,7 +201,7 @@ }

} // NewInputMediaDocument creates a new InputMediaDocument. -func NewInputMediaDocument(media string) InputMediaDocument { +func NewInputMediaDocument(media interface{}) InputMediaDocument { return InputMediaDocument{ BaseInputMedia: BaseInputMedia{ Type: "document",
M types.gotypes.go

@@ -1112,10 +1112,11 @@ }

// BaseInputMedia is a base type for the InputMedia types. type BaseInputMedia struct { - Type string `json:"type"` - Media interface{} `json:"media"` - Caption string `json:"caption,omitempty"` - ParseMode string `json:"parse_mode,omitempty"` + 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"` } // InputMediaPhoto is a photo to send as part of a media group.

@@ -1144,6 +1145,7 @@

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

// 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.