From 898e79fe47dafa598b3f61dd570aa2921628e530 Mon Sep 17 00:00:00 2001 From: Syfaro Date: Fri, 21 Sep 2018 20:20:28 -0500 Subject: [PATCH] Add initial support for sendMediaGroup. --- bot_test.go | 14 ++++++++++++++ configs.go | 26 ++++++++++++++++++++++++++ helpers.go | 28 ++++++++++++++++++++++++++++ log.go | 3 ++- types.go | 21 +++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/bot_test.go b/bot_test.go index e7aa7ac..e9cfc79 100644 --- a/bot_test.go +++ b/bot_test.go @@ -519,6 +519,20 @@ func TestUpdatesChan(t *testing.T) { } } +func TestSendWithMediaGroup(t *testing.T) { + bot, _ := getBot(t) + + cfg := tgbotapi.NewMediaGroup(ChatID, []interface{}{ + tgbotapi.NewInputMediaPhoto("https://i.imgur.com/unQLJIb.jpg"), + tgbotapi.NewInputMediaPhoto("https://i.imgur.com/J5qweNZ.jpg"), + tgbotapi.NewInputMediaVideo("https://i.imgur.com/F6RmI24.mp4"), + }) + _, err := bot.Send(cfg) + if err != nil { + t.Error(err) + } +} + func ExampleNewBotAPI() { bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") if err != nil { diff --git a/configs.go b/configs.go index be12d3a..181d4e4 100644 --- a/configs.go +++ b/configs.go @@ -657,6 +657,32 @@ func (config VoiceConfig) method() string { return "sendVoice" } +// MediaGroupConfig contains information about a sendMediaGroup request. +type MediaGroupConfig struct { + BaseChat + InputMedia []interface{} +} + +func (config MediaGroupConfig) values() (url.Values, error) { + v, err := config.BaseChat.values() + if err != nil { + return v, err + } + + data, err := json.Marshal(config.InputMedia) + if err != nil { + return v, err + } + + v.Add("media", string(data)) + + return v, nil +} + +func (config MediaGroupConfig) method() string { + return "sendMediaGroup" +} + // LocationConfig contains information about a SendLocation request. type LocationConfig struct { BaseChat diff --git a/helpers.go b/helpers.go index cdff850..f49cbba 100644 --- a/helpers.go +++ b/helpers.go @@ -18,6 +18,7 @@ func NewMessage(chatID int64, text string) MessageConfig { } } +// NewDeleteMessage creates a request to delete a message. func NewDeleteMessage(chatID int64, messageID int) DeleteMessageConfig { return DeleteMessageConfig{ ChatID: chatID, @@ -289,6 +290,33 @@ func NewVoiceShare(chatID int64, fileID string) VoiceConfig { } } +// NewMediaGroup creates a new media group. Files should be an array of +// two to ten InputMediaPhoto or InputMediaVideo. +func NewMediaGroup(chatID int64, files []interface{}) MediaGroupConfig { + return MediaGroupConfig{ + BaseChat: BaseChat{ + ChatID: chatID, + }, + InputMedia: files, + } +} + +// NewInputMediaPhoto creates a new InputMediaPhoto. +func NewInputMediaPhoto(media string) InputMediaPhoto { + return InputMediaPhoto{ + Type: "photo", + Media: media, + } +} + +// NewInputMediaVideo creates a new InputMediaVideo. +func NewInputMediaVideo(media string) InputMediaVideo { + return InputMediaVideo{ + Type: "video", + Media: media, + } +} + // NewContact allows you to send a shared contact. func NewContact(chatID int64, phoneNumber, firstName string) ContactConfig { return ContactConfig{ diff --git a/log.go b/log.go index 18e339f..7a99bbd 100644 --- a/log.go +++ b/log.go @@ -1,13 +1,14 @@ package tgbotapi import ( - "os" "errors" stdlog "log" + "os" ) var log = stdlog.New(os.Stderr, "", stdlog.LstdFlags) +// SetLogger specifies the logger that the package should use. func SetLogger(newLog *stdlog.Logger) error { if newLog == nil { return errors.New("logger is nil") diff --git a/types.go b/types.go index cc315f4..b2fc704 100644 --- a/types.go +++ b/types.go @@ -524,6 +524,27 @@ func (info WebhookInfo) IsSet() bool { return info.URL != "" } +// InputMediaPhoto contains a photo for displaying as part of a media group. +type InputMediaPhoto struct { + Type string `json:"type"` + Media string `json:"media"` + Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` +} + +// InputMediaVideo contains a video for displaying as part of a media group. +type InputMediaVideo struct { + Type string `json:"type"` + Media string `json:"media"` + // thumb intentionally missing as it is not currently compatible + Caption string `json:"caption"` + ParseMode string `json:"parse_mode"` + Width int `json:"width"` + Height int `json:"height"` + Duration int `json:"duration"` + SupportsStreaming bool `json:"supports_streaming"` +} + // InlineQuery is a Query from Telegram for an inline request. type InlineQuery struct { ID string `json:"id"`