Skip to content

Commit 915dfba

Browse files
committed
bot: add audio and document for sendMediaGroup
1 parent 03976a5 commit 915dfba

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed

api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ func (b *Bot) sendFiles(method string, files map[string]File, params map[string]
8383

8484
pipeReader, pipeWriter := io.Pipe()
8585
writer := multipart.NewWriter(pipeWriter)
86+
8687
go func() {
8788
defer pipeWriter.Close()
8889

8990
for field, file := range rawFiles {
90-
if err := addFileToWriter(writer, params["file_name"], field, file); err != nil {
91+
if err := addFileToWriter(writer, files[field].FileName, field, file); err != nil {
9192
pipeWriter.CloseWithError(err)
9293
return
9394
}

bot.go

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ func (b *Bot) ProcessUpdate(upd Update) {
557557

558558
return
559559
}
560-
561560
}
562561

563562
func (b *Bot) handle(end string, m *Message) bool {
@@ -645,6 +644,8 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
645644
return nil, ErrBadRecipient
646645
}
647646

647+
sendOpts := extractOptions(options)
648+
648649
media := make([]string, len(a))
649650
files := make(map[string]File)
650651

@@ -670,15 +671,15 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
670671
switch y := x.(type) {
671672
case *Photo:
672673
data, _ = json.Marshal(struct {
673-
Type string `json:"type"`
674-
Media string `json:"media"`
675-
Caption string `json:"caption,omitempty"`
676-
ParseMode ParseMode `json:"parse_mode,omitempty"`
674+
Type string `json:"type"`
675+
Media string `json:"media"`
676+
Caption string `json:"caption,omitempty"`
677+
ParseMode string `json:"parse_mode,omitempty"`
677678
}{
678679
Type: "photo",
679680
Media: repr,
680681
Caption: y.Caption,
681-
ParseMode: y.ParseMode,
682+
ParseMode: sendOpts.ParseMode,
682683
})
683684
case *Video:
684685
data, _ = json.Marshal(struct {
@@ -689,6 +690,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
689690
Height int `json:"height,omitempty"`
690691
Duration int `json:"duration,omitempty"`
691692
SupportsStreaming bool `json:"supports_streaming,omitempty"`
693+
ParseMode string `json:"parse_mode,omitempty"`
692694
}{
693695
Type: "video",
694696
Caption: y.Caption,
@@ -697,6 +699,37 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
697699
Height: y.Height,
698700
Duration: y.Duration,
699701
SupportsStreaming: y.SupportsStreaming,
702+
ParseMode: sendOpts.ParseMode,
703+
})
704+
case *Audio:
705+
data, _ = json.Marshal(struct {
706+
Type string `json:"type"`
707+
Media string `json:"media"`
708+
Caption string `json:"caption,omitempty"`
709+
Duration int `json:"duration,omitempty"`
710+
Performer string `json:"performer,omitempty"`
711+
Title string `json:"title,omitempty"`
712+
ParseMode string `json:"parse_mode,omitempty"`
713+
}{
714+
Type: "audio",
715+
Media: repr,
716+
Caption: y.Caption,
717+
Duration: y.Duration,
718+
Performer: y.Performer,
719+
Title: y.Title,
720+
ParseMode: sendOpts.ParseMode,
721+
})
722+
case *Document:
723+
data, _ = json.Marshal(struct {
724+
Type string `json:"type"`
725+
Media string `json:"media"`
726+
Caption string `json:"caption,omitempty"`
727+
ParseMode string `json:"parse_mode,omitempty"`
728+
}{
729+
Type: "document",
730+
Media: repr,
731+
Caption: y.Caption,
732+
ParseMode: sendOpts.ParseMode,
700733
})
701734
default:
702735
return nil, errors.Errorf("telebot: album entry #%d is not valid", i)
@@ -709,8 +742,6 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
709742
"chat_id": to.Recipient(),
710743
"media": "[" + strings.Join(media, ",") + "]",
711744
}
712-
713-
sendOpts := extractOptions(options)
714745
b.embedSendOptions(params, sendOpts)
715746

716747
data, err := b.sendFiles("sendMediaGroup", files, params)
@@ -727,12 +758,18 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
727758

728759
for attachName := range files {
729760
i, _ := strconv.Atoi(attachName)
761+
r := resp.Result[i]
730762

731763
var newID string
732-
if resp.Result[i].Photo != nil {
733-
newID = resp.Result[i].Photo.FileID
734-
} else {
735-
newID = resp.Result[i].Video.FileID
764+
switch {
765+
case r.Photo != nil:
766+
newID = r.Photo.FileID
767+
case r.Video != nil:
768+
newID = r.Video.FileID
769+
case r.Audio != nil:
770+
newID = r.Audio.FileID
771+
case r.Document != nil:
772+
newID = r.Document.FileID
736773
}
737774

738775
a[i].MediaFile().FileID = newID

media.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ type InputMedia interface {
2222
type Photo struct {
2323
File
2424

25-
Width int `json:"width"`
26-
Height int `json:"height"`
27-
28-
// (Optional)
29-
Caption string `json:"caption,omitempty"`
30-
ParseMode ParseMode `json:"parse_mode,omitempty"`
25+
Width int `json:"width"`
26+
Height int `json:"height"`
27+
Caption string `json:"caption,omitempty"`
3128
}
3229

3330
type photoSize struct {
@@ -57,7 +54,6 @@ func (p *Photo) UnmarshalJSON(jsonStr []byte) error {
5754
}
5855
} else {
5956
var sizes []photoSize
60-
6157
if err := json.Unmarshal(jsonStr, &sizes); err != nil {
6258
return err
6359
}
@@ -136,9 +132,8 @@ func (v *Video) MediaFile() *File {
136132
type Animation struct {
137133
File
138134

139-
Width int `json:"width"`
140-
Height int `json:"height"`
141-
135+
Width int `json:"width"`
136+
Height int `json:"height"`
142137
Duration int `json:"duration,omitempty"`
143138

144139
// (Optional)
@@ -198,12 +193,12 @@ type Location struct {
198193
// Horizontal Accuracy
199194
HorizontalAccuracy *float32 `json:"horizontal_accuracy,omitempty"`
200195

201-
Heading int `json:"heading,omitempty"`
202-
203196
// Period in seconds for which the location will be updated
204197
// (see Live Locations, should be between 60 and 86400.)
205198
LivePeriod int `json:"live_period,omitempty"`
206199

200+
Heading int `json:"heading,omitempty"`
201+
207202
ProximityAlertRadius int `json:"proximity_alert_radius,omitempty"`
208203
}
209204

sendable.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
5151
"caption": a.Caption,
5252
"performer": a.Performer,
5353
"title": a.Title,
54-
"file_name": a.FileName,
5554
}
5655
b.embedSendOptions(params, opt)
5756

5857
if a.Duration != 0 {
5958
params["duration"] = strconv.Itoa(a.Duration)
6059
}
6160

62-
msg, err := b.sendObject(&a.File, "audio", params, thumbnailToFilemap(a.Thumbnail))
61+
msg, err := b.sendObject(a.MediaFile(), "audio", params, thumbnailToFilemap(a.Thumbnail))
6362
if err != nil {
6463
return nil, err
6564
}
@@ -81,17 +80,16 @@ func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
8180
// Send delivers media through bot b to recipient.
8281
func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
8382
params := map[string]string{
84-
"chat_id": to.Recipient(),
85-
"caption": d.Caption,
86-
"file_name": d.FileName,
83+
"chat_id": to.Recipient(),
84+
"caption": d.Caption,
8785
}
8886
b.embedSendOptions(params, opt)
8987

9088
if d.FileSize != 0 {
9189
params["file_size"] = strconv.Itoa(d.FileSize)
9290
}
9391

94-
msg, err := b.sendObject(&d.File, "document", params, thumbnailToFilemap(d.Thumbnail))
92+
msg, err := b.sendObject(d.MediaFile(), "document", params, thumbnailToFilemap(d.Thumbnail))
9593
if err != nil {
9694
return nil, err
9795
}
@@ -124,9 +122,8 @@ func (s *Sticker) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error)
124122
// Send delivers media through bot b to recipient.
125123
func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
126124
params := map[string]string{
127-
"chat_id": to.Recipient(),
128-
"caption": v.Caption,
129-
"file_name": v.FileName,
125+
"chat_id": to.Recipient(),
126+
"caption": v.Caption,
130127
}
131128
b.embedSendOptions(params, opt)
132129

@@ -143,7 +140,7 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
143140
params["supports_streaming"] = "true"
144141
}
145142

146-
msg, err := b.sendObject(&v.File, "video", params, thumbnailToFilemap(v.Thumbnail))
143+
msg, err := b.sendObject(v.MediaFile(), "video", params, thumbnailToFilemap(v.Thumbnail))
147144
if err != nil {
148145
return nil, err
149146
}
@@ -165,12 +162,10 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
165162
}
166163

167164
// Send delivers animation through bot b to recipient.
168-
// @see https://core.telegram.org/bots/api#sendanimation
169165
func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
170166
params := map[string]string{
171-
"chat_id": to.Recipient(),
172-
"caption": a.Caption,
173-
"file_name": a.FileName,
167+
"chat_id": to.Recipient(),
168+
"caption": a.Caption,
174169
}
175170
b.embedSendOptions(params, opt)
176171

@@ -184,12 +179,12 @@ func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro
184179
params["height"] = strconv.Itoa(a.Height)
185180
}
186181

187-
// file_name is required, without file_name GIFs sent as document
188-
if params["file_name"] == "" && a.File.OnDisk() {
189-
params["file_name"] = filepath.Base(a.File.FileLocal)
182+
// Without the FileName GIF sends as a document.
183+
if a.FileName == "" && a.File.OnDisk() {
184+
a.FileName = filepath.Base(a.File.FileLocal)
190185
}
191186

192-
msg, err := b.sendObject(&a.File, "animation", params, nil)
187+
msg, err := b.sendObject(a.MediaFile(), "animation", params, nil)
193188
if err != nil {
194189
return nil, err
195190
}

0 commit comments

Comments
 (0)