Skip to content

Commit

Permalink
Embed optional fields in SendAlbum
Browse files Browse the repository at this point in the history
  • Loading branch information
stek29 committed Sep 15, 2018
1 parent 6264ba6 commit fd3c84f
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,24 +487,10 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
files := make(map[string]string)

for i, x := range a {
var (
f *File
caption string
mediaRepr, mediaType string
)
var mediaRepr string
var jsonRepr []byte

switch y := x.(type) {
case *Photo:
f = &y.File
mediaType = "photo"
caption = y.Caption
case *Video:
f = &y.File
mediaType = "video"
caption = y.Caption
default:
return nil, errors.Errorf("telebot: album entry #%d is not valid", i)
}
f := x.MediaFile()

if f.InCloud() {
mediaRepr = f.FileID
Expand All @@ -518,11 +504,38 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
"telebot: album entry #%d doesn't exist anywhere", i)
}

jsonRepr, _ := json.Marshal(map[string]string{
"type": mediaType,
"media": mediaRepr,
"caption": caption,
})
switch y := x.(type) {
case *Photo:
jsonRepr, _ = json.Marshal(struct {
Type string `json:"type"`
Caption string `json:"caption"`
Media string `json:"media"`
}{
"photo",
y.Caption,
mediaRepr,
})
case *Video:
jsonRepr, _ = json.Marshal(struct {
Type string `json:"type"`
Caption string `json:"caption"`
Media string `json:"media"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Duration int `json:"duration,omitempty"`
SupportsStreaming bool `json:"supports_streaming,omitempty"`
}{
"video",
y.Caption,
mediaRepr,
y.Width,
y.Height,
y.Duration,
y.SupportsStreaming,
})
default:
return nil, errors.Errorf("telebot: album entry #%d is not valid", i)
}

media[i] = string(jsonRepr)
}
Expand Down

0 comments on commit fd3c84f

Please sign in to comment.