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) bot, _ := getBot(t)
msg := NewDocument(ChatID, "tests/voice.ogg") msg := NewDocument(ChatID, "tests/voice.ogg")
msg.AddFile("thumb", "tests/image.jpg") msg.Thumb = "tests/image.jpg"
_, err := bot.Send(msg) _, err := bot.Send(msg)
if err != nil { if err != nil {
@ -242,8 +242,6 @@ func TestSendWithNewAudio(t *testing.T) {
msg.Title = "TEST" msg.Title = "TEST"
msg.Duration = 10 msg.Duration = 10
msg.Performer = "TEST" msg.Performer = "TEST"
msg.MimeType = "audio/mpeg"
msg.FileSize = 688
_, err := bot.Send(msg) _, err := bot.Send(msg)
if err != nil { 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. // BaseFile is a base type for all file config types.
type BaseFile struct { type BaseFile struct {
BaseChat BaseChat
Files []RequestFile File interface{}
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,
})
} }
func (file BaseFile) params() (Params, error) { func (file BaseFile) params() (Params, error) {
params, err := file.BaseChat.params() return 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
} }
// BaseEdit is base type of all chat edits. // BaseEdit is base type of all chat edits.
@ -200,6 +177,7 @@ func (config ForwardConfig) method() string {
// PhotoConfig contains information about a SendPhoto request. // PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct { type PhotoConfig struct {
BaseFile BaseFile
Thumb interface{}
Caption string Caption string
ParseMode string ParseMode string
} }
@ -213,17 +191,30 @@ func (config PhotoConfig) params() (Params, error) {
return params, err return params, err
} }
func (config PhotoConfig) name() string {
return "photo"
}
func (config PhotoConfig) method() string { func (config PhotoConfig) method() string {
return "sendPhoto" 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. // AudioConfig contains information about a SendAudio request.
type AudioConfig struct { type AudioConfig struct {
BaseFile BaseFile
Thumb interface{}
Caption string Caption string
ParseMode string ParseMode string
Duration int Duration int
@ -246,17 +237,30 @@ func (config AudioConfig) params() (Params, error) {
return params, nil return params, nil
} }
func (config AudioConfig) name() string {
return "audio"
}
func (config AudioConfig) method() string { func (config AudioConfig) method() string {
return "sendAudio" 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. // DocumentConfig contains information about a SendDocument request.
type DocumentConfig struct { type DocumentConfig struct {
BaseFile BaseFile
Thumb interface{}
Caption string Caption string
ParseMode string ParseMode string
} }
@ -270,14 +274,26 @@ func (config DocumentConfig) params() (Params, error) {
return params, err return params, err
} }
func (config DocumentConfig) name() string {
return "document"
}
func (config DocumentConfig) method() string { func (config DocumentConfig) method() string {
return "sendDocument" 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. // StickerConfig contains information about a SendSticker request.
type StickerConfig struct { type StickerConfig struct {
BaseFile BaseFile
@ -287,17 +303,21 @@ func (config StickerConfig) params() (Params, error) {
return config.BaseChat.params() return config.BaseChat.params()
} }
func (config StickerConfig) name() string {
return "sticker"
}
func (config StickerConfig) method() string { func (config StickerConfig) method() string {
return "sendSticker" return "sendSticker"
} }
func (config StickerConfig) files() []RequestFile {
return []RequestFile{{
Name: "sticker",
File: config.File,
}}
}
// VideoConfig contains information about a SendVideo request. // VideoConfig contains information about a SendVideo request.
type VideoConfig struct { type VideoConfig struct {
BaseFile BaseFile
Thumb interface{}
Duration int Duration int
Caption string Caption string
ParseMode string ParseMode string
@ -315,18 +335,31 @@ func (config VideoConfig) params() (Params, error) {
return params, err return params, err
} }
func (config VideoConfig) name() string {
return "video"
}
func (config VideoConfig) method() string { func (config VideoConfig) method() string {
return "sendVideo" 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. // AnimationConfig contains information about a SendAnimation request.
type AnimationConfig struct { type AnimationConfig struct {
BaseFile BaseFile
Duration int Duration int
Thumb interface{}
Caption string Caption string
ParseMode string ParseMode string
} }
@ -349,9 +382,26 @@ func (config AnimationConfig) method() string {
return "sendAnimation" 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. // VideoNoteConfig contains information about a SendVideoNote request.
type VideoNoteConfig struct { type VideoNoteConfig struct {
BaseFile BaseFile
Thumb interface{}
Duration int Duration int
Length int Length int
} }
@ -365,17 +415,30 @@ func (config VideoNoteConfig) params() (Params, error) {
return params, err return params, err
} }
func (config VideoNoteConfig) name() string {
return "video_note"
}
func (config VideoNoteConfig) method() string { func (config VideoNoteConfig) method() string {
return "sendVideoNote" 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. // VoiceConfig contains information about a SendVoice request.
type VoiceConfig struct { type VoiceConfig struct {
BaseFile BaseFile
Thumb interface{}
Caption string Caption string
ParseMode string ParseMode string
Duration int Duration int
@ -391,14 +454,26 @@ func (config VoiceConfig) params() (Params, error) {
return params, err return params, err
} }
func (config VoiceConfig) name() string {
return "voice"
}
func (config VoiceConfig) method() string { func (config VoiceConfig) method() string {
return "sendVoice" 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. // LocationConfig contains information about a SendLocation request.
type LocationConfig struct { type LocationConfig struct {
BaseChat BaseChat
@ -1313,8 +1388,11 @@ func (config SetChatPhotoConfig) method() string {
return "setChatPhoto" return "setChatPhoto"
} }
func (config SetChatPhotoConfig) name() string { func (config SetChatPhotoConfig) files() []RequestFile {
return "photo" return []RequestFile{{
Name: "photo",
File: config.File,
}}
} }
// DeleteChatPhotoConfig allows you to delete a group, supergroup, or channel's photo. // 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. // Note that you must send animated GIFs as a document.
func NewPhoto(chatID int64, file interface{}) PhotoConfig { func NewPhoto(chatID int64, file interface{}) PhotoConfig {
config := PhotoConfig{ return PhotoConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, 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. // NewPhotoToChannel creates a new photo uploader to send a photo to a channel.
// //
// Note that you must send animated GIFs as a document. // Note that you must send animated GIFs as a document.
func NewPhotoToChannel(username string, file interface{}) PhotoConfig { func NewPhotoToChannel(username string, file interface{}) PhotoConfig {
config := PhotoConfig{ return PhotoConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ BaseChat: BaseChat{
ChannelUsername: username, ChannelUsername: username,
}, },
File: file,
}, },
} }
config.AddFile(config.name(), file)
return config
} }
// NewAudio creates a new sendAudio request. // NewAudio creates a new sendAudio request.
func NewAudio(chatID int64, file interface{}) AudioConfig { func NewAudio(chatID int64, file interface{}) AudioConfig {
config := AudioConfig{ return AudioConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
File: file,
}, },
} }
config.AddFile(config.name(), file)
return config
} }
// NewDocument creates a new sendDocument request. // NewDocument creates a new sendDocument request.
func NewDocument(chatID int64, file interface{}) DocumentConfig { func NewDocument(chatID int64, file interface{}) DocumentConfig {
config := DocumentConfig{ return DocumentConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
File: file,
}, },
} }
config.AddFile(config.name(), file)
return config
} }
// NewSticker creates a new sendSticker request. // NewSticker creates a new sendSticker request.
func NewSticker(chatID int64, file interface{}) StickerConfig { func NewSticker(chatID int64, file interface{}) StickerConfig {
config := StickerConfig{ return StickerConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
File: file,
}, },
} }
config.AddFile(config.name(), file)
return config
} }
// NewVideo creates a new sendVideo request. // NewVideo creates a new sendVideo request.
func NewVideo(chatID int64, file interface{}) VideoConfig { func NewVideo(chatID int64, file interface{}) VideoConfig {
config := VideoConfig{ return VideoConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
File: file,
}, },
} }
config.AddFile(config.name(), file)
return config
} }
// NewAnimation creates a new sendAnimation request. // NewAnimation creates a new sendAnimation request.
func NewAnimation(chatID int64, file interface{}) AnimationConfig { func NewAnimation(chatID int64, file interface{}) AnimationConfig {
config := AnimationConfig{ return AnimationConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
File: file,
}, },
} }
config.AddFile(config.name(), file)
return config
} }
// NewVideoNote creates a new sendVideoNote request. // 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, // chatID is where to send it, file is a string path to the file,
// FileReader, or FileBytes. // FileReader, or FileBytes.
func NewVideoNote(chatID int64, length int, file interface{}) VideoNoteConfig { func NewVideoNote(chatID int64, length int, file interface{}) VideoNoteConfig {
config := VideoNoteConfig{ return VideoNoteConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
File: file,
}, },
Length: length, Length: length,
} }
config.AddFile(config.name(), file)
return config
} }
// NewVoice creates a new sendVoice request. // NewVoice creates a new sendVoice request.
func NewVoice(chatID int64, file interface{}) VoiceConfig { func NewVoice(chatID int64, file interface{}) VoiceConfig {
config := VoiceConfig{ return VoiceConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ChatID: chatID}, 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 // 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. // NewChatPhoto allows you to update the photo for a chat.
func NewChatPhoto(chatID int64, photo interface{}) SetChatPhotoConfig { func NewChatPhoto(chatID int64, photo interface{}) SetChatPhotoConfig {
config := SetChatPhotoConfig{ return SetChatPhotoConfig{
BaseFile: BaseFile{ BaseFile: BaseFile{
BaseChat: BaseChat{ BaseChat: BaseChat{
ChatID: chatID, ChatID: chatID,
}, },
File: photo,
}, },
} }
config.AddFile(config.name(), photo)
return config
} }
// NewDeleteChatPhoto allows you to delete the photo for a chat. // NewDeleteChatPhoto allows you to delete the photo for a chat.