Skip to content
Merged
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
61 changes: 14 additions & 47 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package tgbotapi

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -12,7 +11,6 @@ import (
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"time"
)
Expand Down Expand Up @@ -185,54 +183,37 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
}

for _, file := range files {
switch f := file.File.(type) {
case string:
fileHandle, err := os.Open(f)
if file.Data.NeedsUpload() {
name, reader, err := file.Data.UploadData()
if err != nil {
w.CloseWithError(err)
return
}
defer fileHandle.Close()

part, err := m.CreateFormFile(file.Name, fileHandle.Name())
part, err := m.CreateFormFile(file.Name, name)
if err != nil {
w.CloseWithError(err)
return
}

io.Copy(part, fileHandle)
case FileBytes:
part, err := m.CreateFormFile(file.Name, f.Name)
if err != nil {
if _, err := io.Copy(part, reader); err != nil {
w.CloseWithError(err)
return
}

buf := bytes.NewBuffer(f.Bytes)
io.Copy(part, buf)
case FileReader:
part, err := m.CreateFormFile(file.Name, f.Name)
if err != nil {
w.CloseWithError(err)
return
if closer, ok := reader.(io.ReadCloser); ok {
if err = closer.Close(); err != nil {
w.CloseWithError(err)
return
}
}
} else {
value := file.Data.SendData()

io.Copy(part, f.Reader)
case FileURL:
val := string(f)
if err := m.WriteField(file.Name, val); err != nil {
w.CloseWithError(err)
return
}
case FileID:
val := string(f)
if err := m.WriteField(file.Name, val); err != nil {
if err := m.WriteField(file.Name, value); err != nil {
w.CloseWithError(err)
return
}
default:
w.CloseWithError(errors.New(ErrBadFileType))
return
}
}
}()
Expand Down Expand Up @@ -321,8 +302,7 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool {

func hasFilesNeedingUpload(files []RequestFile) bool {
for _, file := range files {
switch file.File.(type) {
case string, FileBytes, FileReader:
if file.Data.NeedsUpload() {
return true
}
}
Expand All @@ -349,20 +329,7 @@ func (bot *BotAPI) Request(c Chattable) (*APIResponse, error) {
// However, if there are no files to be uploaded, there's likely things
// that need to be turned into params instead.
for _, file := range files {
var s string

switch f := file.File.(type) {
case string:
s = f
case FileID:
s = string(f)
case FileURL:
s = string(f)
default:
return nil, errors.New(ErrBadFileType)
}

params[file.Name] = s
params[file.Name] = file.Data.SendData()
}
}

Expand Down
48 changes: 24 additions & 24 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestCopyMessage(t *testing.T) {
func TestSendWithNewPhoto(t *testing.T) {
bot, _ := getBot(t)

msg := NewPhoto(ChatID, "tests/image.jpg")
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
msg.Caption = "Test"
_, err := bot.Send(msg)

Expand Down Expand Up @@ -169,7 +169,7 @@ func TestSendWithNewPhotoWithFileReader(t *testing.T) {
func TestSendWithNewPhotoReply(t *testing.T) {
bot, _ := getBot(t)

msg := NewPhoto(ChatID, "tests/image.jpg")
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
msg.ReplyToMessageID = ReplyToMessageID

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

msg := NewPhotoToChannel(Channel, "tests/image.jpg")
msg := NewPhotoToChannel(Channel, FilePath("tests/image.jpg"))
msg.Caption = "Test"
_, err := bot.Send(msg)

Expand Down Expand Up @@ -239,7 +239,7 @@ func TestSendWithExistingPhoto(t *testing.T) {
func TestSendWithNewDocument(t *testing.T) {
bot, _ := getBot(t)

msg := NewDocument(ChatID, "tests/image.jpg")
msg := NewDocument(ChatID, FilePath("tests/image.jpg"))
_, err := bot.Send(msg)

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

msg := NewDocument(ChatID, "tests/voice.ogg")
msg.Thumb = "tests/image.jpg"
msg := NewDocument(ChatID, FilePath("tests/voice.ogg"))
msg.Thumb = FilePath("tests/image.jpg")
_, err := bot.Send(msg)

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

msg := NewAudio(ChatID, "tests/audio.mp3")
msg := NewAudio(ChatID, FilePath("tests/audio.mp3"))
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
Expand Down Expand Up @@ -302,7 +302,7 @@ func TestSendWithExistingAudio(t *testing.T) {
func TestSendWithNewVoice(t *testing.T) {
bot, _ := getBot(t)

msg := NewVoice(ChatID, "tests/voice.ogg")
msg := NewVoice(ChatID, FilePath("tests/voice.ogg"))
msg.Duration = 10
_, err := bot.Send(msg)

Expand Down Expand Up @@ -356,7 +356,7 @@ func TestSendWithVenue(t *testing.T) {
func TestSendWithNewVideo(t *testing.T) {
bot, _ := getBot(t)

msg := NewVideo(ChatID, "tests/video.mp4")
msg := NewVideo(ChatID, FilePath("tests/video.mp4"))
msg.Duration = 10
msg.Caption = "TEST"

Expand Down Expand Up @@ -384,7 +384,7 @@ func TestSendWithExistingVideo(t *testing.T) {
func TestSendWithNewVideoNote(t *testing.T) {
bot, _ := getBot(t)

msg := NewVideoNote(ChatID, 240, "tests/videonote.mp4")
msg := NewVideoNote(ChatID, 240, FilePath("tests/videonote.mp4"))
msg.Duration = 10

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

msg := NewSticker(ChatID, "tests/image.jpg")
msg := NewSticker(ChatID, FilePath("tests/image.jpg"))

_, err := bot.Send(msg)

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

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

bot.Request(DeleteWebhookConfig{})

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

if err != nil {
t.Error(err)
Expand Down Expand Up @@ -609,8 +609,8 @@ func TestSendWithMediaGroupPhotoVideo(t *testing.T) {

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

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

cfg := NewMediaGroup(ChatID, []interface{}{
NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
NewInputMediaDocument("tests/image.jpg"),
NewInputMediaDocument(FilePath("tests/image.jpg")),
})

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

cfg := NewMediaGroup(ChatID, []interface{}{
NewInputMediaAudio("tests/audio.mp3"),
NewInputMediaAudio("tests/audio.mp3"),
NewInputMediaAudio(FilePath("tests/audio.mp3")),
NewInputMediaAudio(FilePath("tests/audio.mp3")),
})

messages, err := bot.SendMediaGroup(cfg)
Expand Down Expand Up @@ -715,7 +715,7 @@ func ExampleNewWebhook() {

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

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

if err != nil {
panic(err)
Expand Down Expand Up @@ -755,7 +755,7 @@ func ExampleWebhookHandler() {

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

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

if err != nil {
panic(err)
Expand Down Expand Up @@ -1019,7 +1019,7 @@ func TestCommands(t *testing.T) {
// ChatID: ChatID,
// MessageID: m.MessageID,
// },
// Media: NewInputMediaVideo("tests/video.mp4"),
// Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
// }

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

func TestPrepareInputMediaForParams(t *testing.T) {
media := []interface{}{
NewInputMediaPhoto("tests/image.jpg"),
NewInputMediaPhoto(FilePath("tests/image.jpg")),
NewInputMediaVideo(FileID("test")),
}

prepared := prepareInputMediaForParams(media)

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

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

Expand Down
Loading