Skip to content

Commit badaa40

Browse files
authored
Merge pull request #356 from go-telegram-bot-api/multiple-uploads
Add support for uploading multiple files
2 parents d10c681 + 05db49c commit badaa40

File tree

8 files changed

+738
-566
lines changed

8 files changed

+738
-566
lines changed

bot.go

Lines changed: 129 additions & 117 deletions
Large diffs are not rendered by default.

bot_test.go

Lines changed: 133 additions & 32 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 := NewPhotoUpload(ChatID, "tests/image.jpg")
130+
msg := NewPhoto(ChatID, "tests/image.jpg")
131131
msg.Caption = "Test"
132132
_, err := bot.Send(msg)
133133

@@ -142,7 +142,7 @@ func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
142142
data, _ := ioutil.ReadFile("tests/image.jpg")
143143
b := FileBytes{Name: "image.jpg", Bytes: data}
144144

145-
msg := NewPhotoUpload(ChatID, b)
145+
msg := NewPhoto(ChatID, b)
146146
msg.Caption = "Test"
147147
_, err := bot.Send(msg)
148148

@@ -155,9 +155,9 @@ func TestSendWithNewPhotoWithFileReader(t *testing.T) {
155155
bot, _ := getBot(t)
156156

157157
f, _ := os.Open("tests/image.jpg")
158-
reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}
158+
reader := FileReader{Name: "image.jpg", Reader: f}
159159

160-
msg := NewPhotoUpload(ChatID, reader)
160+
msg := NewPhoto(ChatID, reader)
161161
msg.Caption = "Test"
162162
_, err := bot.Send(msg)
163163

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

