Merge pull request #156 from stek29/optional_fields

Embed optional fields when sending (fix #147)
pull/149/merge
Ian Byrd 6 years ago committed by GitHub
commit 9d0170e505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -487,24 +487,10 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
files := make(map[string]string) files := make(map[string]string)
for i, x := range a { for i, x := range a {
var ( var mediaRepr string
f *File var jsonRepr []byte
caption string
mediaRepr, mediaType string
)
switch y := x.(type) { f := x.MediaFile()
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)
}
if f.InCloud() { if f.InCloud() {
mediaRepr = f.FileID mediaRepr = f.FileID
@ -518,11 +504,38 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
"telebot: album entry #%d doesn't exist anywhere", i) "telebot: album entry #%d doesn't exist anywhere", i)
} }
jsonRepr, _ := json.Marshal(map[string]string{ switch y := x.(type) {
"type": mediaType, case *Photo:
"media": mediaRepr, jsonRepr, _ = json.Marshal(struct {
"caption": caption, 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) media[i] = string(jsonRepr)
} }

@ -109,9 +109,10 @@ type Video struct {
Duration int `json:"duration,omitempty"` Duration int `json:"duration,omitempty"`
// (Optional) // (Optional)
Caption string `json:"caption,omitempty"` Caption string `json:"caption,omitempty"`
Thumbnail *Photo `json:"thumb,omitempty"` Thumbnail *Photo `json:"thumb,omitempty"`
MIME string `json:"mime_type,omitempty"` SupportsStreaming bool `json:"supports_streaming,omitempty"`
MIME string `json:"mime_type,omitempty"`
} }
// MediaFile returns &Video.File // MediaFile returns &Video.File
@ -140,6 +141,7 @@ type VideoNote struct {
// (Optional) // (Optional)
Thumbnail *Photo `json:"thumb,omitempty"` Thumbnail *Photo `json:"thumb,omitempty"`
Length int `json:"length,omitempty"`
} }
// Contact object represents a contact to Telegram user // Contact object represents a contact to Telegram user
@ -172,5 +174,6 @@ type Venue struct {
Address string `json:"address"` Address string `json:"address"`
// (Optional) // (Optional)
FoursquareID string `json:"foursquare_id,omitempty"` FoursquareID string `json:"foursquare_id,omitempty"`
FoursquareType string `json:"foursquare_type,omitempty"`
} }

@ -1,6 +1,9 @@
package telebot package telebot
import "fmt" import (
"fmt"
"strconv"
)
// Recipient is any possible endpoint you can send // Recipient is any possible endpoint you can send
// messages to: either user, group or a channel. // messages to: either user, group or a channel.
@ -41,9 +44,16 @@ func (p *Photo) 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 (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { func (a *Audio) 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,
"performer": a.Performer,
"title": a.Title,
}
if a.Duration != 0 {
params["duration"] = strconv.Itoa(a.Duration)
} }
embedSendOptions(params, opt) embedSendOptions(params, opt)
msg, err := b.sendObject(&a.File, "audio", params) msg, err := b.sendObject(&a.File, "audio", params)
@ -60,9 +70,15 @@ 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,
} }
if d.FileSize != 0 {
params["file_size"] = strconv.Itoa(d.FileSize)
}
embedSendOptions(params, opt) embedSendOptions(params, opt)
msg, err := b.sendObject(&d.File, "document", params) msg, err := b.sendObject(&d.File, "document", params)
@ -100,6 +116,20 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
"chat_id": to.Recipient(), "chat_id": to.Recipient(),
"caption": v.Caption, "caption": v.Caption,
} }
if v.Duration != 0 {
params["duration"] = strconv.Itoa(v.Duration)
}
if v.Width != 0 {
params["width"] = strconv.Itoa(v.Width)
}
if v.Height != 0 {
params["height"] = strconv.Itoa(v.Height)
}
if v.SupportsStreaming {
params["supports_streaming"] = "true"
}
embedSendOptions(params, opt) embedSendOptions(params, opt)
msg, err := b.sendObject(&v.File, "video", params) msg, err := b.sendObject(&v.File, "video", params)
@ -127,6 +157,11 @@ func (v *Voice) 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(),
} }
if v.Duration != 0 {
params["duration"] = strconv.Itoa(v.Duration)
}
embedSendOptions(params, opt) embedSendOptions(params, opt)
msg, err := b.sendObject(&v.File, "voice", params) msg, err := b.sendObject(&v.File, "voice", params)
@ -145,6 +180,14 @@ func (v *VideoNote) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro
params := map[string]string{ params := map[string]string{
"chat_id": to.Recipient(), "chat_id": to.Recipient(),
} }
if v.Duration != 0 {
params["duration"] = strconv.Itoa(v.Duration)
}
if v.Length != 0 {
params["length"] = strconv.Itoa(v.Length)
}
embedSendOptions(params, opt) embedSendOptions(params, opt)
msg, err := b.sendObject(&v.File, "videoNote", params) msg, err := b.sendObject(&v.File, "videoNote", params)
@ -179,12 +222,13 @@ func (x *Location) 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 *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { func (v *Venue) 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(),
"latitude": fmt.Sprintf("%f", v.Location.Lat), "latitude": fmt.Sprintf("%f", v.Location.Lat),
"longitude": fmt.Sprintf("%f", v.Location.Lng), "longitude": fmt.Sprintf("%f", v.Location.Lng),
"title": v.Title, "title": v.Title,
"address": v.Address, "address": v.Address,
"foursquare_id": v.FoursquareID, "foursquare_id": v.FoursquareID,
"foursquare_type": v.FoursquareType,
} }
embedSendOptions(params, opt) embedSendOptions(params, opt)

Loading…
Cancel
Save