all repos — telegram-bot-api @ 898e79fe47dafa598b3f61dd570aa2921628e530

Golang bindings for the Telegram Bot API

Add initial support for sendMediaGroup.
Syfaro syfaro@huefox.com
Fri, 21 Sep 2018 20:20:28 -0500
commit

898e79fe47dafa598b3f61dd570aa2921628e530

parent

7ff5871e2875951ebf0fbdea7c174337c9600136

5 files changed, 91 insertions(+), 1 deletions(-)

jump to
M bot_test.gobot_test.go

@@ -519,6 +519,20 @@ t.Fail()

} } +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 {
M configs.goconfigs.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
M helpers.gohelpers.go

@@ -18,6 +18,7 @@ DisableWebPagePreview: false,

} } +// NewDeleteMessage creates a request to delete a message. func NewDeleteMessage(chatID int64, messageID int) DeleteMessageConfig { return DeleteMessageConfig{ ChatID: chatID,

@@ -286,6 +287,33 @@ BaseChat: BaseChat{ChatID: chatID},

FileID: fileID, UseExisting: true, }, + } +} + +// 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, } }
M log.golog.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")
M types.gotypes.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"`