From 3af6a01d45e514a60dddb53cdd1b4afd54eceb5c Mon Sep 17 00:00:00 2001 From: Savely Krasovsky Date: Mon, 30 Jan 2023 00:16:53 +0300 Subject: [PATCH 1/2] feat: versatile file uploading --- bot.go | 5 ++--- client.go | 5 ++--- example/main.go | 4 ++-- message.go | 27 +++++++++++++++++++++++---- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/bot.go b/bot.go index 1e84245..dc9ce48 100644 --- a/bot.go +++ b/bot.go @@ -9,7 +9,6 @@ import ( "context" "fmt" "net/http" - "os" "github.com/sirupsen/logrus" ) @@ -147,7 +146,7 @@ func (b *Bot) NewInlineKeyboardMessage(chatID, text string, keyboard Keyboard) * } // NewFileMessage returns new file message -func (b *Bot) NewFileMessage(chatID string, file *os.File) *Message { +func (b *Bot) NewFileMessage(chatID string, file *MessageFile) *Message { return &Message{ client: b.client, Chat: Chat{ID: chatID}, @@ -167,7 +166,7 @@ func (b *Bot) NewFileMessageByFileID(chatID, fileID string) *Message { } // NewVoiceMessage returns new voice message -func (b *Bot) NewVoiceMessage(chatID string, file *os.File) *Message { +func (b *Bot) NewVoiceMessage(chatID string, file *MessageFile) *Message { return &Message{ client: b.client, Chat: Chat{ID: chatID}, diff --git a/client.go b/client.go index d44b398..816e734 100644 --- a/client.go +++ b/client.go @@ -10,7 +10,6 @@ import ( "mime/multipart" "net/http" "net/url" - "os" "strconv" "github.com/sirupsen/logrus" @@ -23,11 +22,11 @@ type Client struct { logger *logrus.Logger } -func (c *Client) Do(path string, params url.Values, file *os.File) ([]byte, error) { +func (c *Client) Do(path string, params url.Values, file *MessageFile) ([]byte, error) { return c.DoWithContext(context.Background(), path, params, file) } -func (c *Client) DoWithContext(ctx context.Context, path string, params url.Values, file *os.File) ([]byte, error) { +func (c *Client) DoWithContext(ctx context.Context, path string, params url.Values, file *MessageFile) ([]byte, error) { apiURL, err := url.Parse(c.baseURL + path) params.Set("token", c.token) diff --git a/example/main.go b/example/main.go index f519b68..2dbbd61 100644 --- a/example/main.go +++ b/example/main.go @@ -30,7 +30,7 @@ func main() { log.Fatalf("cannot open file: %s", err) } - fileMessage := bot.NewFileMessage("d.dorofeev@corp.mail.ru", file) + fileMessage := bot.NewFileMessage("d.dorofeev@corp.mail.ru", botgolang.NewMessageFile(file.Name(), file)) if err := fileMessage.Send(); err != nil { log.Println(err) } @@ -49,7 +49,7 @@ func main() { } defer file.Close() - voiceMessage := bot.NewVoiceMessage("g.gabolaev@corp.mail.ru", file) + voiceMessage := bot.NewVoiceMessage("g.gabolaev@corp.mail.ru", botgolang.NewMessageFile(file.Name(), file)) if err := voiceMessage.Send(); err != nil { log.Println(err) } diff --git a/message.go b/message.go index 98cbdbb..65c9f55 100644 --- a/message.go +++ b/message.go @@ -2,7 +2,7 @@ package botgolang import ( "fmt" - "os" + "io" "path/filepath" ) @@ -26,7 +26,7 @@ type Message struct { ID string `json:"msgId"` // File contains file attachment of the message - File *os.File `json:"-"` + File *MessageFile `json:"-"` // Id of file to send FileID string `json:"fileId"` @@ -62,7 +62,26 @@ type Message struct { RequestID string `json:"requestID"` } -func (m *Message) AttachNewFile(file *os.File) { +// MessageFile represents a file to send +type MessageFile struct { + io.Reader + name string +} + +// Name returns a name of file to be compatible with *os.File +func (f *MessageFile) Name() string { + return f.name +} + +// NewMessageFile returns *MessageFile from name and reader +func NewMessageFile(name string, reader io.Reader) *MessageFile { + return &MessageFile{ + name: name, + Reader: reader, + } +} + +func (m *Message) AttachNewFile(file *MessageFile) { m.File = file m.ContentType = OtherFile } @@ -72,7 +91,7 @@ func (m *Message) AttachExistingFile(fileID string) { m.ContentType = OtherFile } -func (m *Message) AttachNewVoice(file *os.File) { +func (m *Message) AttachNewVoice(file *MessageFile) { m.File = file m.ContentType = Voice } From fd7d386c8db1bd08f2e066c2c4caa1a14802fadd Mon Sep 17 00:00:00 2001 From: Savely Krasovsky Date: Mon, 30 Jan 2023 00:18:28 +0300 Subject: [PATCH 2/2] fix: replace deprecated ioutil by io --- client.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 816e734..b14f8e6 100644 --- a/client.go +++ b/client.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "mime/multipart" "net/http" "net/url" @@ -59,7 +58,7 @@ func (c *Client) DoWithContext(ctx context.Context, path string, params url.Valu } req.Header.Set("Content-Type", multipartWriter.FormDataContentType()) - req.Body = ioutil.NopCloser(buffer) + req.Body = io.NopCloser(buffer) req.Method = http.MethodPost } @@ -83,7 +82,7 @@ func (c *Client) DoWithContext(ctx context.Context, path string, params url.Valu } }() - responseBody, err := ioutil.ReadAll(resp.Body) + responseBody, err := io.ReadAll(resp.Body) if err != nil { c.logger.WithFields(logrus.Fields{ "err": err, @@ -635,7 +634,6 @@ func (c *Client) SendAnswerCallbackQuery(answer *ButtonResponse) error { func NewClient(baseURL string, token string, logger *logrus.Logger) *Client { return NewCustomClient(http.DefaultClient, baseURL, token, logger) - } func NewCustomClient(client *http.Client, baseURL string, token string, logger *logrus.Logger) *Client {