Skip to content

Commit

Permalink
bot: fix media file name setting and parse mode passing in SendAlbum
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Aug 13, 2021
1 parent 915dfba commit 3f7ecbd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (b *Bot) sendFiles(method string, files map[string]File, params map[string]
defer pipeWriter.Close()

for field, file := range rawFiles {
if err := addFileToWriter(writer, files[field].FileName, field, file); err != nil {
if err := addFileToWriter(writer, files[field].fileName, field, file); err != nil {
pipeWriter.CloseWithError(err)
return
}
Expand Down
7 changes: 5 additions & 2 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ func TestBot(t *testing.T) {
_, err = b.SendAlbum(to, nil)
assert.Error(t, err)

msgs, err := b.SendAlbum(to, Album{photo, photo})
photo2 := *photo
photo2.Caption = ""

msgs, err := b.SendAlbum(to, Album{photo, &photo2}, ModeHTML)
require.NoError(t, err)
assert.Len(t, msgs, 2)
assert.NotEmpty(t, msgs[0].AlbumID)
Expand Down Expand Up @@ -433,7 +436,7 @@ func TestBot(t *testing.T) {
assert.NotNil(t, edited.Location)
})

// should be the last
// should be after Edit tests
t.Run("Delete()", func(t *testing.T) {
require.NoError(t, b.Delete(msg))
})
Expand Down
3 changes: 2 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
type File struct {
FileID string `json:"file_id"`
UniqueID string `json:"file_unique_id"`
FileName string `json:"file_name"`
FileSize int `json:"file_size"`

// file on telegram server https://core.telegram.org/bots/api#file
Expand All @@ -23,6 +22,8 @@ type File struct {

// file backed with io.Reader
FileReader io.Reader `json:"-"`

fileName string
}

// FromDisk constructs a new local (on-disk) file object.
Expand Down
4 changes: 4 additions & 0 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Audio struct {

// MediaFile returns &Audio.File
func (a *Audio) MediaFile() *File {
a.fileName = a.FileName
return &a.File
}

Expand All @@ -103,6 +104,7 @@ type Document struct {

// MediaFile returns &Document.File
func (d *Document) MediaFile() *File {
d.fileName = d.FileName
return &d.File
}

Expand All @@ -125,6 +127,7 @@ type Video struct {

// MediaFile returns &Video.File
func (v *Video) MediaFile() *File {
v.fileName = v.FileName
return &v.File
}

Expand All @@ -145,6 +148,7 @@ type Animation struct {

// MediaFile returns &Animation.File
func (a *Animation) MediaFile() *File {
a.fileName = a.FileName
return &a.File
}

Expand Down
12 changes: 1 addition & 11 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,15 @@ func extractMessage(data []byte) (*Message, error) {
}

func extractOptions(how []interface{}) *SendOptions {
var opts *SendOptions
opts := &SendOptions{}

for _, prop := range how {
switch opt := prop.(type) {
case *SendOptions:
opts = opt.copy()
case *ReplyMarkup:
if opts == nil {
opts = &SendOptions{}
}
opts.ReplyMarkup = opt.copy()
case Option:
if opts == nil {
opts = &SendOptions{}
}

switch opt {
case NoPreview:
opts.DisableWebPagePreview = true
Expand All @@ -158,9 +151,6 @@ func extractOptions(how []interface{}) *SendOptions {
panic("telebot: unsupported flag-option")
}
case ParseMode:
if opts == nil {
opts = &SendOptions{}
}
opts.ParseMode = opt
default:
panic("telebot: unsupported send-option")
Expand Down

0 comments on commit 3f7ecbd

Please sign in to comment.