Merge pull request #226 from demget/patch-files

Media objects improvements
This commit is contained in:
Ian Patrick Badtrousers 2019-10-05 08:10:13 +02:00 committed by GitHub
commit 7f125632ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 27 deletions

31
api.go
View File

@ -8,7 +8,6 @@ import (
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -18,7 +17,7 @@ import (
// Raw lets you call any method of Bot API manually.
func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
url := b.URL+ "/bot" + b.Token+"/"+ method
url := b.URL + "/bot" + b.Token + "/" + method
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(payload); err != nil {
@ -31,6 +30,7 @@ func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
}
resp.Close = true
defer resp.Body.Close()
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, wrapSystem(err)
@ -39,16 +39,11 @@ func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
return json, nil
}
func addFileToWriter(writer *multipart.Writer, fieldName string, file interface{}) error {
var reader io.Reader
var part io.Writer
var err error
var filename string
func addFileToWriter(writer *multipart.Writer,
filename, field string, file interface{}) error {
var reader io.Reader
if r, ok := file.(io.Reader); ok {
// Telegram requires fields to have a filename, otherwise
// `Bad Request: wrong URL host` would be returned
filename = "empty"
reader = r
} else if path, ok := file.(string); ok {
f, err := os.Open(path)
@ -56,14 +51,12 @@ func addFileToWriter(writer *multipart.Writer, fieldName string, file interface{
return err
}
defer f.Close()
reader = f
filename = filepath.Base(path)
} else {
return errors.Errorf("File for field `%v` should be an io.ReadCloser or string", fieldName)
return errors.Errorf("File for field `%v` should be an io.ReadCloser or string", field)
}
part, err = writer.CreateFormFile(fieldName, filename)
part, err := writer.CreateFormFile(field, filename)
if err != nil {
return err
}
@ -72,11 +65,9 @@ func addFileToWriter(writer *multipart.Writer, fieldName string, file interface{
return err
}
func (b *Bot) sendFiles(
method string,
files map[string]File,
func (b *Bot) sendFiles(method string, files map[string]File,
params map[string]string) ([]byte, error) {
// ---
body := &bytes.Buffer{}
rawFiles := map[string]interface{}{}
@ -102,7 +93,7 @@ func (b *Bot) sendFiles(
writer := multipart.NewWriter(body)
for field, file := range rawFiles {
if err := addFileToWriter(writer, field, file); err != nil {
if err := addFileToWriter(writer, params["file_name"], field, file); err != nil {
return nil, wrapSystem(err)
}
}
@ -117,7 +108,7 @@ func (b *Bot) sendFiles(
return nil, wrapSystem(err)
}
url := b.URL+ "/bot" + b.Token+"/"+ method
url := b.URL + "/bot" + b.Token + "/" + method
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, wrapSystem(err)

View File

@ -83,6 +83,7 @@ type Audio struct {
Title string `json:"title,omitempty"`
Performer string `json:"performer,omitempty"`
MIME string `json:"mime_type,omitempty"`
FileName string `json:"file_name,omitempty"`
}
// MediaFile returns &Audio.File
@ -95,13 +96,11 @@ func (a *Audio) MediaFile() *File {
type Document struct {
File
// Original filename as defined by sender.
FileName string `json:"file_name"`
// (Optional)
Thumbnail *Photo `json:"thumb,omitempty"`
Caption string `json:"caption,omitempty"`
MIME string `json:"mime_type"`
FileName string `json:"file_name,omitempty"`
}
// MediaFile returns &Document.File
@ -123,6 +122,7 @@ type Video struct {
Thumbnail *Photo `json:"thumb,omitempty"`
SupportsStreaming bool `json:"supports_streaming,omitempty"`
MIME string `json:"mime_type,omitempty"`
FileName string `json:"file_name,omitempty"`
}
// MediaFile returns &Video.File

View File

@ -38,6 +38,7 @@ func (p *Photo) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
msg.Photo.File.stealRef(&p.File)
*p = *msg.Photo
p.Caption = msg.Caption
return msg, nil
}
@ -49,6 +50,7 @@ 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,
}
if a.Duration != 0 {
@ -65,6 +67,7 @@ func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
if msg.Audio != nil {
msg.Audio.File.stealRef(&a.File)
*a = *msg.Audio
a.Caption = msg.Caption
}
if msg.Document != nil {
@ -78,8 +81,9 @@ 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,
"chat_id": to.Recipient(),
"caption": d.Caption,
"file_name": d.FileName,
}
if d.FileSize != 0 {
@ -95,6 +99,7 @@ func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error
msg.Document.File.stealRef(&d.File)
*d = *msg.Document
d.Caption = msg.Caption
return msg, nil
}
@ -120,8 +125,9 @@ 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,
"chat_id": to.Recipient(),
"caption": v.Caption,
"file_name": v.FileName,
}
if v.Duration != 0 {
@ -147,6 +153,7 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
if vid := msg.Video; vid != nil {
vid.File.stealRef(&v.File)
*v = *vid
v.Caption = msg.Caption
} else if doc := msg.Document; doc != nil {
// If video has no sound, Telegram can turn it into Document (GIF)
doc.File.stealRef(&v.File)