bot: add audio and document for sendMediaGroup

pull/433/head
Demian 3 years ago
parent 03976a572d
commit 915dfba6a4

@ -83,11 +83,12 @@ func (b *Bot) sendFiles(method string, files map[string]File, params map[string]
pipeReader, pipeWriter := io.Pipe()
writer := multipart.NewWriter(pipeWriter)
go func() {
defer pipeWriter.Close()
for field, file := range rawFiles {
if err := addFileToWriter(writer, params["file_name"], field, file); err != nil {
if err := addFileToWriter(writer, files[field].FileName, field, file); err != nil {
pipeWriter.CloseWithError(err)
return
}

@ -557,7 +557,6 @@ func (b *Bot) ProcessUpdate(upd Update) {
return
}
}
func (b *Bot) handle(end string, m *Message) bool {
@ -645,6 +644,8 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
return nil, ErrBadRecipient
}
sendOpts := extractOptions(options)
media := make([]string, len(a))
files := make(map[string]File)
@ -670,15 +671,15 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
switch y := x.(type) {
case *Photo:
data, _ = json.Marshal(struct {
Type string `json:"type"`
Media string `json:"media"`
Caption string `json:"caption,omitempty"`
ParseMode ParseMode `json:"parse_mode,omitempty"`
Type string `json:"type"`
Media string `json:"media"`
Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
}{
Type: "photo",
Media: repr,
Caption: y.Caption,
ParseMode: y.ParseMode,
ParseMode: sendOpts.ParseMode,
})
case *Video:
data, _ = json.Marshal(struct {
@ -689,6 +690,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
Height int `json:"height,omitempty"`
Duration int `json:"duration,omitempty"`
SupportsStreaming bool `json:"supports_streaming,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
}{
Type: "video",
Caption: y.Caption,
@ -697,6 +699,37 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
Height: y.Height,
Duration: y.Duration,
SupportsStreaming: y.SupportsStreaming,
ParseMode: sendOpts.ParseMode,
})
case *Audio:
data, _ = json.Marshal(struct {
Type string `json:"type"`
Media string `json:"media"`
Caption string `json:"caption,omitempty"`
Duration int `json:"duration,omitempty"`
Performer string `json:"performer,omitempty"`
Title string `json:"title,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
}{
Type: "audio",
Media: repr,
Caption: y.Caption,
Duration: y.Duration,
Performer: y.Performer,
Title: y.Title,
ParseMode: sendOpts.ParseMode,
})
case *Document:
data, _ = json.Marshal(struct {
Type string `json:"type"`
Media string `json:"media"`
Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
}{
Type: "document",
Media: repr,
Caption: y.Caption,
ParseMode: sendOpts.ParseMode,
})
default:
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
"chat_id": to.Recipient(),
"media": "[" + strings.Join(media, ",") + "]",
}
sendOpts := extractOptions(options)
b.embedSendOptions(params, sendOpts)
data, err := b.sendFiles("sendMediaGroup", files, params)
@ -727,12 +758,18 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
for attachName := range files {
i, _ := strconv.Atoi(attachName)
r := resp.Result[i]
var newID string
if resp.Result[i].Photo != nil {
newID = resp.Result[i].Photo.FileID
} else {
newID = resp.Result[i].Video.FileID
switch {
case r.Photo != nil:
newID = r.Photo.FileID
case r.Video != nil:
newID = r.Video.FileID
case r.Audio != nil:
newID = r.Audio.FileID
case r.Document != nil:
newID = r.Document.FileID
}
a[i].MediaFile().FileID = newID

@ -22,12 +22,9 @@ type InputMedia interface {
type Photo struct {
File
Width int `json:"width"`
Height int `json:"height"`
// (Optional)
Caption string `json:"caption,omitempty"`
ParseMode ParseMode `json:"parse_mode,omitempty"`
Width int `json:"width"`
Height int `json:"height"`
Caption string `json:"caption,omitempty"`
}
type photoSize struct {
@ -57,7 +54,6 @@ func (p *Photo) UnmarshalJSON(jsonStr []byte) error {
}
} else {
var sizes []photoSize
if err := json.Unmarshal(jsonStr, &sizes); err != nil {
return err
}
@ -136,9 +132,8 @@ func (v *Video) MediaFile() *File {
type Animation struct {
File
Width int `json:"width"`
Height int `json:"height"`
Width int `json:"width"`
Height int `json:"height"`
Duration int `json:"duration,omitempty"`
// (Optional)
@ -198,12 +193,12 @@ type Location struct {
// Horizontal Accuracy
HorizontalAccuracy *float32 `json:"horizontal_accuracy,omitempty"`
Heading int `json:"heading,omitempty"`
// Period in seconds for which the location will be updated
// (see Live Locations, should be between 60 and 86400.)
LivePeriod int `json:"live_period,omitempty"`
Heading int `json:"heading,omitempty"`
ProximityAlertRadius int `json:"proximity_alert_radius,omitempty"`
}

@ -51,7 +51,6 @@ func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
"caption": a.Caption,
"performer": a.Performer,
"title": a.Title,
"file_name": a.FileName,
}
b.embedSendOptions(params, opt)
@ -59,7 +58,7 @@ func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params["duration"] = strconv.Itoa(a.Duration)
}
msg, err := b.sendObject(&a.File, "audio", params, thumbnailToFilemap(a.Thumbnail))
msg, err := b.sendObject(a.MediaFile(), "audio", params, thumbnailToFilemap(a.Thumbnail))
if err != nil {
return nil, err
}
@ -81,9 +80,8 @@ func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
// Send delivers media through bot b to recipient.
func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
"caption": d.Caption,
"file_name": d.FileName,
"chat_id": to.Recipient(),
"caption": d.Caption,
}
b.embedSendOptions(params, opt)
@ -91,7 +89,7 @@ func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error
params["file_size"] = strconv.Itoa(d.FileSize)
}
msg, err := b.sendObject(&d.File, "document", params, thumbnailToFilemap(d.Thumbnail))
msg, err := b.sendObject(d.MediaFile(), "document", params, thumbnailToFilemap(d.Thumbnail))
if err != nil {
return nil, err
}
@ -124,9 +122,8 @@ func (s *Sticker) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error)
// Send delivers media through bot b to recipient.
func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
"caption": v.Caption,
"file_name": v.FileName,
"chat_id": to.Recipient(),
"caption": v.Caption,
}
b.embedSendOptions(params, opt)
@ -143,7 +140,7 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params["supports_streaming"] = "true"
}
msg, err := b.sendObject(&v.File, "video", params, thumbnailToFilemap(v.Thumbnail))
msg, err := b.sendObject(v.MediaFile(), "video", params, thumbnailToFilemap(v.Thumbnail))
if err != nil {
return nil, err
}
@ -165,12 +162,10 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
}
// Send delivers animation through bot b to recipient.
// @see https://core.telegram.org/bots/api#sendanimation
func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
"caption": a.Caption,
"file_name": a.FileName,
"chat_id": to.Recipient(),
"caption": a.Caption,
}
b.embedSendOptions(params, opt)
@ -184,12 +179,12 @@ func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro
params["height"] = strconv.Itoa(a.Height)
}
// file_name is required, without file_name GIFs sent as document
if params["file_name"] == "" && a.File.OnDisk() {
params["file_name"] = filepath.Base(a.File.FileLocal)
// Without the FileName GIF sends as a document.
if a.FileName == "" && a.File.OnDisk() {
a.FileName = filepath.Base(a.File.FileLocal)
}
msg, err := b.sendObject(&a.File, "animation", params, nil)
msg, err := b.sendObject(a.MediaFile(), "animation", params, nil)
if err != nil {
return nil, err
}

Loading…
Cancel
Save