172-
msg := NewPhotoUpload(ChatID, "tests/image.jpg")
172+
msg := NewPhoto(ChatID, "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 := NewPhotoUploadToChannel(Channel, "tests/image.jpg")
185+
msg := NewPhotoToChannel(Channel, "tests/image.jpg")
186186
msg.Caption = "Test"
187187
_, err := bot.Send(msg)
188188

@@ -198,7 +198,7 @@ func TestSendNewPhotoToChannelFileBytes(t *testing.T) {
198198
data, _ := ioutil.ReadFile("tests/image.jpg")
199199
b := FileBytes{Name: "image.jpg", Bytes: data}
200200

201-
msg := NewPhotoUploadToChannel(Channel, b)
201+
msg := NewPhotoToChannel(Channel, b)
202202
msg.Caption = "Test"
203203
_, err := bot.Send(msg)
204204

@@ -212,9 +212,9 @@ func TestSendNewPhotoToChannelFileReader(t *testing.T) {
212212
bot, _ := getBot(t)
213213

214214
f, _ := os.Open("tests/image.jpg")
215-
reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}
215+
reader := FileReader{Name: "image.jpg", Reader: f}
216216

217-
msg := NewPhotoUploadToChannel(Channel, reader)
217+
msg := NewPhotoToChannel(Channel, reader)
218218
msg.Caption = "Test"
219219
_, err := bot.Send(msg)
220220

@@ -227,7 +227,7 @@ func TestSendNewPhotoToChannelFileReader(t *testing.T) {
227227
func TestSendWithExistingPhoto(t *testing.T) {
228228
bot, _ := getBot(t)
229229

230-
msg := NewPhotoShare(ChatID, ExistingPhotoFileID)
230+
msg := NewPhoto(ChatID, FileID(ExistingPhotoFileID))
231231
msg.Caption = "Test"
232232
_, err := bot.Send(msg)
233233

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

242-
msg := NewDocumentUpload(ChatID, "tests/image.jpg")
242+
msg := NewDocument(ChatID, "tests/image.jpg")
243+
_, err := bot.Send(msg)
244+
245+
if err != nil {
246+
t.Error(err)
247+
}
248+
}
249+
250+
func TestSendWithNewDocumentAndThumb(t *testing.T) {
251+
bot, _ := getBot(t)
252+
253+
msg := NewDocument(ChatID, "tests/voice.ogg")
254+
msg.Thumb = "tests/image.jpg"
243255
_, err := bot.Send(msg)
244256

245257
if err != nil {
@@ -250,7 +262,7 @@ func TestSendWithNewDocument(t *testing.T) {
250262
func TestSendWithExistingDocument(t *testing.T) {
251263
bot, _ := getBot(t)
252264

253-
msg := NewDocumentShare(ChatID, ExistingDocumentFileID)
265+
msg := NewDocument(ChatID, FileID(ExistingDocumentFileID))
254266
_, err := bot.Send(msg)
255267

256268
if err != nil {
@@ -261,12 +273,10 @@ func TestSendWithExistingDocument(t *testing.T) {
261273
func TestSendWithNewAudio(t *testing.T) {
262274
bot, _ := getBot(t)
263275

264-
msg := NewAudioUpload(ChatID, "tests/audio.mp3")
276+
msg := NewAudio(ChatID, "tests/audio.mp3")
265277
msg.Title = "TEST"
266278
msg.Duration = 10
267279
msg.Performer = "TEST"
268-
msg.MimeType = "audio/mpeg"
269-
msg.FileSize = 688
270280
_, err := bot.Send(msg)
271281

272282
if err != nil {
@@ -277,7 +287,7 @@ func TestSendWithNewAudio(t *testing.T) {
277287
func TestSendWithExistingAudio(t *testing.T) {
278288
bot, _ := getBot(t)
279289

280-
msg := NewAudioShare(ChatID, ExistingAudioFileID)
290+
msg := NewAudio(ChatID, FileID(ExistingAudioFileID))
281291
msg.Title = "TEST"
282292
msg.Duration = 10
283293
msg.Performer = "TEST"
@@ -292,7 +302,7 @@ func TestSendWithExistingAudio(t *testing.T) {
292302
func TestSendWithNewVoice(t *testing.T) {
293303
bot, _ := getBot(t)
294304

295-
msg := NewVoiceUpload(ChatID, "tests/voice.ogg")
305+
msg := NewVoice(ChatID, "tests/voice.ogg")
296306
msg.Duration = 10
297307
_, err := bot.Send(msg)
298308

@@ -304,7 +314,7 @@ func TestSendWithNewVoice(t *testing.T) {
304314
func TestSendWithExistingVoice(t *testing.T) {
305315
bot, _ := getBot(t)
306316

307-
msg := NewVoiceShare(ChatID, ExistingVoiceFileID)
317+
msg := NewVoice(ChatID, FileID(ExistingVoiceFileID))
308318
msg.Duration = 10
309319
_, err := bot.Send(msg)
310320

@@ -346,7 +356,7 @@ func TestSendWithVenue(t *testing.T) {
346356
func TestSendWithNewVideo(t *testing.T) {
347357
bot, _ := getBot(t)
348358

349-
msg := NewVideoUpload(ChatID, "tests/video.mp4")
359+
msg := NewVideo(ChatID, "tests/video.mp4")
350360
msg.Duration = 10
351361
msg.Caption = "TEST"
352362

@@ -360,7 +370,7 @@ func TestSendWithNewVideo(t *testing.T) {
360370
func TestSendWithExistingVideo(t *testing.T) {
361371
bot, _ := getBot(t)
362372

363-
msg := NewVideoShare(ChatID, ExistingVideoFileID)
373+
msg := NewVideo(ChatID, FileID(ExistingVideoFileID))
364374
msg.Duration = 10
365375
msg.Caption = "TEST"
366376

@@ -374,7 +384,7 @@ func TestSendWithExistingVideo(t *testing.T) {
374384
func TestSendWithNewVideoNote(t *testing.T) {
375385
bot, _ := getBot(t)
376386

377-
msg := NewVideoNoteUpload(ChatID, 240, "tests/videonote.mp4")
387+
msg := NewVideoNote(ChatID, 240, "tests/videonote.mp4")
378388
msg.Duration = 10
379389

380390
_, err := bot.Send(msg)
@@ -387,7 +397,7 @@ func TestSendWithNewVideoNote(t *testing.T) {
387397
func TestSendWithExistingVideoNote(t *testing.T) {
388398
bot, _ := getBot(t)
389399

390-
msg := NewVideoNoteShare(ChatID, 240, ExistingVideoNoteFileID)
400+
msg := NewVideoNote(ChatID, 240, FileID(ExistingVideoNoteFileID))
391401
msg.Duration = 10
392402

393403
_, err := bot.Send(msg)
@@ -400,7 +410,7 @@ func TestSendWithExistingVideoNote(t *testing.T) {
400410
func TestSendWithNewSticker(t *testing.T) {
401411
bot, _ := getBot(t)
402412

403-
msg := NewStickerUpload(ChatID, "tests/image.jpg")
413+
msg := NewSticker(ChatID, "tests/image.jpg")
404414

405415
_, err := bot.Send(msg)
406416

@@ -412,7 +422,7 @@ func TestSendWithNewSticker(t *testing.T) {
412422
func TestSendWithExistingSticker(t *testing.T) {
413423
bot, _ := getBot(t)
414424

415-
msg := NewStickerShare(ChatID, ExistingStickerFileID)
425+
msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
416426

417427
_, err := bot.Send(msg)
418428

@@ -424,7 +434,7 @@ func TestSendWithExistingSticker(t *testing.T) {
424434
func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
425435
bot, _ := getBot(t)
426436

427-
msg := NewStickerUpload(ChatID, "tests/image.jpg")
437+
msg := NewSticker(ChatID, "tests/image.jpg")
428438
msg.ReplyMarkup = ReplyKeyboardRemove{
429439
RemoveKeyboard: true,
430440
Selective: false,
@@ -439,7 +449,7 @@ func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
439449
func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
440450
bot, _ := getBot(t)
441451

442-
msg := NewStickerShare(ChatID, ExistingStickerFileID)
452+
msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
443453
msg.ReplyMarkup = ReplyKeyboardRemove{
444454
RemoveKeyboard: true,
445455
Selective: false,
@@ -583,12 +593,13 @@ func TestSetWebhookWithoutCert(t *testing.T) {
583593
bot.Request(DeleteWebhookConfig{})
584594
}
585595

586-
func TestSendWithMediaGroup(t *testing.T) {
596+
func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
587597
bot, _ := getBot(t)
588598

589599
cfg := NewMediaGroup(ChatID, []interface{}{
590-
NewInputMediaPhoto("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg"),
591-
NewInputMediaVideo("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/video.mp4"),
600+
NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
601+
NewInputMediaPhoto("tests/image.jpg"),
602+
NewInputMediaVideo("tests/video.mp4"),
592603
})
593604

594605
messages, err := bot.SendMediaGroup(cfg)
@@ -597,11 +608,55 @@ func TestSendWithMediaGroup(t *testing.T) {
597608
}
598609

599610
if messages == nil {
600-
t.Error()
611+
t.Error("No received messages")
601612
}
602613

603-
if len(messages) != 2 {
604-
t.Error()
614+
if len(messages) != len(cfg.Media) {
615+
t.Errorf("Different number of messages: %d", len(messages))
616+
}
617+
}
618+
619+
func TestSendWithMediaGroupDocument(t *testing.T) {
620+
bot, _ := getBot(t)
621+
622+
cfg := NewMediaGroup(ChatID, []interface{}{
623+
NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
624+
NewInputMediaDocument("tests/image.jpg"),
625+
})
626+
627+
messages, err := bot.SendMediaGroup(cfg)
628+
if err != nil {
629+
t.Error(err)
630+
}
631+
632+
if messages == nil {
633+
t.Error("No received messages")
634+
}
635+
636+
if len(messages) != len(cfg.Media) {
637+
t.Errorf("Different number of messages: %d", len(messages))
638+
}
639+
}
640+
641+
func TestSendWithMediaGroupAudio(t *testing.T) {
642+
bot, _ := getBot(t)
643+
644+
cfg := NewMediaGroup(ChatID, []interface{}{
645+
NewInputMediaAudio("tests/audio.mp3"),
646+
NewInputMediaAudio("tests/audio.mp3"),
647+
})
648+
649+
messages, err := bot.SendMediaGroup(cfg)
650+
if err != nil {
651+
t.Error(err)
652+
}
653+
654+
if messages == nil {
655+
t.Error("No received messages")
656+
}
657+
658+
if len(messages) != len(cfg.Media) {
659+
t.Errorf("Different number of messages: %d", len(messages))
605660
}
606661
}
607662

@@ -899,3 +954,49 @@ func TestSetCommands(t *testing.T) {
899954
t.Error("Commands were incorrectly set")
900955
}
901956
}
957+
958+
func TestEditMessageMedia(t *testing.T) {
959+
bot, _ := getBot(t)
960+
961+
msg := NewPhoto(ChatID, "tests/image.jpg")
962+
msg.Caption = "Test"
963+
m, err := bot.Send(msg)
964+
965+
if err != nil {
966+
t.Error(err)
967+
}
968+
969+
edit := EditMessageMediaConfig{
970+
BaseEdit: BaseEdit{
971+
ChatID: ChatID,
972+
MessageID: m.MessageID,
973+
},
974+
Media: NewInputMediaVideo("tests/video.mp4"),
975+
}
976+
977+
_, err = bot.Request(edit)
978+
if err != nil {
979+
t.Error(err)
980+
}
981+
}
982+
983+
func TestPrepareInputMediaForParams(t *testing.T) {
984+
media := []interface{}{
985+
NewInputMediaPhoto("tests/image.jpg"),
986+
NewInputMediaVideo(FileID("test")),
987+
}
988+
989+
prepared := prepareInputMediaForParams(media)
990+
991+
if media[0].(InputMediaPhoto).Media != "tests/image.jpg" {
992+
t.Error("Original media was changed")
993+
}
994+
995+
if prepared[0].(InputMediaPhoto).Media != "attach://file-0" {
996+
t.Error("New media was not replaced")
997+
}
998+
999+
if prepared[1].(InputMediaVideo).Media != FileID("test") {
1000+
t.Error("Passthrough value was not the same")
1001+
}
1002+
}

0 commit comments

Comments
 (0)