pull/228/merge
REDHAT3 6 years ago committed by GitHub
commit 59d8040d49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 122
      bot.go
  2. 2
      bot_test.go
  3. 22
      configs.go
  4. 2
      helpers.go
  5. BIN
      tests/gopher.png
  6. BIN
      tests/video.mp4
  7. BIN
      tests/videonote.mp4

122
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.

@ -8,7 +8,7 @@ import (
"testing"
"time"
"github.com/go-telegram-bot-api/telegram-bot-api"
tgbotapi "."
)
const (

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

@ -622,7 +622,7 @@ func NewEditMessageCaption(chatID int64, messageID int, caption string) EditMess
ChatID: chatID,
MessageID: messageID,
},
Caption: caption,
Caption: caption,
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Binary file not shown.