Improve usability of new files for configs.

bot-api-6.1
Syfaro 2020-07-25 21:20:05 -05:00
parent ce4fc988c9
commit 99b74b8efa
3 changed files with 154 additions and 108 deletions

View File

@ -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 @@ func TestSendWithNewAudio(t *testing.T) {
msg.Title = "TEST"
msg.Duration = 10
msg.Performer = "TEST"
msg.MimeType = "audio/mpeg"
msg.FileSize = 688
_, err := bot.Send(msg)
if err != nil {

View File

@ -94,34 +94,11 @@ func (chat *BaseChat) params() (Params, error) {
// 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 @@ func (config ForwardConfig) method() string {
// PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct {
BaseFile
Thumb interface{}
Caption string
ParseMode string
}
@ -213,17 +191,30 @@ func (config PhotoConfig) params() (Params, error) {
return params, err
}
func (config PhotoConfig) name() string {
return "photo"
}
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 @@ func (config AudioConfig) params() (Params, error) {
return params, nil
}
func (config AudioConfig) name() string {
return "audio"
}
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 @@ func (config DocumentConfig) params() (Params, error) {
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) 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 @@ func (config VideoConfig) params() (Params, error) {
return params, err
}
func (config VideoConfig) name() string {
return "video"
}
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 @@ func (config VideoNoteConfig) params() (Params, error) {
return params, err
}
func (config VideoNoteConfig) name() string {
return "video_note"
}
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,14 +454,26 @@ func (config VoiceConfig) params() (Params, error) {
return params, err
}
func (config VoiceConfig) name() string {
return "voice"
}
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.
type LocationConfig struct {
BaseChat
@ -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.

View File

@ -55,97 +55,76 @@ func NewForward(chatID int64, fromChatID int64, messageID int) ForwardConfig {
//
// 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 @@ func NewAnimation(chatID int64, file interface{}) AnimationConfig {
// 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 @@ func NewChatDescription(chatID int64, description string) SetChatDescriptionConf
// 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.