bot: add audio and document for sendMediaGroup

pull/433/head
Demian 4 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() pipeReader, pipeWriter := io.Pipe()
writer := multipart.NewWriter(pipeWriter) writer := multipart.NewWriter(pipeWriter)
go func() { go func() {
defer pipeWriter.Close() defer pipeWriter.Close()
for field, file := range rawFiles { 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) pipeWriter.CloseWithError(err)
return return
} }

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

@ -22,12 +22,9 @@ type InputMedia interface {
type Photo struct { type Photo struct {
File File
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
Caption string `json:"caption,omitempty"`
// (Optional)
Caption string `json:"caption,omitempty"`
ParseMode ParseMode `json:"parse_mode,omitempty"`
} }
type photoSize struct { type photoSize struct {
@ -57,7 +54,6 @@ func (p *Photo) UnmarshalJSON(jsonStr []byte) error {
} }
} else { } else {
var sizes []photoSize var sizes []photoSize
if err := json.Unmarshal(jsonStr, &sizes); err != nil { if err := json.Unmarshal(jsonStr, &sizes); err != nil {
return err return err
} }
@ -136,9 +132,8 @@ func (v *Video) MediaFile() *File {
type Animation struct { type Animation struct {
File File
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
Duration int `json:"duration,omitempty"` Duration int `json:"duration,omitempty"`
// (Optional) // (Optional)
@ -198,12 +193,12 @@ type Location struct {
// Horizontal Accuracy // Horizontal Accuracy
HorizontalAccuracy *float32 `json:"horizontal_accuracy,omitempty"` HorizontalAccuracy *float32 `json:"horizontal_accuracy,omitempty"`
Heading int `json:"heading,omitempty"`
// Period in seconds for which the location will be updated // Period in seconds for which the location will be updated
// (see Live Locations, should be between 60 and 86400.) // (see Live Locations, should be between 60 and 86400.)
LivePeriod int `json:"live_period,omitempty"` LivePeriod int `json:"live_period,omitempty"`
Heading int `json:"heading,omitempty"`
ProximityAlertRadius int `json:"proximity_alert_radius,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, "caption": a.Caption,
"performer": a.Performer, "performer": a.Performer,
"title": a.Title, "title": a.Title,
"file_name": a.FileName,
} }
b.embedSendOptions(params, opt) 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) 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 { if err != nil {
return nil, err 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. // Send delivers media through bot b to recipient.
func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{ params := map[string]string{
"chat_id": to.Recipient(), "chat_id": to.Recipient(),
"caption": d.Caption, "caption": d.Caption,
"file_name": d.FileName,
} }
b.embedSendOptions(params, opt) 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) 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 { if err != nil {
return nil, err 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. // Send delivers media through bot b to recipient.
func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{ params := map[string]string{
"chat_id": to.Recipient(), "chat_id": to.Recipient(),
"caption": v.Caption, "caption": v.Caption,
"file_name": v.FileName,
} }
b.embedSendOptions(params, opt) b.embedSendOptions(params, opt)
@ -143,7 +140,7 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params["supports_streaming"] = "true" 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 { if err != nil {
return nil, err 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. // 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) { func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{ params := map[string]string{
"chat_id": to.Recipient(), "chat_id": to.Recipient(),
"caption": a.Caption, "caption": a.Caption,
"file_name": a.FileName,
} }
b.embedSendOptions(params, opt) 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) params["height"] = strconv.Itoa(a.Height)
} }
// file_name is required, without file_name GIFs sent as document // Without the FileName GIF sends as a document.
if params["file_name"] == "" && a.File.OnDisk() { if a.FileName == "" && a.File.OnDisk() {
params["file_name"] = filepath.Base(a.File.FileLocal) 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 { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save