all repos — telegram-bot-api @ 99b74b8efaa519636cf7f56afed97b65ecafb512

Golang bindings for the Telegram Bot API

Improve usability of new files for configs.
Syfaro syfaro@huefox.com
Sat, 25 Jul 2020 21:20:05 -0500
commit

99b74b8efaa519636cf7f56afed97b65ecafb512

parent

ce4fc988c916518bf64e8d02be6e19d89d745928

3 files changed, 154 insertions(+), 108 deletions(-)

jump to
M bot_test.gobot_test.go

@@ -216,7 +216,7 @@ func TestSendWithNewDocumentAndThumb(t *testing.T) {

bot, _ := getBot(t) msg := NewDocument(ChatID, "tests/voice.ogg") - msg.AddFile("thumb", "tests/image.jpg") + msg.Thumb = "tests/image.jpg" _, err := bot.Send(msg) if err != nil {

@@ -242,8 +242,6 @@ msg := NewAudio(ChatID, "tests/audio.mp3")

msg.Title = "TEST" msg.Duration = 10 msg.Performer = "TEST" - msg.MimeType = "audio/mpeg" - msg.FileSize = 688 _, err := bot.Send(msg) if err != nil {
M configs.goconfigs.go

@@ -94,34 +94,11 @@

// BaseFile is a base type for all file config types. type BaseFile struct { BaseChat - Files []RequestFile - MimeType string - FileSize int -} - -// AddFile specifies a file for a Telegram request. -func (file *BaseFile) AddFile(name string, f interface{}) { - if file.Files == nil { - file.Files = make([]RequestFile, 0, 1) - } - - file.Files = append(file.Files, RequestFile{ - Name: name, - File: f, - }) + File interface{} } func (file BaseFile) params() (Params, error) { - params, err := file.BaseChat.params() - - params.AddNonEmpty("mime_type", file.MimeType) - params.AddNonZero("file_size", file.FileSize) - - return params, err -} - -func (file BaseFile) files() []RequestFile { - return file.Files + return file.BaseChat.params() } // BaseEdit is base type of all chat edits.

@@ -200,6 +177,7 @@

// PhotoConfig contains information about a SendPhoto request. type PhotoConfig struct { BaseFile + Thumb interface{} Caption string ParseMode string }

@@ -213,17 +191,30 @@

return params, err } -func (config PhotoConfig) name() string { - return "photo" +func (config PhotoConfig) method() string { + return "sendPhoto" } -func (config PhotoConfig) method() string { - return "sendPhoto" +func (config PhotoConfig) files() []RequestFile { + files := []RequestFile{{ + Name: "photo", + File: config.File, + }} + + if config.Thumb != nil { + files = append(files, RequestFile{ + Name: "thumb", + File: config.Thumb, + }) + } + + return files } // AudioConfig contains information about a SendAudio request. type AudioConfig struct { BaseFile + Thumb interface{} Caption string ParseMode string Duration int

@@ -246,17 +237,30 @@

return params, nil } -func (config AudioConfig) name() string { - return "audio" +func (config AudioConfig) method() string { + return "sendAudio" } -func (config AudioConfig) method() string { - return "sendAudio" +func (config AudioConfig) files() []RequestFile { + files := []RequestFile{{ + Name: "audio", + File: config.File, + }} + + if config.Thumb != nil { + files = append(files, RequestFile{ + Name: "thumb", + File: config.Thumb, + }) + } + + return files } // DocumentConfig contains information about a SendDocument request. type DocumentConfig struct { BaseFile + Thumb interface{} Caption string ParseMode string }

@@ -270,14 +274,26 @@

return params, err } -func (config DocumentConfig) name() string { - return "document" -} - func (config DocumentConfig) method() string { return "sendDocument" } +func (config DocumentConfig) files() []RequestFile { + files := []RequestFile{{ + Name: "document", + File: config.File, + }} + + if config.Thumb != nil { + files = append(files, RequestFile{ + Name: "thumb", + File: config.Thumb, + }) + } + + return files +} + // StickerConfig contains information about a SendSticker request. type StickerConfig struct { BaseFile

@@ -287,17 +303,21 @@ func (config StickerConfig) params() (Params, error) {

return config.BaseChat.params() } -func (config StickerConfig) name() string { - return "sticker" +func (config StickerConfig) method() string { + return "sendSticker" } -func (config StickerConfig) method() string { - return "sendSticker" +func (config StickerConfig) files() []RequestFile { + return []RequestFile{{ + Name: "sticker", + File: config.File, + }} } // VideoConfig contains information about a SendVideo request. type VideoConfig struct { BaseFile + Thumb interface{} Duration int Caption string ParseMode string

@@ -315,18 +335,31 @@

return params, err } -func (config VideoConfig) name() string { - return "video" +func (config VideoConfig) method() string { + return "sendVideo" } -func (config VideoConfig) method() string { - return "sendVideo" +func (config VideoConfig) files() []RequestFile { + files := []RequestFile{{ + Name: "video", + File: config.File, + }} + + if config.Thumb != nil { + files = append(files, RequestFile{ + Name: "thumb", + File: config.Thumb, + }) + } + + return files } // AnimationConfig contains information about a SendAnimation request. type AnimationConfig struct { BaseFile Duration int + Thumb interface{} Caption string ParseMode string }

@@ -349,9 +382,26 @@ func (config AnimationConfig) method() string {

return "sendAnimation" } +func (config AnimationConfig) files() []RequestFile { + files := []RequestFile{{ + Name: "animation", + File: config.File, + }} + + if config.Thumb != nil { + files = append(files, RequestFile{ + Name: "thumb", + File: config.Thumb, + }) + } + + return files +} + // VideoNoteConfig contains information about a SendVideoNote request. type VideoNoteConfig struct { BaseFile + Thumb interface{} Duration int Length int }

@@ -365,17 +415,30 @@

return params, err } -func (config VideoNoteConfig) name() string { - return "video_note" +func (config VideoNoteConfig) method() string { + return "sendVideoNote" } -func (config VideoNoteConfig) method() string { - return "sendVideoNote" +func (config VideoNoteConfig) files() []RequestFile { + files := []RequestFile{{ + Name: "video_note", + File: config.File, + }} + + if config.Thumb != nil { + files = append(files, RequestFile{ + Name: "thumb", + File: config.Thumb, + }) + } + + return files } // VoiceConfig contains information about a SendVoice request. type VoiceConfig struct { BaseFile + Thumb interface{} Caption string ParseMode string Duration int

@@ -391,12 +454,24 @@

return params, err } -func (config VoiceConfig) name() string { - return "voice" +func (config VoiceConfig) method() string { + return "sendVoice" } -func (config VoiceConfig) method() string { - return "sendVoice" +func (config VoiceConfig) files() []RequestFile { + files := []RequestFile{{ + Name: "voice", + File: config.File, + }} + + if config.Thumb != nil { + files = append(files, RequestFile{ + Name: "thumb", + File: config.Thumb, + }) + } + + return files } // LocationConfig contains information about a SendLocation request.

@@ -1313,8 +1388,11 @@ func (config SetChatPhotoConfig) method() string {

return "setChatPhoto" } -func (config SetChatPhotoConfig) name() string { - return "photo" +func (config SetChatPhotoConfig) files() []RequestFile { + return []RequestFile{{ + Name: "photo", + File: config.File, + }} } // DeleteChatPhotoConfig allows you to delete a group, supergroup, or channel's photo.
M helpers.gohelpers.go

@@ -55,97 +55,76 @@ // NewPhoto creates a new sendPhoto request.

// // Note that you must send animated GIFs as a document. func NewPhoto(chatID int64, file interface{}) PhotoConfig { - config := PhotoConfig{ + return PhotoConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewPhotoToChannel creates a new photo uploader to send a photo to a channel. // // Note that you must send animated GIFs as a document. func NewPhotoToChannel(username string, file interface{}) PhotoConfig { - config := PhotoConfig{ + return PhotoConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ ChannelUsername: username, }, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewAudio creates a new sendAudio request. func NewAudio(chatID int64, file interface{}) AudioConfig { - config := AudioConfig{ + return AudioConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewDocument creates a new sendDocument request. func NewDocument(chatID int64, file interface{}) DocumentConfig { - config := DocumentConfig{ + return DocumentConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewSticker creates a new sendSticker request. func NewSticker(chatID int64, file interface{}) StickerConfig { - config := StickerConfig{ + return StickerConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewVideo creates a new sendVideo request. func NewVideo(chatID int64, file interface{}) VideoConfig { - config := VideoConfig{ + return VideoConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewAnimation creates a new sendAnimation request. func NewAnimation(chatID int64, file interface{}) AnimationConfig { - config := AnimationConfig{ + return AnimationConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewVideoNote creates a new sendVideoNote request.

@@ -153,29 +132,23 @@ //

// chatID is where to send it, file is a string path to the file, // FileReader, or FileBytes. func NewVideoNote(chatID int64, length int, file interface{}) VideoNoteConfig { - config := VideoNoteConfig{ + return VideoNoteConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, Length: length, } - - config.AddFile(config.name(), file) - - return config } // NewVoice creates a new sendVoice request. func NewVoice(chatID int64, file interface{}) VoiceConfig { - config := VoiceConfig{ + return VoiceConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ChatID: chatID}, + File: file, }, } - - config.AddFile(config.name(), file) - - return config } // NewMediaGroup creates a new media group. Files should be an array of

@@ -763,17 +736,14 @@ }

// NewChatPhoto allows you to update the photo for a chat. func NewChatPhoto(chatID int64, photo interface{}) SetChatPhotoConfig { - config := SetChatPhotoConfig{ + return SetChatPhotoConfig{ BaseFile: BaseFile{ BaseChat: BaseChat{ ChatID: chatID, }, + File: photo, }, } - - config.AddFile(config.name(), photo) - - return config } // NewDeleteChatPhoto allows you to delete the photo for a chat.