Refactoring

bot-api-6.1
Gleb Sinyavsky 2015-11-20 17:08:53 +03:00
parent 54b9c7e14b
commit 9361631c6d
3 changed files with 302 additions and 349 deletions

327
bot.go
View File

@ -188,7 +188,7 @@ func (bot *BotAPI) GetMe() (User, error) {
return user, nil return user, nil
} }
func (bot *BotAPI) Send(c Chattable) error { func (bot *BotAPI) Send(c BaseChat) error {
return nil return nil
} }
@ -199,13 +199,27 @@ func (bot *BotAPI) DebugLog(context string, v url.Values, message interface{}) {
} }
} }
func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) {
v, err := config.Values()
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest(method, v)
if err != nil {
return Message{}, err
}
return message, nil
}
// SendMessage sends a Message to a chat. // SendMessage sends a Message to a chat.
// //
// Requires ChatID and Text. // Requires ChatID and Text.
// DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional. // DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional.
func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) { func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
v, err := config.Values() v, err := config.Values()
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
@ -223,7 +237,10 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
// //
// Requires ChatID (destination), FromChatID (source), and MessageID. // Requires ChatID (destination), FromChatID (source), and MessageID.
func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) { func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
v, _ := config.Values() v, err := config.Values()
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("forwardMessage", v) message, err := bot.MakeMessageRequest("forwardMessage", v)
if err != nil { if err != nil {
@ -233,54 +250,40 @@ func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
return message, nil return message, nil
} }
// SendLocation sends a location to a chat.
//
// Requires ChatID, Latitude, and Longitude.
// ReplyToMessageID and ReplyMarkup are optional.
func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
v, err := config.Values()
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("sendLocation", v)
if err != nil {
return Message{}, err
}
return message, nil
}
// SendPhoto sends or uploads a photo to a chat. // SendPhoto sends or uploads a photo to a chat.
// //
// Requires ChatID and FileID OR File. // Requires ChatID and FileID OR File.
// Caption, ReplyToMessageID, and ReplyMarkup are optional. // Caption, ReplyToMessageID, and ReplyMarkup are optional.
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
if config.UseExistingPhoto { if config.UseExisting {
v, err := config.Values() return bot.sendExisting("SendPhoto", config)
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("SendPhoto", v)
if err != nil {
return Message{}, err
}
return message, nil
} }
params := make(map[string]string) params, err := config.Params()
if config.ChannelUsername != "" { if err != nil {
params["chat_id"] = config.ChannelUsername return Message{}, err
} else {
params["chat_id"] = strconv.Itoa(config.ChatID)
}
if config.Caption != "" {
params["caption"] = config.Caption
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return Message{}, err
}
params["reply_markup"] = string(data)
} }
var file interface{} file := config.GetFile()
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("SendPhoto", params, "photo", file) resp, err := bot.UploadFile("SendPhoto", params, "photo", file)
if err != nil { if err != nil {
@ -309,54 +312,16 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
if config.UseExistingAudio { if config.UseExisting {
v, err := config.Values() return bot.sendExisting("sendAudio", config)
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("sendAudio", v)
if err != nil {
return Message{}, err
}
return message, nil
} }
params := make(map[string]string) params, err := config.Params()
if err != nil {
if config.ChannelUsername != "" { return Message{}, err
params["chat_id"] = config.ChannelUsername
} else {
params["chat_id"] = strconv.Itoa(config.ChatID)
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return Message{}, err
}
params["reply_markup"] = string(data)
}
if config.Performer != "" {
params["performer"] = config.Performer
}
if config.Title != "" {
params["title"] = config.Title
} }
var file interface{} file := config.GetFile()
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendAudio", params, "audio", file) resp, err := bot.UploadFile("sendAudio", params, "audio", file)
if err != nil { if err != nil {
@ -379,45 +344,16 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
if config.UseExistingDocument { if config.UseExisting {
v, err := config.Values() return bot.sendExisting("sendDocument", config)
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("sendDocument", v)
if err != nil {
return Message{}, err
}
return message, nil
} }
params := make(map[string]string) params, err := config.Params()
if err != nil {
if config.ChannelUsername != "" { return Message{}, err
params["chat_id"] = config.ChannelUsername
} else {
params["chat_id"] = strconv.Itoa(config.ChatID)
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return Message{}, err
}
params["reply_markup"] = string(data)
} }
var file interface{} file := config.GetFile()
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendDocument", params, "document", file) resp, err := bot.UploadFile("sendDocument", params, "document", file)
if err != nil { if err != nil {
@ -442,48 +378,16 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
if config.UseExistingVoice { if config.UseExisting {
v, err := config.Values() return bot.sendExisting("sendVoice", config)
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("sendVoice", v)
if err != nil {
return Message{}, err
}
return message, nil
} }
params := make(map[string]string) params, err := config.Params()
if err != nil {
if config.ChannelUsername != "" { return Message{}, err
params["chat_id"] = config.ChannelUsername
} else {
params["chat_id"] = strconv.Itoa(config.ChatID)
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return Message{}, err
}
params["reply_markup"] = string(data)
} }
var file interface{} file := config.GetFile()
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("SendVoice", params, "voice", file) resp, err := bot.UploadFile("SendVoice", params, "voice", file)
if err != nil { if err != nil {
@ -506,45 +410,16 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
if config.UseExistingSticker { if config.UseExisting {
v, err := config.Values() return bot.sendExisting("sendSticker", config)
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("sendSticker", v)
if err != nil {
return Message{}, err
}
return message, nil
} }
params := make(map[string]string) params, err := config.Params()
if err != nil {
if config.ChannelUsername != "" { return Message{}, err
params["chat_id"] = config.ChannelUsername
} else {
params["chat_id"] = strconv.Itoa(config.ChatID)
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return Message{}, err
}
params["reply_markup"] = string(data)
} }
var file interface{} file := config.GetFile()
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendSticker", params, "sticker", file) resp, err := bot.UploadFile("sendSticker", params, "sticker", file)
if err != nil { if err != nil {
@ -567,45 +442,16 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) { func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
if config.UseExistingVideo { if config.UseExisting {
v, err := config.Values() return bot.sendExisting("sendVideo", config)
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("sendVideo", v)
if err != nil {
return Message{}, err
}
return message, nil
} }
params := make(map[string]string) params, err := config.Params()
if err != nil {
if config.ChannelUsername != "" { return Message{}, err
params["chat_id"] = config.ChannelUsername
} else {
params["chat_id"] = strconv.Itoa(config.ChatID)
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return Message{}, err
}
params["reply_markup"] = string(data)
} }
var file interface{} file := config.GetFile()
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendVideo", params, "video", file) resp, err := bot.UploadFile("sendVideo", params, "video", file)
if err != nil { if err != nil {
@ -622,31 +468,16 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
return message, nil return message, nil
} }
// SendLocation sends a location to a chat.
//
// Requires ChatID, Latitude, and Longitude.
// ReplyToMessageID and ReplyMarkup are optional.
func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
v, err := config.Values()
if err != nil {
return Message{}, err
}
message, err := bot.MakeMessageRequest("sendLocation", v)
if err != nil {
return Message{}, err
}
return message, nil
}
// SendChatAction sets a current action in a chat. // SendChatAction sets a current action in a chat.
// //
// Requires ChatID and a valid Action (see Chat constants). // Requires ChatID and a valid Action (see Chat constants).
func (bot *BotAPI) SendChatAction(config ChatActionConfig) error { func (bot *BotAPI) SendChatAction(config ChatActionConfig) error {
v, _ := config.Values() v, err := config.Values()
if err != nil {
return err
}
_, err := bot.MakeRequest("sendChatAction", v) _, err = bot.MakeRequest("sendChatAction", v)
if err != nil { if err != nil {
return err return err
} }

View File

@ -38,31 +38,66 @@ const (
ModeMarkdown = "Markdown" ModeMarkdown = "Markdown"
) )
type Chattable interface {
Values() (url.Values, error)
}
type Fileable interface {
Chattable
Params() (map[string]string, error)
GetFile() interface{}
}
// Base struct for all chat event(Message, Photo and so on) // Base struct for all chat event(Message, Photo and so on)
type Chattable struct { type BaseChat struct {
ChatID int ChatID int
ChannelUsername string ChannelUsername string
} }
type Fileable struct { func (chat *BaseChat) Values() (url.Values, error) {
FilePath string
File interface{}
FileID string
}
func (chattable *Chattable) Values() (url.Values, error) {
v := url.Values{} v := url.Values{}
if chattable.ChannelUsername != "" { if chat.ChannelUsername != "" {
v.Add("chat_id", chattable.ChannelUsername) v.Add("chat_id", chat.ChannelUsername)
} else { } else {
v.Add("chat_id", strconv.Itoa(chattable.ChatID)) v.Add("chat_id", strconv.Itoa(chat.ChatID))
} }
return v, nil return v, nil
} }
type BaseFile struct {
BaseChat
FilePath string
File interface{}
FileID string
UseExisting bool
}
func (file BaseFile) Params() (map[string]string, error) {
params := make(map[string]string)
if file.ChannelUsername != "" {
params["chat_id"] = file.ChannelUsername
} else {
params["chat_id"] = strconv.Itoa(file.ChatID)
}
return params, nil
}
func (file BaseFile) GetFile() interface{} {
var result interface{}
if file.FilePath == "" {
result = file.File
} else {
result = file.FilePath
}
return result
}
// MessageConfig contains information about a SendMessage request. // MessageConfig contains information about a SendMessage request.
type MessageConfig struct { type MessageConfig struct {
Chattable BaseChat
Text string Text string
ParseMode string ParseMode string
DisableWebPagePreview bool DisableWebPagePreview bool
@ -70,8 +105,8 @@ type MessageConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
} }
func (config *MessageConfig) Values() (url.Values, error) { func (config MessageConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values() v, _ := config.BaseChat.Values()
v.Add("text", config.Text) v.Add("text", config.Text)
v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview)) v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
if config.ParseMode != "" { if config.ParseMode != "" {
@ -94,14 +129,14 @@ func (config *MessageConfig) Values() (url.Values, error) {
// ForwardConfig contains information about a ForwardMessage request. // ForwardConfig contains information about a ForwardMessage request.
type ForwardConfig struct { type ForwardConfig struct {
Chattable BaseChat
FromChatID int FromChatID int
FromChannelUsername string FromChannelUsername string
MessageID int MessageID int
} }
func (config *ForwardConfig) Values() (url.Values, error) { func (config ForwardConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values() v, _ := config.BaseChat.Values()
if config.FromChannelUsername != "" { if config.FromChannelUsername != "" {
v.Add("chat_id", config.FromChannelUsername) v.Add("chat_id", config.FromChannelUsername)
@ -115,16 +150,35 @@ func (config *ForwardConfig) Values() (url.Values, error) {
// PhotoConfig contains information about a SendPhoto request. // PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct { type PhotoConfig struct {
Chattable BaseFile
Fileable
Caption string Caption string
ReplyToMessageID int ReplyToMessageID int
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingPhoto bool
} }
func (config *PhotoConfig) Values() (url.Values, error) { func (config PhotoConfig) Params() (map[string]string, error) {
v, _ := config.Chattable.Values() params, _ := config.BaseFile.Params()
if config.Caption != "" {
params["caption"] = config.Caption
}
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
func (config PhotoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("photo", config.FileID) v.Add("photo", config.FileID)
if config.Caption != "" { if config.Caption != "" {
@ -147,18 +201,16 @@ func (config *PhotoConfig) Values() (url.Values, error) {
// AudioConfig contains information about a SendAudio request. // AudioConfig contains information about a SendAudio request.
type AudioConfig struct { type AudioConfig struct {
Chattable BaseFile
Fileable
Duration int Duration int
Performer string Performer string
Title string Title string
ReplyToMessageID int ReplyToMessageID int
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingAudio bool
} }
func (config *AudioConfig) Values() (url.Values, error) { func (config AudioConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values() v, _ := config.BaseChat.Values()
v.Add("audio", config.FileID) v.Add("audio", config.FileID)
if config.ReplyToMessageID != 0 { if config.ReplyToMessageID != 0 {
@ -185,17 +237,42 @@ func (config *AudioConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// DocumentConfig contains information about a SendDocument request. func (config AudioConfig) Params() (map[string]string, error) {
type DocumentConfig struct { params, _ := config.BaseFile.Params()
Chattable
Fileable if config.ReplyToMessageID != 0 {
ReplyToMessageID int params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
ReplyMarkup interface{} }
UseExistingDocument bool if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
if config.Performer != "" {
params["performer"] = config.Performer
}
if config.Title != "" {
params["title"] = config.Title
}
return params, nil
} }
func (config *DocumentConfig) Values() (url.Values, error) { // DocumentConfig contains information about a SendDocument request.
v, _ := config.Chattable.Values() type DocumentConfig struct {
BaseFile
ReplyToMessageID int
ReplyMarkup interface{}
}
func (config DocumentConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("document", config.FileID) v.Add("document", config.FileID)
if config.ReplyToMessageID != 0 { if config.ReplyToMessageID != 0 {
@ -213,17 +290,33 @@ func (config *DocumentConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
// StickerConfig contains information about a SendSticker request. func (config DocumentConfig) Params() (map[string]string, error) {
type StickerConfig struct { params, _ := config.BaseFile.Params()
Chattable
Fileable if config.ReplyToMessageID != 0 {
ReplyToMessageID int params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
ReplyMarkup interface{} }
UseExistingSticker bool if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
} }
func (config *StickerConfig) Values() (url.Values, error) { // StickerConfig contains information about a SendSticker request.
v, _ := config.Chattable.Values() type StickerConfig struct {
BaseFile
ReplyToMessageID int
ReplyMarkup interface{}
}
func (config StickerConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values()
v.Add("sticker", config.FileID) v.Add("sticker", config.FileID)
if config.ReplyToMessageID != 0 { if config.ReplyToMessageID != 0 {
@ -241,19 +334,35 @@ func (config *StickerConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
func (config StickerConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
// VideoConfig contains information about a SendVideo request. // VideoConfig contains information about a SendVideo request.
type VideoConfig struct { type VideoConfig struct {
Chattable BaseFile
Fileable
Duration int Duration int
Caption string Caption string
ReplyToMessageID int ReplyToMessageID int
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingVideo bool
} }
func (config *VideoConfig) Values() (url.Values, error) { func (config VideoConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values() v, _ := config.BaseChat.Values()
v.Add("video", config.FileID) v.Add("video", config.FileID)
if config.ReplyToMessageID != 0 { if config.ReplyToMessageID != 0 {
@ -277,18 +386,34 @@ func (config *VideoConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
func (config VideoConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
// VoiceConfig contains information about a SendVoice request. // VoiceConfig contains information about a SendVoice request.
type VoiceConfig struct { type VoiceConfig struct {
Chattable BaseFile
Fileable
Duration int Duration int
ReplyToMessageID int ReplyToMessageID int
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingVoice bool
} }
func (config *VoiceConfig) Values() (url.Values, error) { func (config VoiceConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values() v, _ := config.BaseChat.Values()
v.Add("voice", config.FileID) v.Add("voice", config.FileID)
if config.ReplyToMessageID != 0 { if config.ReplyToMessageID != 0 {
@ -309,17 +434,38 @@ func (config *VoiceConfig) Values() (url.Values, error) {
return v, nil return v, nil
} }
func (config VoiceConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil
}
// LocationConfig contains information about a SendLocation request. // LocationConfig contains information about a SendLocation request.
type LocationConfig struct { type LocationConfig struct {
Chattable BaseChat
Latitude float64 Latitude float64
Longitude float64 Longitude float64
ReplyToMessageID int ReplyToMessageID int
ReplyMarkup interface{} ReplyMarkup interface{}
} }
func (config *LocationConfig) Values() (url.Values, error) { func (config LocationConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values() v, _ := config.BaseChat.Values()
v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64)) v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64)) v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
@ -341,12 +487,12 @@ func (config *LocationConfig) Values() (url.Values, error) {
// ChatActionConfig contains information about a SendChatAction request. // ChatActionConfig contains information about a SendChatAction request.
type ChatActionConfig struct { type ChatActionConfig struct {
Chattable BaseChat
Action string Action string
} }
func (config *ChatActionConfig) Values() (url.Values, error) { func (config ChatActionConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values() v, _ := config.BaseChat.Values()
v.Add("action", config.Action) v.Add("action", config.Action)
return v, nil return v, nil
} }

View File

@ -10,7 +10,7 @@ import (
// chatID is where to send it, text is the message text. // chatID is where to send it, text is the message text.
func NewMessage(chatID int, text string) MessageConfig { func NewMessage(chatID int, text string) MessageConfig {
return MessageConfig{ return MessageConfig{
Chattable: Chattable{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
Text: text, Text: text,
DisableWebPagePreview: false, DisableWebPagePreview: false,
ReplyToMessageID: 0, ReplyToMessageID: 0,
@ -23,7 +23,7 @@ func NewMessage(chatID int, text string) MessageConfig {
// and messageID is the ID of the original message. // and messageID is the ID of the original message.
func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig { func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
return ForwardConfig{ return ForwardConfig{
Chattable: Chattable{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
FromChatID: fromChatID, FromChatID: fromChatID,
MessageID: messageID, MessageID: messageID,
} }
@ -36,9 +36,7 @@ func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewPhotoUpload(chatID int, file interface{}) PhotoConfig { func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
return PhotoConfig{ return PhotoConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
Fileable: Fileable{File: file},
UseExistingPhoto: false,
} }
} }
@ -48,9 +46,7 @@ func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
// chatID is where to send it, fileID is the ID of the file already uploaded. // chatID is where to send it, fileID is the ID of the file already uploaded.
func NewPhotoShare(chatID int, fileID string) PhotoConfig { func NewPhotoShare(chatID int, fileID string) PhotoConfig {
return PhotoConfig{ return PhotoConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
Fileable: Fileable{FileID: fileID},
UseExistingPhoto: true,
} }
} }
@ -61,9 +57,7 @@ func NewPhotoShare(chatID int, fileID string) PhotoConfig {
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewAudioUpload(chatID int, file interface{}) AudioConfig { func NewAudioUpload(chatID int, file interface{}) AudioConfig {
return AudioConfig{ return AudioConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
Fileable: Fileable{File: file},
UseExistingAudio: false,
} }
} }
@ -73,9 +67,7 @@ func NewAudioUpload(chatID int, file interface{}) AudioConfig {
// chatID is where to send it, fileID is the ID of the audio already uploaded. // chatID is where to send it, fileID is the ID of the audio already uploaded.
func NewAudioShare(chatID int, fileID string) AudioConfig { func NewAudioShare(chatID int, fileID string) AudioConfig {
return AudioConfig{ return AudioConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
Fileable: Fileable{FileID: fileID},
UseExistingAudio: true,
} }
} }
@ -86,9 +78,7 @@ func NewAudioShare(chatID int, fileID string) AudioConfig {
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewDocumentUpload(chatID int, file interface{}) DocumentConfig { func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
return DocumentConfig{ return DocumentConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
Fileable: Fileable{File: file},
UseExistingDocument: false,
} }
} }
@ -98,9 +88,7 @@ func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
// chatID is where to send it, fileID is the ID of the document already uploaded. // chatID is where to send it, fileID is the ID of the document already uploaded.
func NewDocumentShare(chatID int, fileID string) DocumentConfig { func NewDocumentShare(chatID int, fileID string) DocumentConfig {
return DocumentConfig{ return DocumentConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
Fileable: Fileable{FileID: fileID},
UseExistingDocument: true,
} }
} }
@ -110,9 +98,7 @@ func NewDocumentShare(chatID int, fileID string) DocumentConfig {
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewStickerUpload(chatID int, file interface{}) StickerConfig { func NewStickerUpload(chatID int, file interface{}) StickerConfig {
return StickerConfig{ return StickerConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
Fileable: Fileable{File: file},
UseExistingSticker: false,
} }
} }
@ -122,9 +108,7 @@ func NewStickerUpload(chatID int, file interface{}) StickerConfig {
// chatID is where to send it, fileID is the ID of the sticker already uploaded. // chatID is where to send it, fileID is the ID of the sticker already uploaded.
func NewStickerShare(chatID int, fileID string) StickerConfig { func NewStickerShare(chatID int, fileID string) StickerConfig {
return StickerConfig{ return StickerConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
Fileable: Fileable{FileID: fileID},
UseExistingSticker: true,
} }
} }
@ -135,9 +119,7 @@ func NewStickerShare(chatID int, fileID string) StickerConfig {
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewVideoUpload(chatID int, file interface{}) VideoConfig { func NewVideoUpload(chatID int, file interface{}) VideoConfig {
return VideoConfig{ return VideoConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
Fileable: Fileable{File: file},
UseExistingVideo: false,
} }
} }
@ -147,9 +129,7 @@ func NewVideoUpload(chatID int, file interface{}) VideoConfig {
// chatID is where to send it, fileID is the ID of the video already uploaded. // chatID is where to send it, fileID is the ID of the video already uploaded.
func NewVideoShare(chatID int, fileID string) VideoConfig { func NewVideoShare(chatID int, fileID string) VideoConfig {
return VideoConfig{ return VideoConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
UseExistingVideo: true,
Fileable: Fileable{FileID: fileID},
} }
} }
@ -160,9 +140,7 @@ func NewVideoShare(chatID int, fileID string) VideoConfig {
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewVoiceUpload(chatID int, file interface{}) VoiceConfig { func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
return VoiceConfig{ return VoiceConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
Fileable: Fileable{File: file},
UseExistingVoice: false,
} }
} }
@ -172,9 +150,7 @@ func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
// chatID is where to send it, fileID is the ID of the video already uploaded. // chatID is where to send it, fileID is the ID of the video already uploaded.
func NewVoiceShare(chatID int, fileID string) VoiceConfig { func NewVoiceShare(chatID int, fileID string) VoiceConfig {
return VoiceConfig{ return VoiceConfig{
Chattable: Chattable{ChatID: chatID}, BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
Fileable: Fileable{FileID: fileID},
UseExistingVoice: true,
} }
} }
@ -184,7 +160,7 @@ func NewVoiceShare(chatID int, fileID string) VoiceConfig {
// chatID is where to send it, latitude and longitude are coordinates. // chatID is where to send it, latitude and longitude are coordinates.
func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig { func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig {
return LocationConfig{ return LocationConfig{
Chattable: Chattable{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
Latitude: latitude, Latitude: latitude,
Longitude: longitude, Longitude: longitude,
ReplyToMessageID: 0, ReplyToMessageID: 0,
@ -198,7 +174,7 @@ func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig
// chatID is where to send it, action should be set via CHAT constants. // chatID is where to send it, action should be set via CHAT constants.
func NewChatAction(chatID int, action string) ChatActionConfig { func NewChatAction(chatID int, action string) ChatActionConfig {
return ChatActionConfig{ return ChatActionConfig{
Chattable: Chattable{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID},
Action: action, Action: action,
} }
} }