diff --git a/bot.go b/bot.go index a996790..d5155f2 100644 --- a/bot.go +++ b/bot.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "io/ioutil" + "mime/multipart" "net/http" "net/url" "os" @@ -142,10 +143,85 @@ func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Messa // Requires the parameter to hold the file not be in the params. // File should be a string to a file path, a FileBytes struct, // a FileReader struct, or a url.URL. -// -// Note that if your FileReader has a size set to -1, it will read -// the file into memory to calculate a size. -func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) { +func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}, thumb interface{}) (APIResponse, error) { + if thumb != nil { + buffer := bytes.Buffer{} + + writer := multipart.NewWriter(&buffer) + + params["thumb"] = "attach://test" + + for k, v := range params { + if writerErr := writer.WriteField(k, v); writerErr != nil { + return APIResponse{}, writerErr + } + } + + // Method file + f := file.(FileReader) + formFile, formFileErr := writer.CreateFormFile(fieldname, f.Name) + if formFileErr != nil { + return APIResponse{}, formFileErr + } + + if _, copyErr := io.Copy(formFile, f.Reader); copyErr != nil { + return APIResponse{}, copyErr + } + + // Thumb file + tf := thumb.(FileReader) + formFile1, formFileErr := writer.CreateFormFile("test", tf.Name) + if formFileErr != nil { + return APIResponse{}, formFileErr + } + + if _, copyErr := io.Copy(formFile1, tf.Reader); copyErr != nil { + return APIResponse{}, copyErr + } + + if err := writer.Close(); err != nil { + fmt.Println(err) + } + + // http + method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) + + req, err := http.NewRequest("POST", method, &buffer) + if err != nil { + return APIResponse{}, err + } + + req.Header.Set("Content-Type", writer.FormDataContentType()) + + res, err := bot.Client.Do(req) + if err != nil { + return APIResponse{}, err + } + defer res.Body.Close() + + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return APIResponse{}, err + } + + if bot.Debug { + log.Println(string(bytes)) + } + fmt.Println("bytes -- ", string(bytes)) + var apiResp APIResponse + + err = json.Unmarshal(bytes, &apiResp) + if err != nil { + return APIResponse{}, err + } + + if !apiResp.Ok { + return APIResponse{}, errors.New(apiResp.Description) + } + + return apiResp, nil + } + ms := multipartstreamer.New() switch f := file.(type) { @@ -174,10 +250,8 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna if f.Size != -1 { ms.WriteReader(fieldname, f.Name, f.Size, f.Reader) - break } - data, err := ioutil.ReadAll(f.Reader) if err != nil { return APIResponse{}, err @@ -219,7 +293,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna } var apiResp APIResponse - + fmt.Println("bytes1 --- ", string(bytes)) err = json.Unmarshal(bytes, &apiResp) if err != nil { return APIResponse{}, err @@ -275,9 +349,26 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool { // // It requires the Chattable to send. func (bot *BotAPI) Send(c Chattable) (Message, error) { + var thumb interface{} + + switch c.(type) { + case AudioConfig: + if c.(AudioConfig).Thumb.Reader != nil { + thumb = c.(AudioConfig).Thumb + } + case VideoConfig: + if c.(VideoConfig).Thumb.Reader != nil { + thumb = c.(VideoConfig).Thumb + } + case DocumentConfig: + if c.(DocumentConfig).Thumb.Reader != nil { + thumb = c.(DocumentConfig).Thumb + } + } + switch c.(type) { case Fileable: - return bot.sendFile(c.(Fileable)) + return bot.sendFile(c.(Fileable), thumb) default: return bot.sendChattable(c) } @@ -310,15 +401,14 @@ func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) } // uploadAndSend will send a Message with a new file to Telegram. -func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error) { +func (bot *BotAPI) uploadAndSend(method string, config Fileable, thumb interface{}) (Message, error) { params, err := config.params() if err != nil { return Message{}, err } file := config.getFile() - - resp, err := bot.UploadFile(method, params, config.name(), file) + resp, err := bot.UploadFile(method, params, config.name(), file, thumb) if err != nil { return Message{}, err } @@ -327,18 +417,16 @@ func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error json.Unmarshal(resp.Result, &message) bot.debugLog(method, nil, message) - return message, nil } // sendFile determines if the file is using an existing file or uploading // a new file, then sends it as needed. -func (bot *BotAPI) sendFile(config Fileable) (Message, error) { +func (bot *BotAPI) sendFile(config Fileable, thumb interface{}) (Message, error) { if config.useExistingFile() { return bot.sendExisting(config.method(), config) } - - return bot.uploadAndSend(config.method(), config) + return bot.uploadAndSend(config.method(), config, thumb) } // sendChattable sends a Chattable. @@ -465,7 +553,7 @@ func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) { params["max_connections"] = strconv.Itoa(config.MaxConnections) } - resp, err := bot.UploadFile("setWebhook", params, "certificate", config.Certificate) + resp, err := bot.UploadFile("setWebhook", params, "certificate", config.Certificate, nil) if err != nil { return APIResponse{}, err } @@ -960,7 +1048,7 @@ func (bot *BotAPI) SetChatPhoto(config SetChatPhotoConfig) (APIResponse, error) file := config.getFile() - return bot.UploadFile(config.method(), params, config.name(), file) + return bot.UploadFile(config.method(), params, config.name(), file, nil) } // DeleteChatPhoto delete photo of chat. diff --git a/bot_test.go b/bot_test.go index 60f3e65..35b7441 100644 --- a/bot_test.go +++ b/bot_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/go-telegram-bot-api/telegram-bot-api" + tgbotapi "." ) const ( diff --git a/configs.go b/configs.go index 181d4e4..6731de9 100644 --- a/configs.go +++ b/configs.go @@ -127,7 +127,6 @@ func (file BaseFile) params() (map[string]string, error) { if err != nil { return params, err } - params["reply_markup"] = string(data) } @@ -297,6 +296,7 @@ type AudioConfig struct { Duration int Performer string Title string + Thumb FileReader } // values returns a url.Values representation of AudioConfig. @@ -366,6 +366,7 @@ type DocumentConfig struct { BaseFile Caption string ParseMode string + Thumb FileReader } // values returns a url.Values representation of DocumentConfig. @@ -447,9 +448,11 @@ func (config StickerConfig) method() string { // VideoConfig contains information about a SendVideo request. type VideoConfig struct { BaseFile - Duration int - Caption string - ParseMode string + Duration int + Caption string + ParseMode string + Thumb FileReader + SupportsStreaming bool } // values returns a url.Values representation of VideoConfig. @@ -458,7 +461,6 @@ func (config VideoConfig) values() (url.Values, error) { if err != nil { return v, err } - v.Add(config.name(), config.FileID) if config.Duration != 0 { v.Add("duration", strconv.Itoa(config.Duration)) @@ -469,6 +471,11 @@ func (config VideoConfig) values() (url.Values, error) { v.Add("parse_mode", config.ParseMode) } } + if config.SupportsStreaming { + v.Add("supports_streaming", "true") + } else { + v.Add("supports_streaming", "false") + } return v, nil } @@ -483,6 +490,11 @@ func (config VideoConfig) params() (map[string]string, error) { params["parse_mode"] = config.ParseMode } } + if config.SupportsStreaming { + params["supports_streaming"] = "true" + } else { + params["supports_streaming"] = "false" + } return params, nil } diff --git a/helpers.go b/helpers.go index 3dabe11..70180bc 100644 --- a/helpers.go +++ b/helpers.go @@ -622,7 +622,7 @@ func NewEditMessageCaption(chatID int64, messageID int, caption string) EditMess ChatID: chatID, MessageID: messageID, }, - Caption: caption, + Caption: caption, } } diff --git a/tests/gopher.png b/tests/gopher.png new file mode 100644 index 0000000..4c4700e Binary files /dev/null and b/tests/gopher.png differ diff --git a/tests/video.mp4 b/tests/video.mp4 deleted file mode 100644 index a203d0c..0000000 Binary files a/tests/video.mp4 and /dev/null differ diff --git a/tests/videonote.mp4 b/tests/videonote.mp4 deleted file mode 100644 index 649d16f..0000000 Binary files a/tests/videonote.mp4 and /dev/null differ