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..b14f8e6 100644 --- a/client.go +++ b/client.go @@ -6,11 +6,9 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "mime/multipart" "net/http" "net/url" - "os" "strconv" "github.com/sirupsen/logrus" @@ -23,11 +21,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) @@ -60,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 } @@ -84,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, @@ -636,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 { 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 }