2017-11-22 10:29:45 +00:00
|
|
|
|
package telegram
|
|
|
|
|
|
2017-12-13 14:02:07 +00:00
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"mime/multipart"
|
2018-02-14 21:53:40 +00:00
|
|
|
|
"os"
|
2018-08-21 11:05:04 +00:00
|
|
|
|
"path"
|
2017-12-13 14:02:07 +00:00
|
|
|
|
|
|
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
|
)
|
|
|
|
|
|
2019-07-23 13:45:57 +00:00
|
|
|
|
type UploadStickerFileParameters struct {
|
|
|
|
|
// User identifier of sticker file owner
|
|
|
|
|
UserID int `json:"user_id"`
|
|
|
|
|
|
|
|
|
|
// Png image with the sticker, must be up to 512 kilobytes in size,
|
|
|
|
|
// dimensions must not exceed 512px, and either width or height
|
|
|
|
|
// must be exactly 512px.
|
|
|
|
|
PNGSticker interface{} `json:"png_sticker"`
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
|
// ErrBadFileType describes error of the unsupported file data type for uploading
|
2017-12-13 14:02:07 +00:00
|
|
|
|
var ErrBadFileType = errors.New("bad file type")
|
|
|
|
|
|
2018-02-15 23:28:49 +00:00
|
|
|
|
/*
|
2018-04-12 11:59:07 +00:00
|
|
|
|
Upload is a helper method which provide are three ways to send files (photos, stickers, audio,
|
2018-02-15 23:28:49 +00:00
|
|
|
|
media, etc.):
|
|
|
|
|
|
|
|
|
|
1. If the file is already stored somewhere on the Telegram servers, you don't need to reupload it:
|
|
|
|
|
each file object has a file_id field, simply pass this file_id as a parameter instead of uploading.
|
|
|
|
|
There are no limits for files sent this way.
|
2018-04-19 13:02:15 +00:00
|
|
|
|
|
2018-10-12 11:14:58 +00:00
|
|
|
|
2. Provide Telegram with an *fasthttp.URI for the file to be sent. Telegram will download and send the
|
2018-02-15 23:28:49 +00:00
|
|
|
|
file. 5 MB max size for photos and 20 MB max for other types of content.
|
2018-04-19 13:02:15 +00:00
|
|
|
|
|
2018-02-15 23:28:49 +00:00
|
|
|
|
3. Post the file using multipart/form-data in the usual way that files are uploaded via the
|
|
|
|
|
browser. Use []byte or io.Reader for this. 10 MB max size for photos, 50 MB for other files.
|
|
|
|
|
|
2018-04-19 13:28:26 +00:00
|
|
|
|
Sending by FileID
|
2018-02-15 23:28:49 +00:00
|
|
|
|
|
2018-04-19 13:28:26 +00:00
|
|
|
|
* It is not possible to change the file type when resending by file_id. I.e. a video can't be sent
|
2018-02-15 23:28:49 +00:00
|
|
|
|
as a photo, a photo can't be sent as a document, etc.
|
2018-04-19 13:28:26 +00:00
|
|
|
|
|
|
|
|
|
* It is not possible to resend thumbnails.
|
|
|
|
|
|
|
|
|
|
* Resending a photo by file_id will send all of its sizes.
|
|
|
|
|
|
|
|
|
|
* file_id is unique for each individual bot and can't be transferred from one bot to another.
|
2018-02-15 23:28:49 +00:00
|
|
|
|
|
|
|
|
|
Sending by URL
|
|
|
|
|
|
2018-10-12 11:14:58 +00:00
|
|
|
|
* When sending by *fasthttp.URI the target file must have the correct MIME type (e.g., audio/mpeg for
|
2018-02-15 23:28:49 +00:00
|
|
|
|
sendAudio, etc.).
|
2018-04-19 13:28:26 +00:00
|
|
|
|
|
|
|
|
|
* In sendDocument, sending by URL will currently only work for gif, pdf and zip files.
|
|
|
|
|
|
|
|
|
|
* To use SendVoice, the file must have the type audio/ogg and be no more than 1MB in size. 1–20MB
|
2018-02-15 23:28:49 +00:00
|
|
|
|
voice notes will be sent as files.
|
2018-04-19 13:28:26 +00:00
|
|
|
|
|
|
|
|
|
* Other configurations may work but we can't guarantee that they will.
|
2018-02-15 23:28:49 +00:00
|
|
|
|
*/
|
2019-07-24 09:34:55 +00:00
|
|
|
|
func (b *Bot) Upload(method, key, name string, file InputFile, args *http.Args) (*Response, error) {
|
2017-12-13 14:02:07 +00:00
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
|
multi := multipart.NewWriter(buffer)
|
|
|
|
|
|
2018-10-09 12:33:01 +00:00
|
|
|
|
requestURI := http.AcquireURI()
|
|
|
|
|
requestURI.SetScheme("https")
|
|
|
|
|
requestURI.SetHost("api.telegram.org")
|
2018-10-12 11:47:49 +00:00
|
|
|
|
requestURI.SetPath(path.Join("bot"+b.AccessToken, method))
|
2017-12-13 14:02:07 +00:00
|
|
|
|
|
2018-10-12 11:14:58 +00:00
|
|
|
|
args.VisitAll(func(key, value []byte) {
|
2018-10-12 11:47:49 +00:00
|
|
|
|
_ = multi.WriteField(string(key), string(value))
|
2018-10-12 11:14:58 +00:00
|
|
|
|
})
|
2017-12-13 14:02:07 +00:00
|
|
|
|
|
2019-07-24 09:34:55 +00:00
|
|
|
|
if err := createFileField(multi, file, key, name); err != nil {
|
|
|
|
|
return nil, err
|
2018-04-12 11:59:07 +00:00
|
|
|
|
}
|
2017-12-19 20:38:08 +00:00
|
|
|
|
|
2019-07-24 09:34:55 +00:00
|
|
|
|
if err := multi.Close(); err != nil {
|
|
|
|
|
return nil, err
|
2017-12-13 14:02:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := http.AcquireRequest()
|
|
|
|
|
defer http.ReleaseRequest(req)
|
|
|
|
|
req.SetBody(buffer.Bytes())
|
|
|
|
|
req.Header.SetContentType(multi.FormDataContentType())
|
|
|
|
|
req.Header.SetMethod("POST")
|
|
|
|
|
req.Header.SetRequestURI(requestURI.String())
|
2019-07-24 09:34:55 +00:00
|
|
|
|
req.Header.SetUserAgent(path.Join("telegram", Version))
|
2018-10-09 12:33:01 +00:00
|
|
|
|
req.Header.SetHostBytes(requestURI.Host())
|
2017-12-13 14:02:07 +00:00
|
|
|
|
|
|
|
|
|
resp := http.AcquireResponse()
|
|
|
|
|
defer http.ReleaseResponse(resp)
|
|
|
|
|
|
2019-07-24 09:34:55 +00:00
|
|
|
|
if err := http.Do(req, resp); err != nil {
|
|
|
|
|
return nil, err
|
2018-04-19 10:56:25 +00:00
|
|
|
|
}
|
2017-12-13 14:02:07 +00:00
|
|
|
|
|
2019-07-24 09:34:55 +00:00
|
|
|
|
var response Response
|
|
|
|
|
if err := parser.Unmarshal(resp.Body(), &response); err != nil {
|
|
|
|
|
return nil, err
|
2017-12-13 14:02:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 11:05:04 +00:00
|
|
|
|
if !response.Ok {
|
2019-07-24 09:34:55 +00:00
|
|
|
|
return nil, errors.New(response.Description)
|
2017-12-13 14:02:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 09:34:55 +00:00
|
|
|
|
return &response, nil
|
2017-12-13 14:02:07 +00:00
|
|
|
|
}
|
2018-04-12 11:59:07 +00:00
|
|
|
|
|
2018-10-12 11:47:49 +00:00
|
|
|
|
func createFileField(w *multipart.Writer, file interface{}, key, val string) (err error) {
|
2018-04-19 10:56:25 +00:00
|
|
|
|
switch src := file.(type) {
|
|
|
|
|
case string: // Send FileID of file on disk path
|
2019-07-26 10:09:49 +00:00
|
|
|
|
return createFileFieldString(w, key, src)
|
2018-10-12 11:14:58 +00:00
|
|
|
|
case *http.URI: // Send by URL
|
2019-07-26 10:09:49 +00:00
|
|
|
|
return w.WriteField(key, src.String())
|
2018-04-19 10:56:25 +00:00
|
|
|
|
case []byte: // Upload new
|
2019-07-26 10:09:49 +00:00
|
|
|
|
return createFileFieldRaw(w, key, val, bytes.NewReader(src))
|
2018-04-19 10:56:25 +00:00
|
|
|
|
case io.Reader: // Upload new
|
2019-07-26 10:09:49 +00:00
|
|
|
|
return createFileFieldRaw(w, key, val, src)
|
2018-04-19 10:56:25 +00:00
|
|
|
|
default:
|
2019-07-26 10:09:49 +00:00
|
|
|
|
return ErrBadFileType
|
2018-04-19 10:56:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:47:49 +00:00
|
|
|
|
func createFileFieldString(w *multipart.Writer, key, src string) (err error) {
|
|
|
|
|
_, err = os.Stat(src)
|
2018-04-19 10:56:25 +00:00
|
|
|
|
|
2018-04-12 11:59:07 +00:00
|
|
|
|
switch {
|
|
|
|
|
case os.IsNotExist(err):
|
2018-04-19 10:56:25 +00:00
|
|
|
|
err = w.WriteField(key, src)
|
2018-04-12 11:59:07 +00:00
|
|
|
|
case os.IsExist(err):
|
|
|
|
|
err = uploadFromDisk(w, key, src)
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:47:49 +00:00
|
|
|
|
return
|
2018-04-12 11:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func uploadFromDisk(w *multipart.Writer, key, src string) error {
|
|
|
|
|
file, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-04-12 13:49:05 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
_ = file.Close()
|
|
|
|
|
}()
|
2018-04-12 11:59:07 +00:00
|
|
|
|
|
|
|
|
|
var formFile io.Writer
|
|
|
|
|
formFile, err = w.CreateFormFile(key, file.Name())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(formFile, file)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 10:56:25 +00:00
|
|
|
|
func createFileFieldRaw(w *multipart.Writer, key, value string, src io.Reader) error {
|
2018-04-12 11:59:07 +00:00
|
|
|
|
field, err := w.CreateFormFile(key, value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(field, src)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-07-23 13:45:57 +00:00
|
|
|
|
|
|
|
|
|
// UploadStickerFile upload a .png file with a sticker for later use in
|
|
|
|
|
// createNewStickerSet and addStickerToSet methods (can be used multiple times).
|
|
|
|
|
// Returns the uploaded File on success.
|
|
|
|
|
func (b *Bot) UploadStickerFile(userID int, pngSticker interface{}) (*File, error) {
|
|
|
|
|
args := http.AcquireArgs()
|
|
|
|
|
defer http.ReleaseArgs(args)
|
|
|
|
|
args.SetUint("user_id", userID)
|
|
|
|
|
|
|
|
|
|
resp, err := b.Upload(MethodUploadStickerFile, TypeSticker, "sticker", pngSticker, args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var f File
|
2019-07-24 09:34:55 +00:00
|
|
|
|
err = parser.Unmarshal(resp.Result, &f)
|
2019-07-23 13:45:57 +00:00
|
|
|
|
return &f, err
|
|
|
|
|
}
|