From d3f7ac7197622f37672b08a9fec9743132cbf802 Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Fri, 20 Nov 2015 17:31:01 +0300 Subject: [PATCH] Refactorings --- bot.go | 181 +++++++++++------------------------------------------ configs.go | 30 +++++++++ helpers.go | 8 +-- 3 files changed, 71 insertions(+), 148 deletions(-) diff --git a/bot.go b/bot.go index e27ea55..e9b6cd6 100644 --- a/bot.go +++ b/bot.go @@ -214,6 +214,37 @@ func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) return message, nil } +func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error) { + params, err := config.Params() + if err != nil { + return Message{}, err + } + + file := config.GetFile() + + resp, err := bot.UploadFile(method, params, config.Name(), file) + if err != nil { + return Message{}, err + } + + var message Message + json.Unmarshal(resp.Result, &message) + + if bot.Debug { + log.Printf("%s resp: %+v\n", method, message) + } + + return message, nil +} + +func (bot *BotAPI) sendFile(method string, config Fileable) (Message, error) { + if config.UseExistingFile() { + return bot.sendExisting(method, config) + } + + return bot.uploadAndSend(method, config) +} + // SendMessage sends a Message to a chat. // // Requires ChatID and Text. @@ -274,30 +305,7 @@ func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) { // Caption, ReplyToMessageID, and ReplyMarkup are optional. // File should be either a string, FileBytes, or FileReader. func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { - if config.UseExisting { - return bot.sendExisting("SendPhoto", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("SendPhoto", params, "photo", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("SendPhoto resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("SendPhoto", config) } // SendAudio sends or uploads an audio clip to a chat. @@ -312,30 +320,7 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { // ReplyToMessageID and ReplyMarkup are optional. // File should be either a string, FileBytes, or FileReader. func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { - if config.UseExisting { - return bot.sendExisting("sendAudio", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendAudio", params, "audio", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendAudio resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("sendAudio", config) } // SendDocument sends or uploads a document to a chat. @@ -344,30 +329,7 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { // ReplyToMessageID and ReplyMarkup are optional. // File should be either a string, FileBytes, or FileReader. func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { - if config.UseExisting { - return bot.sendExisting("sendDocument", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendDocument", params, "document", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendDocument resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("sendDocument", config) } // SendVoice sends or uploads a playable voice to a chat. @@ -378,30 +340,7 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { // ReplyToMessageID and ReplyMarkup are optional. // File should be either a string, FileBytes, or FileReader. func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { - if config.UseExisting { - return bot.sendExisting("sendVoice", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("SendVoice", params, "voice", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("SendVoice resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("sendVoice", config) } // SendSticker sends or uploads a sticker to a chat. @@ -410,30 +349,7 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { // ReplyToMessageID and ReplyMarkup are optional. // File should be either a string, FileBytes, or FileReader. func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { - if config.UseExisting { - return bot.sendExisting("sendSticker", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendSticker", params, "sticker", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendSticker resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("sendSticker", config) } // SendVideo sends or uploads a video to a chat. @@ -442,30 +358,7 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { // ReplyToMessageID and ReplyMarkup are optional. // File should be either a string, FileBytes, or FileReader. func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) { - if config.UseExisting { - return bot.sendExisting("sendVideo", config) - } - - params, err := config.Params() - if err != nil { - return Message{}, err - } - - file := config.GetFile() - - resp, err := bot.UploadFile("sendVideo", params, "video", file) - if err != nil { - return Message{}, err - } - - var message Message - json.Unmarshal(resp.Result, &message) - - if bot.Debug { - log.Printf("sendVideo resp: %+v\n", message) - } - - return message, nil + return bot.sendFile("sendVideo", config) } // SendChatAction sets a current action in a chat. diff --git a/configs.go b/configs.go index 2d16394..3a65a6f 100644 --- a/configs.go +++ b/configs.go @@ -45,7 +45,9 @@ type Chattable interface { type Fileable interface { Chattable Params() (map[string]string, error) + Name() string GetFile() interface{} + UseExistingFile() bool } // Base struct for all chat event(Message, Photo and so on) @@ -95,6 +97,10 @@ func (file BaseFile) GetFile() interface{} { return result } +func (file BaseFile) UseExistingFile() bool { + return file.UseExisting +} + // MessageConfig contains information about a SendMessage request. type MessageConfig struct { BaseChat @@ -199,6 +205,10 @@ func (config PhotoConfig) Values() (url.Values, error) { return v, nil } +func (config PhotoConfig) Name() string { + return "photo" +} + // AudioConfig contains information about a SendAudio request. type AudioConfig struct { BaseFile @@ -264,6 +274,10 @@ func (config AudioConfig) Params() (map[string]string, error) { return params, nil } +func (config AudioConfig) Name() string { + return "audio" +} + // DocumentConfig contains information about a SendDocument request. type DocumentConfig struct { BaseFile @@ -308,6 +322,10 @@ func (config DocumentConfig) Params() (map[string]string, error) { return params, nil } +func (config DocumentConfig) Name() string { + return "document" +} + // StickerConfig contains information about a SendSticker request. type StickerConfig struct { BaseFile @@ -352,6 +370,10 @@ func (config StickerConfig) Params() (map[string]string, error) { return params, nil } +func (config StickerConfig) Name() string { + return "sticker" +} + // VideoConfig contains information about a SendVideo request. type VideoConfig struct { BaseFile @@ -404,6 +426,10 @@ func (config VideoConfig) Params() (map[string]string, error) { return params, nil } +func (config VideoConfig) Name() string { + return "viceo" +} + // VoiceConfig contains information about a SendVoice request. type VoiceConfig struct { BaseFile @@ -455,6 +481,10 @@ func (config VoiceConfig) Params() (map[string]string, error) { return params, nil } +func (config VoiceConfig) Name() string { + return "voice" +} + // LocationConfig contains information about a SendLocation request. type LocationConfig struct { BaseChat diff --git a/helpers.go b/helpers.go index 233dbe7..e2578e9 100644 --- a/helpers.go +++ b/helpers.go @@ -11,7 +11,7 @@ import ( func NewMessage(chatID int, text string) MessageConfig { return MessageConfig{ BaseChat: BaseChat{ChatID: chatID}, - Text: text, + Text: text, DisableWebPagePreview: false, ReplyToMessageID: 0, } @@ -23,7 +23,7 @@ func NewMessage(chatID int, text string) MessageConfig { // and messageID is the ID of the original message. func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig { return ForwardConfig{ - BaseChat: BaseChat{ChatID: chatID}, + BaseChat: BaseChat{ChatID: chatID}, FromChatID: fromChatID, MessageID: messageID, } @@ -160,7 +160,7 @@ func NewVoiceShare(chatID int, fileID string) VoiceConfig { // chatID is where to send it, latitude and longitude are coordinates. func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig { return LocationConfig{ - BaseChat: BaseChat{ChatID: chatID}, + BaseChat: BaseChat{ChatID: chatID}, Latitude: latitude, Longitude: longitude, ReplyToMessageID: 0, @@ -175,7 +175,7 @@ func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig func NewChatAction(chatID int, action string) ChatActionConfig { return ChatActionConfig{ BaseChat: BaseChat{ChatID: chatID}, - Action: action, + Action: action, } }