Skip to content

Commit 7c82078

Browse files
authored
Merge pull request #469 from go-telegram-bot-api/files
Create interface for file data
2 parents 0d6825e + d2539d0 commit 7c82078

File tree

10 files changed

+281
-239
lines changed

10 files changed

+281
-239
lines changed

bot.go

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package tgbotapi
44

55
import (
6-
"bytes"
76
"encoding/json"
87
"errors"
98
"fmt"
@@ -12,7 +11,6 @@ import (
1211
"mime/multipart"
1312
"net/http"
1413
"net/url"
15-
"os"
1614
"strings"
1715
"time"
1816
)
@@ -185,54 +183,37 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
185183
}
186184

187185
for _, file := range files {
188-
switch f := file.File.(type) {
189-
case string:
190-
fileHandle, err := os.Open(f)
186+
if file.Data.NeedsUpload() {
187+
name, reader, err := file.Data.UploadData()
191188
if err != nil {
192189
w.CloseWithError(err)
193190
return
194191
}
195-
defer fileHandle.Close()
196192

197-
part, err := m.CreateFormFile(file.Name, fileHandle.Name())
193+
part, err := m.CreateFormFile(file.Name, name)
198194
if err != nil {
199195
w.CloseWithError(err)
200196
return
201197
}
202198

203-
io.Copy(part, fileHandle)
204-
case FileBytes:
205-
part, err := m.CreateFormFile(file.Name, f.Name)
206-
if err != nil {
199+
if _, err := io.Copy(part, reader); err != nil {
207200
w.CloseWithError(err)
208201
return
209202
}
210203

211-
buf := bytes.NewBuffer(f.Bytes)
212-
io.Copy(part, buf)
213-
case FileReader:
214-
part, err := m.CreateFormFile(file.Name, f.Name)
215-
if err != nil {
216-
w.CloseWithError(err)
217-
return
204+
if closer, ok := reader.(io.ReadCloser); ok {
205+
if err = closer.Close(); err != nil {
206+
w.CloseWithError(err)
207+
return
208+
}
218209
}
210+
} else {
211+
value := file.Data.SendData()
219212

220-
io.Copy(part, f.Reader)
221-
case FileURL:
222-
val := string(f)
223-
if err := m.WriteField(file.Name, val); err != nil {
224-
w.CloseWithError(err)
225-
return
226-
}
227-
case FileID:
228-
val := string(f)
229-
if err := m.WriteField(file.Name, val); err != nil {
213+
if err := m.WriteField(file.Name, value); err != nil {
230214
w.CloseWithError(err)
231215
return
232216
}
233-
default:
234-
w.CloseWithError(errors.New(ErrBadFileType))
235-
return
236217
}
237218
}
238219
}()
@@ -321,8 +302,7 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool {
321302

322303
func hasFilesNeedingUpload(files []RequestFile) bool {
323304
for _, file := range files {
324-
switch file.File.(type) {
325-
case string, FileBytes, FileReader:
305+
if file.Data.NeedsUpload() {
326306
return true
327307
}
328308
}
@@ -349,20 +329,7 @@ func (bot *BotAPI) Request(c Chattable) (*APIResponse, error) {
349329
// However, if there are no files to be uploaded, there's likely things
350330
// that need to be turned into params instead.
351331
for _, file := range files {
352-
var s string
353-
354-
switch f := file.File.(type) {
355-
case string:
356-
s = f
357-
case FileID:
358-
s = string(f)
359-
case FileURL:
360-
s = string(f)
361-
default:
362-
return nil, errors.New(ErrBadFileType)
363-
}
364-
365-
params[file.Name] = s
332+
params[file.Name] = file.Data.SendData()
366333
}
367334
}
368335

bot_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestCopyMessage(t *testing.T) {
127127
func TestSendWithNewPhoto(t *testing.T) {
128128
bot, _ := getBot(t)
129129

130-
msg := NewPhoto(ChatID, "tests/image.jpg")
130+
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
131131
msg.Caption = "Test"
132132
_, err := bot.Send(msg)
133133

@@ -169,7 +169,7 @@ func TestSendWithNewPhotoWithFileReader(t *testing.T) {
169169
func TestSendWithNewPhotoReply(t *testing.T) {
170170
bot, _ := getBot(t)
171171

172-
msg := NewPhoto(ChatID, "tests/image.jpg")
172+
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
173173
msg.ReplyToMessageID = ReplyToMessageID
174174

175175
_, err := bot.Send(msg)
@@ -182,7 +182,7 @@ func TestSendWithNewPhotoReply(t *testing.T) {
182182
func TestSendNewPhotoToChannel(t *testing.T) {
183183
bot, _ := getBot(t)
184184

185-
msg := NewPhotoToChannel(Channel, "tests/image.jpg")
185+
msg := NewPhotoToChannel(Channel, FilePath("tests/image.jpg"))
186186
msg.Caption = "Test"
187187
_, err := bot.Send(msg)
188188

@@ -239,7 +239,7 @@ func TestSendWithExistingPhoto(t *testing.T) {
239239
func TestSendWithNewDocument(t *testing.T) {
240240
bot, _ := getBot(t)
241241

242-
msg := NewDocument(ChatID, "tests/image.jpg")
242+
msg := NewDocument(ChatID, FilePath("tests/image.jpg"))
243243
_, err := bot.Send(msg)
244244

245245
if err != nil {
@@ -250,8 +250,8 @@ func TestSendWithNewDocument(t *testing.T) {
250250
func TestSendWithNewDocumentAndThumb(t *testing.T) {
251251
bot, _ := getBot(t)
252252

253-
msg := NewDocument(ChatID, "tests/voice.ogg")
254-
msg.Thumb = "tests/image.jpg"
253+
msg := NewDocument(ChatID, FilePath("tests/voice.ogg"))
254+
msg.Thumb = FilePath("tests/image.jpg")
255255
_, err := bot.Send(msg)
256256

257257
if err != nil {
@@ -273,7 +273,7 @@ func TestSendWithExistingDocument(t *testing.T) {
273273
func TestSendWithNewAudio(t *testing.T) {
274274
bot, _ := getBot(t)
275275

276-
msg := NewAudio(ChatID, "tests/audio.mp3")
276+
msg := NewAudio(ChatID, FilePath("tests/audio.mp3"))
277277
msg.Title = "TEST"
278278
msg.Duration = 10
279279
msg.Performer = "TEST"
@@ -302,7 +302,7 @@ func TestSendWithExistingAudio(t *testing.T) {
302302
func TestSendWithNewVoice(t *testing.T) {
303303
bot, _ := getBot(t)
304304

305-
msg := NewVoice(ChatID, "tests/voice.ogg")
305+
msg := NewVoice(ChatID, FilePath("tests/voice.ogg"))
306306
msg.Duration = 10
307307
_, err := bot.Send(msg)
308308

@@ -356,7 +356,7 @@ func TestSendWithVenue(t *testing.T) {
356356
func TestSendWithNewVideo(t *testing.T) {
357357
bot, _ := getBot(t)
358358

359-
msg := NewVideo(ChatID, "tests/video.mp4")
359+
msg := NewVideo(ChatID, FilePath("tests/video.mp4"))
360360
msg.Duration = 10
361361
msg.Caption = "TEST"
362362

@@ -384,7 +384,7 @@ func TestSendWithExistingVideo(t *testing.T) {
384384
func TestSendWithNewVideoNote(t *testing.T) {
385385
bot, _ := getBot(t)
386386

387-
msg := NewVideoNote(ChatID, 240, "tests/videonote.mp4")
387+
msg := NewVideoNote(ChatID, 240, FilePath("tests/videonote.mp4"))
388388
msg.Duration = 10
389389

390390
_, err := bot.Send(msg)
@@ -410,7 +410,7 @@ func TestSendWithExistingVideoNote(t *testing.T) {
410410
func TestSendWithNewSticker(t *testing.T) {
411411
bot, _ := getBot(t)
412412

413-
msg := NewSticker(ChatID, "tests/image.jpg")
413+
msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
414414

415415
_, err := bot.Send(msg)
416416

@@ -434,7 +434,7 @@ func TestSendWithExistingSticker(t *testing.T) {
434434
func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
435435
bot, _ := getBot(t)
436436

437-
msg := NewSticker(ChatID, "tests/image.jpg")
437+
msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
438438
msg.ReplyMarkup = ReplyKeyboardRemove{
439439
RemoveKeyboard: true,
440440
Selective: false,
@@ -550,7 +550,7 @@ func TestSetWebhookWithCert(t *testing.T) {
550550

551551
bot.Request(DeleteWebhookConfig{})
552552

553-
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
553+
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, FilePath("tests/cert.pem"))
554554

555555
if err != nil {
556556
t.Error(err)
@@ -609,8 +609,8 @@ func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
609609

610610
cfg := NewMediaGroup(ChatID, []interface{}{
611611
NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
612-
NewInputMediaPhoto("tests/image.jpg"),
613-
NewInputMediaVideo("tests/video.mp4"),
612+
NewInputMediaPhoto(FilePath("tests/image.jpg")),
613+
NewInputMediaVideo(FilePath("tests/video.mp4")),
614614
})
615615

616616
messages, err := bot.SendMediaGroup(cfg)
@@ -632,7 +632,7 @@ func TestSendWithMediaGroupDocument(t *testing.T) {
632632

633633
cfg := NewMediaGroup(ChatID, []interface{}{
634634
NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
635-
NewInputMediaDocument("tests/image.jpg"),
635+
NewInputMediaDocument(FilePath("tests/image.jpg")),
636636
})
637637

638638
messages, err := bot.SendMediaGroup(cfg)
@@ -653,8 +653,8 @@ func TestSendWithMediaGroupAudio(t *testing.T) {
653653
bot, _ := getBot(t)
654654

655655
cfg := NewMediaGroup(ChatID, []interface{}{
656-
NewInputMediaAudio("tests/audio.mp3"),
657-
NewInputMediaAudio("tests/audio.mp3"),
656+
NewInputMediaAudio(FilePath("tests/audio.mp3")),
657+
NewInputMediaAudio(FilePath("tests/audio.mp3")),
658658
})
659659

660660
messages, err := bot.SendMediaGroup(cfg)
@@ -715,7 +715,7 @@ func ExampleNewWebhook() {
715715

716716
log.Printf("Authorized on account %s", bot.Self.UserName)
717717

718-
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
718+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
719719

720720
if err != nil {
721721
panic(err)
@@ -755,7 +755,7 @@ func ExampleWebhookHandler() {
755755

756756
log.Printf("Authorized on account %s", bot.Self.UserName)
757757

758-
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
758+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
759759

760760
if err != nil {
761761
panic(err)
@@ -1019,7 +1019,7 @@ func TestCommands(t *testing.T) {
10191019
// ChatID: ChatID,
10201020
// MessageID: m.MessageID,
10211021
// },
1022-
// Media: NewInputMediaVideo("tests/video.mp4"),
1022+
// Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
10231023
// }
10241024

10251025
// _, err = bot.Request(edit)
@@ -1030,17 +1030,17 @@ func TestCommands(t *testing.T) {
10301030

10311031
func TestPrepareInputMediaForParams(t *testing.T) {
10321032
media := []interface{}{
1033-
NewInputMediaPhoto("tests/image.jpg"),
1033+
NewInputMediaPhoto(FilePath("tests/image.jpg")),
10341034
NewInputMediaVideo(FileID("test")),
10351035
}
10361036

10371037
prepared := prepareInputMediaForParams(media)
10381038

1039-
if media[0].(InputMediaPhoto).Media != "tests/image.jpg" {
1039+
if media[0].(InputMediaPhoto).Media != FilePath("tests/image.jpg") {
10401040
t.Error("Original media was changed")
10411041
}
10421042

1043-
if prepared[0].(InputMediaPhoto).Media != "attach://file-0" {
1043+
if prepared[0].(InputMediaPhoto).Media != fileAttach("attach://file-0") {
10441044
t.Error("New media was not replaced")
10451045
}
10461046

0 commit comments

Comments
 (0)