|
|
@ -9,6 +9,7 @@ import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
|
|
|
|
"mime/multipart" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"net/url" |
|
|
|
"net/url" |
|
|
|
"os" |
|
|
|
"os" |
|
|
@ -134,10 +135,85 @@ func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Messa |
|
|
|
// Requires the parameter to hold the file not be in the params.
|
|
|
|
// Requires the parameter to hold the file not be in the params.
|
|
|
|
// File should be a string to a file path, a FileBytes struct,
|
|
|
|
// File should be a string to a file path, a FileBytes struct,
|
|
|
|
// a FileReader struct, or a url.URL.
|
|
|
|
// a FileReader struct, or a url.URL.
|
|
|
|
//
|
|
|
|
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}, thumb interface{}) (APIResponse, error) { |
|
|
|
// Note that if your FileReader has a size set to -1, it will read
|
|
|
|
if thumb != nil { |
|
|
|
// the file into memory to calculate a size.
|
|
|
|
buffer := bytes.Buffer{} |
|
|
|
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) { |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
ms := multipartstreamer.New() |
|
|
|
|
|
|
|
|
|
|
|
switch f := file.(type) { |
|
|
|
switch f := file.(type) { |
|
|
@ -166,10 +242,8 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna |
|
|
|
|
|
|
|
|
|
|
|
if f.Size != -1 { |
|
|
|
if f.Size != -1 { |
|
|
|
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader) |
|
|
|
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader) |
|
|
|
|
|
|
|
|
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(f.Reader) |
|
|
|
data, err := ioutil.ReadAll(f.Reader) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return APIResponse{}, err |
|
|
|
return APIResponse{}, err |
|
|
@ -211,7 +285,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var apiResp APIResponse |
|
|
|
var apiResp APIResponse |
|
|
|
|
|
|
|
fmt.Println("bytes1 --- ", string(bytes)) |
|
|
|
err = json.Unmarshal(bytes, &apiResp) |
|
|
|
err = json.Unmarshal(bytes, &apiResp) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return APIResponse{}, err |
|
|
|
return APIResponse{}, err |
|
|
@ -267,9 +341,26 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// It requires the Chattable to send.
|
|
|
|
// It requires the Chattable to send.
|
|
|
|
func (bot *BotAPI) Send(c Chattable) (Message, error) { |
|
|
|
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) { |
|
|
|
switch c.(type) { |
|
|
|
case Fileable: |
|
|
|
case Fileable: |
|
|
|
return bot.sendFile(c.(Fileable)) |
|
|
|
return bot.sendFile(c.(Fileable), thumb) |
|
|
|
default: |
|
|
|
default: |
|
|
|
return bot.sendChattable(c) |
|
|
|
return bot.sendChattable(c) |
|
|
|
} |
|
|
|
} |
|
|
@ -302,15 +393,14 @@ func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// uploadAndSend will send a Message with a new file to Telegram.
|
|
|
|
// 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() |
|
|
|
params, err := config.params() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return Message{}, err |
|
|
|
return Message{}, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
file := config.getFile() |
|
|
|
file := config.getFile() |
|
|
|
|
|
|
|
resp, err := bot.UploadFile(method, params, config.name(), file, thumb) |
|
|
|
resp, err := bot.UploadFile(method, params, config.name(), file) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return Message{}, err |
|
|
|
return Message{}, err |
|
|
|
} |
|
|
|
} |
|
|
@ -319,18 +409,16 @@ func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error |
|
|
|
json.Unmarshal(resp.Result, &message) |
|
|
|
json.Unmarshal(resp.Result, &message) |
|
|
|
|
|
|
|
|
|
|
|
bot.debugLog(method, nil, message) |
|
|
|
bot.debugLog(method, nil, message) |
|
|
|
|
|
|
|
|
|
|
|
return message, nil |
|
|
|
return message, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// sendFile determines if the file is using an existing file or uploading
|
|
|
|
// sendFile determines if the file is using an existing file or uploading
|
|
|
|
// a new file, then sends it as needed.
|
|
|
|
// 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() { |
|
|
|
if config.useExistingFile() { |
|
|
|
return bot.sendExisting(config.method(), config) |
|
|
|
return bot.sendExisting(config.method(), config) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return bot.uploadAndSend(config.method(), config, thumb) |
|
|
|
return bot.uploadAndSend(config.method(), config) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// sendChattable sends a Chattable.
|
|
|
|
// sendChattable sends a Chattable.
|
|
|
@ -457,7 +545,7 @@ func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) { |
|
|
|
params["max_connections"] = strconv.Itoa(config.MaxConnections) |
|
|
|
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 { |
|
|
|
if err != nil { |
|
|
|
return APIResponse{}, err |
|
|
|
return APIResponse{}, err |
|
|
|
} |
|
|
|
} |
|
|
@ -952,7 +1040,7 @@ func (bot *BotAPI) SetChatPhoto(config SetChatPhotoConfig) (APIResponse, error) |
|
|
|
|
|
|
|
|
|
|
|
file := config.getFile() |
|
|
|
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.
|
|
|
|
// DeleteChatPhoto delete photo of chat.
|
|
|
|