Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #29 #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"fmt"
"net/http"
"os"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand Down
11 changes: 4 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"

"github.com/sirupsen/logrus"
Expand All @@ -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)

Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
27 changes: 23 additions & 4 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package botgolang

import (
"fmt"
"os"
"io"
"path/filepath"
)

Expand All @@ -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"`
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down