Skip to content

Commit

Permalink
Merge 712611e into 509315f
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelyabuzhsky committed Oct 21, 2020
2 parents 509315f + 712611e commit 99a2d23
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
12 changes: 7 additions & 5 deletions senders/telegram/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,18 @@ func (sender *Sender) sendAsMessage(chat *telebot.Chat, message string) error {
return nil
}

func (sender *Sender) sendAsAlbum(chat *telebot.Chat, plots [][]byte, caption string) error {
func prepareAlbum(plots [][]byte, caption string) telebot.Album {
var album telebot.Album
firstPhoto := true
for _, plot := range plots {
photo := &telebot.Photo{File: telebot.FromReader(bytes.NewReader(plot)), Caption: caption}
album = append(album, photo)
if firstPhoto {
caption = "" // Caption should be defined only for first photo
}
caption = "" // Caption should be defined only for first photo
}
return album
}

func (sender *Sender) sendAsAlbum(chat *telebot.Chat, plots [][]byte, caption string) error {
album := prepareAlbum(plots, caption)

_, err := sender.bot.SendAlbum(chat, album)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions senders/telegram/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/moira-alert/moira/database"
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/tucnak/telebot.v2"
)

func TestBuildMessage(t *testing.T) {
Expand Down Expand Up @@ -145,3 +146,25 @@ func TestGetChatUID(t *testing.T) {
})
})
}

func TestPrepareAlbum(t *testing.T) {
Convey("Prepare album", t, func() {
Convey("Only the first photo of the album has a caption", func() {
Convey("An album with one photo", func() {
plots := [][]byte{{1, 0, 1}}
album := prepareAlbum(plots, "caption")

So(album[0].(*telebot.Photo).Caption, ShouldEqual, "caption")
})

Convey("An album with several photos", func() {
plots := [][]byte{{1, 0, 1}, {1, 0, 0}, {0, 0, 1}}
album := prepareAlbum(plots, "caption")

So(album[0].(*telebot.Photo).Caption, ShouldEqual, "caption")
So(album[1].(*telebot.Photo).Caption, ShouldEqual, "")
So(album[2].(*telebot.Photo).Caption, ShouldEqual, "")
})
})
})
}

0 comments on commit 99a2d23

Please sign in to comment.