Skip to content

Commit

Permalink
media: implement SetCaption in Album
Browse files Browse the repository at this point in the history
  • Loading branch information
nentenpizza authored and demget committed Mar 5, 2024
1 parent cb88a11 commit 260a67b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ type Inputtable interface {
// Album lets you group multiple media into a single message.
type Album []Inputtable

func (a Album) SetCaption(caption string) {
if len(a) < 1 {
return
}
switch a[0].MediaType() {
case "audio":
a[0].(*Audio).Caption = caption
case "video":
a[0].(*Video).Caption = caption
case "document":
a[0].(*Document).Caption = caption
case "photo":
a[0].(*Photo).Caption = caption
case "animation":
a[0].(*Animation).Caption = caption
}
}

// Photo object represents a single photo file.
type Photo struct {
File
Expand Down
43 changes: 43 additions & 0 deletions media_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package telebot

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestAlbumSetCaption(t *testing.T) {
var a Album
a = append(a, &Photo{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)

a = Album{}
a = append(a, &Animation{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)

a = Album{}
a = append(a, &Audio{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)

a = Album{}
a = append(a, &Document{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)

a = Album{}
a = append(a, &Video{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)
}

0 comments on commit 260a67b

Please sign in to comment.