Add custom file name support for media

pull/226/head
demiangetman 5 years ago
parent 6fa61fef85
commit 8001defa0c

@ -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)

@ -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

@ -49,6 +49,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 {
@ -78,8 +79,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 {
@ -120,8 +122,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 {

Loading…
Cancel
Save